ADDED: Support model normal matrix location #1691
This commit is contained in:
parent
2488d361b6
commit
cdc3754449
6 changed files with 18 additions and 13 deletions
|
@ -9,6 +9,7 @@ in vec4 vertexColor;
|
||||||
// Input uniform values
|
// Input uniform values
|
||||||
uniform mat4 mvp;
|
uniform mat4 mvp;
|
||||||
uniform mat4 matModel;
|
uniform mat4 matModel;
|
||||||
|
uniform mat4 matNormal;
|
||||||
|
|
||||||
// Output vertex attributes (to fragment shader)
|
// Output vertex attributes (to fragment shader)
|
||||||
out vec3 fragPosition;
|
out vec3 fragPosition;
|
||||||
|
@ -24,9 +25,7 @@ void main()
|
||||||
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
||||||
fragTexCoord = vertexTexCoord;
|
fragTexCoord = vertexTexCoord;
|
||||||
fragColor = vertexColor;
|
fragColor = vertexColor;
|
||||||
|
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
|
||||||
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
|
||||||
fragNormal = normalize(normalMatrix*vertexNormal);
|
|
||||||
|
|
||||||
// Calculate final vertex position
|
// Calculate final vertex position
|
||||||
gl_Position = mvp*vec4(vertexPosition, 1.0);
|
gl_Position = mvp*vec4(vertexPosition, 1.0);
|
||||||
|
|
|
@ -10,6 +10,7 @@ in mat4 instanceTransform;
|
||||||
|
|
||||||
// Input uniform values
|
// Input uniform values
|
||||||
uniform mat4 mvp;
|
uniform mat4 mvp;
|
||||||
|
uniform mat4 matNormal;
|
||||||
|
|
||||||
// Output vertex attributes (to fragment shader)
|
// Output vertex attributes (to fragment shader)
|
||||||
out vec3 fragPosition;
|
out vec3 fragPosition;
|
||||||
|
@ -21,15 +22,14 @@ out vec3 fragNormal;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
// Compute MVP for current instance
|
||||||
|
mat4 mvpi = mvp*instanceTransform;
|
||||||
|
|
||||||
// Send vertex attributes to fragment shader
|
// Send vertex attributes to fragment shader
|
||||||
fragPosition = vec3(vec4(vertexPosition, 1.0));
|
fragPosition = vec3(mvpi*vec4(vertexPosition, 1.0));
|
||||||
fragTexCoord = vertexTexCoord;
|
fragTexCoord = vertexTexCoord;
|
||||||
fragColor = vertexColor;
|
fragColor = vertexColor;
|
||||||
|
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
|
||||||
mat3 normalMatrix = transpose(inverse(mat3(instanceTransform)));
|
|
||||||
fragNormal = normalize(normalMatrix*vertexNormal);
|
|
||||||
|
|
||||||
mat4 mvpi = mvp*instanceTransform;
|
|
||||||
|
|
||||||
// Calculate final vertex position
|
// Calculate final vertex position
|
||||||
gl_Position = mvpi*vec4(vertexPosition, 1.0);
|
gl_Position = mvpi*vec4(vertexPosition, 1.0);
|
||||||
|
|
|
@ -2056,8 +2056,9 @@ Shader LoadShader(const char *vsFileName, const char *fsFileName)
|
||||||
|
|
||||||
// Get handles to GLSL uniform locations (vertex shader)
|
// Get handles to GLSL uniform locations (vertex shader)
|
||||||
shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, "mvp");
|
shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, "mvp");
|
||||||
shader.locs[SHADER_LOC_MATRIX_PROJECTION] = rlGetLocationUniform(shader.id, "projection");
|
|
||||||
shader.locs[SHADER_LOC_MATRIX_VIEW] = rlGetLocationUniform(shader.id, "view");
|
shader.locs[SHADER_LOC_MATRIX_VIEW] = rlGetLocationUniform(shader.id, "view");
|
||||||
|
shader.locs[SHADER_LOC_MATRIX_PROJECTION] = rlGetLocationUniform(shader.id, "projection");
|
||||||
|
shader.locs[SHADER_LOC_MATRIX_NORMAL] = rlGetLocationUniform(shader.id, "matNormal");
|
||||||
|
|
||||||
// Get handles to GLSL uniform locations (fragment shader)
|
// Get handles to GLSL uniform locations (fragment shader)
|
||||||
shader.locs[SHADER_LOC_COLOR_DIFFUSE] = rlGetLocationUniform(shader.id, "colDiffuse");
|
shader.locs[SHADER_LOC_COLOR_DIFFUSE] = rlGetLocationUniform(shader.id, "colDiffuse");
|
||||||
|
|
|
@ -1058,6 +1058,9 @@ void DrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int ins
|
||||||
// transform: function parameter transformation
|
// transform: function parameter transformation
|
||||||
matModelView = MatrixMultiply(transforms[0], MatrixMultiply(rlGetMatrixTransform(), matView));
|
matModelView = MatrixMultiply(transforms[0], MatrixMultiply(rlGetMatrixTransform(), matView));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upload model normal matrix (if locations available)
|
||||||
|
if (material.shader.locs[SHADER_LOC_MATRIX_NORMAL] != -1) rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_NORMAL], MatrixTranspose(MatrixInvert(matModelView)));
|
||||||
//-----------------------------------------------------
|
//-----------------------------------------------------
|
||||||
|
|
||||||
// Bind active texture maps (if available)
|
// Bind active texture maps (if available)
|
||||||
|
|
|
@ -739,9 +739,10 @@ typedef enum {
|
||||||
SHADER_LOC_VERTEX_TANGENT,
|
SHADER_LOC_VERTEX_TANGENT,
|
||||||
SHADER_LOC_VERTEX_COLOR,
|
SHADER_LOC_VERTEX_COLOR,
|
||||||
SHADER_LOC_MATRIX_MVP,
|
SHADER_LOC_MATRIX_MVP,
|
||||||
SHADER_LOC_MATRIX_MODEL,
|
|
||||||
SHADER_LOC_MATRIX_VIEW,
|
SHADER_LOC_MATRIX_VIEW,
|
||||||
SHADER_LOC_MATRIX_PROJECTION,
|
SHADER_LOC_MATRIX_PROJECTION,
|
||||||
|
SHADER_LOC_MATRIX_MODEL,
|
||||||
|
SHADER_LOC_MATRIX_NORMAL,
|
||||||
SHADER_LOC_VECTOR_VIEW,
|
SHADER_LOC_VECTOR_VIEW,
|
||||||
SHADER_LOC_COLOR_DIFFUSE,
|
SHADER_LOC_COLOR_DIFFUSE,
|
||||||
SHADER_LOC_COLOR_SPECULAR,
|
SHADER_LOC_COLOR_SPECULAR,
|
||||||
|
|
|
@ -417,6 +417,7 @@ typedef enum {
|
||||||
SHADER_LOC_MATRIX_MVP,
|
SHADER_LOC_MATRIX_MVP,
|
||||||
SHADER_LOC_MATRIX_MODEL,
|
SHADER_LOC_MATRIX_MODEL,
|
||||||
SHADER_LOC_MATRIX_VIEW,
|
SHADER_LOC_MATRIX_VIEW,
|
||||||
|
SHADER_LOC_MATRIX_NORMAL,
|
||||||
SHADER_LOC_MATRIX_PROJECTION,
|
SHADER_LOC_MATRIX_PROJECTION,
|
||||||
SHADER_LOC_VECTOR_VIEW,
|
SHADER_LOC_VECTOR_VIEW,
|
||||||
SHADER_LOC_COLOR_DIFFUSE,
|
SHADER_LOC_COLOR_DIFFUSE,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue