I'm learning environment mapping in OpenGL by following this page.
In his vertex shader, the author calculates the vertex normal in eye space with the following code:
nEye = vec3(viewMatrix * modelMatrix * vec4(vertexNormal, 0.0));
This works, but I've fooled around and made modifications to the shader code, so that it calculates the vertex normal with this code:
nEye = vec3(normalMatrix * vec4(vertexNormal, 0.0));
Where normalMatrix is a uniform 4x4 matrix I calculated outside the shader:
objectNormalMatrix = viewMatrix * modelMatrix;
objectNormalMatrix.invert();
objectNormalMatrix.transpose();
(I read from this question that the above is how you are supposed to calculate the normal matrix).
For either method of calculating the vertex normal I use, I get the same (working) graphical result, and this is what is confusing me.
My question is, why is(viewMatrix * modelMatrix)
apparently equivalent normalMatrix
? In calculating the normal matrix, I invert and transpose, but I do none of that when just multiplying by (viewMatrix * modelMatrix)
.
Here is some extra info that might be relevant:
- The matrix library I use in my application is column major order (same as OpenGL).
- When I pass my matrices to GLSL with
glUniformMatrix
, I havetranspose
asfalse
. - The view matrix and model matrix in GLSL are uniforms from the application, and they are the exact same view matrix and model matrix matrices I use in my calculation of the normal matrix.
viewMatrix * modelMatrix
does not contain any shear or non-uniform scaling term, then the normal matrix is just the upper 3x3 of the modelview matrix, which has the same effect as multiplying the modelview by (x,y,z,0) instead of (x,y,z,1). Shear and nonuniform scale are fairly rare nowadays, which explicit meshes. \$\endgroup\$(viewMatrix * modelMatrix)
doesn't work butnormalMatrix
does! Thanks for the help, I will accept if you post this as an answer. \$\endgroup\$