mirror of
https://github.com/raysan5/raylib.git
synced 2025-12-25 10:22:33 -05:00
Added section for explaining DrawMeshInstanced
@ -20,6 +20,7 @@
|
||||
- [Why are my sound files not working?](#why-are-my-sound-files-not-working)
|
||||
- [How do I setup a custom icon for my executable?](#how-do-i-setup-a-custom-icon-for-my-executable)
|
||||
- [How I deal with UTF-16 strings?](#how-i-deal-with-utf-16-strings)
|
||||
- [Why does `DrawMeshInstanced` not draw anything?](#why-does-drawmeshinstanced-not-draw-anything)
|
||||
|
||||
## Can I run raylib in my old PC?
|
||||
|
||||
@ -297,4 +298,125 @@ UnloadImage(icon);
|
||||
|
||||
## How I deal with UTF-16 strings?
|
||||
|
||||
raylib supports by default UTF-8 strings, actually, text drawing functions expect to receive UTF-8 strings as inputs but sometimes source text is provided as UTF-16. [Here is a handy conversion library](https://gist.github.com/gulrak/2eda01eacebdb308787b639fa30958b3).
|
||||
raylib supports by default UTF-8 strings, actually, text drawing functions expect to receive UTF-8 strings as inputs but sometimes source text is provided as UTF-16. [Here is a handy conversion library](https://gist.github.com/gulrak/2eda01eacebdb308787b639fa30958b3).
|
||||
|
||||
## Why does `DrawMeshInstanced` not draw anything?
|
||||
|
||||
You are likely using the default material when calling the function. `DrawMeshInstanced` does not work with the default shader.
|
||||
|
||||
To create a vertex shader that would work with instancing, you must provide an input for the model transform matrix (the transform matrix for the instance) as a vertex attribute, and link its location to `SHADER_LOC_MATRIX_MODEL`. By default `SHADER_LOC_MATRIX_MODEL` binds as an uinform input in yout shader, you must use `GetShaderLocationAttrib` to bind to it as an attribute.
|
||||
|
||||
<details>
|
||||
<summary>Vertex Shader Example</summary>
|
||||
|
||||
```glsl
|
||||
#version 330
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 vertexPosition; // vertex position relative to origin
|
||||
in vec2 vertexTexCoord; // texture coord of vertex
|
||||
in mat4 instanceTransform; // model transformation matrix
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 mvp; // model-view-projection
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec2 fragTexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Pass texture coord
|
||||
fragTexCoord = vertexTexCoord;
|
||||
// Compute MVP for current instance
|
||||
mat4 mvpi = mvp*instanceTransform;
|
||||
// Calculate final vertex position
|
||||
gl_Position = mvpi*vec4(vertexPosition, 1.0);
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
<details>
|
||||
<summary>Fragment Shader Example</summary>
|
||||
|
||||
```glsl
|
||||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec3 fragPosition;
|
||||
in vec2 fragTexCoord;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Texel color fetching from texture sampler
|
||||
vec4 texelColor = texture(texture0, fragTexCoord);
|
||||
// colorize texture with diffuse color
|
||||
finalColor = colDiffuse*texelColor;
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Full C Example</summary>
|
||||
|
||||
```c
|
||||
const char fs[] =
|
||||
"#version 330 \n"
|
||||
" \n"
|
||||
"// Input vertex attributes (from vertex shader) \n"
|
||||
"in vec3 fragPosition; \n"
|
||||
"in vec2 fragTexCoord; \n"
|
||||
" \n"
|
||||
"// Input uniform values \n"
|
||||
"uniform sampler2D texture0; \n"
|
||||
"uniform vec4 colDiffuse; \n"
|
||||
" \n"
|
||||
"// Output fragment color \n"
|
||||
"out vec4 finalColor; \n"
|
||||
" \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" // Texel color fetching from texture sampler \n"
|
||||
" vec4 texelColor = texture(texture0, fragTexCoord); \n"
|
||||
" // colorize texture with diffuse color \n"
|
||||
" finalColor = colDiffuse*texelColor; \n"
|
||||
"} \n";
|
||||
|
||||
const char vs[] =
|
||||
"#version 330 \n"
|
||||
" \n"
|
||||
"// Input vertex attributes \n"
|
||||
"in vec3 vertexPosition; // vertex position relative to origin \n"
|
||||
"in vec2 vertexTexCoord; // texture coord of vertex \n"
|
||||
"in mat4 instanceTransform; // model transformation matrix \n"
|
||||
" \n"
|
||||
"// Input uniform values \n"
|
||||
"uniform mat4 mvp; // model-view-projection \n"
|
||||
" \n"
|
||||
"// Output vertex attributes (to fragment shader) \n"
|
||||
"out vec2 fragTexCoord; \n"
|
||||
" \n"
|
||||
"void main() \n"
|
||||
"{ \n"
|
||||
" // Pass texture coord \n"
|
||||
" fragTexCoord = vertexTexCoord; \n"
|
||||
" // Compute MVP for current instance \n"
|
||||
" mat4 mvpi = mvp*instanceTransform; \n"
|
||||
" // Calculate final vertex position \n"
|
||||
" gl_Position = mvpi*vec4(vertexPosition, 1.0); \n"
|
||||
"} \n";
|
||||
|
||||
Shader shader = LoadShaderFromMemory(vs, fs);
|
||||
shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform");
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
Reference in New Issue
Block a user