iliak's picture

Getting normal matrix from modelview matrix


How can I get the normal matrix (mat3) from the modelview matrix (mat4) for GLSL shaders ?




Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hannesh's picture

gl_NormalMatrix in your vertex shader ;)

iliak's picture

no more in OpenGL > 3.x...

Shocker's picture

When I want to get the normalmatrix, I prepare a normalmatrix variable as Matrix4 and substitute "Inverse of modelview matrix" for it by using Matrix4.Invert .
Next, use GL.UniformMatrix4 for loading the variables into shaders .
Then, in a shader using normalmatrix(type is mat4), write "mat3(normalmatrix)" . It means taking upper-left 3x3 of the normalmatrix .

You can get the inversion matrix by using inverse(mat 4) as well.

iliak's picture

Ok, thank you !

Shocker's picture

Sorry . I have forgotten to write about transposing matrix .
You can transpose a matrix by specifying an argument in UniformMatrix4 or writing "transpose(mat 4)" .

rotard's picture

I'm not sure I follow... Do you have a code sample you could post?