GLM to create gl_NormalMatrix

Hey,

I managed to use this nice GLM library to replace glOrtho(),
gluPerspective() and gluLookAt() to create the matrices for
the deprecated shader uniforms gl_*Matrix.

What’s left is to create gl_NormalMatrix.
I already know it must be the transpose of the inverse
of the upper left 3x3 part of the gl_ModelViewMatrix.

So this is my code:

gl_NormalMatrix= glm::transpose(glm::inverse(glm::mat3(gl_ModelViewMatrix)));

Is this correct? Anyone already tried to create a
gl_NormalMatrix using GLM and can confirm that
this code should do the trick? :slight_smile:

Thanks!

It should work!

If you care a bit about efficiency:


#include <glm/gtx/inverse_transpose.hpp>

gl_NormalMatrix = glm::inverseTranspose(glm::mat3(gl_ModelViewMatrix));

Great thanks! :slight_smile:

The model matrix shouldn’t be based on the ModelView matrix, should it? It looks more logical that it should be based on the model matrix only?

If you do your lighting in eye/camera/view/etc space, rather than world space, then it should. And generally, that’s the space where lighting is done.

You could also simply use simply the model-view matrix if you do only orthogonal transformations (i.e. translation and rotation). This is so because the inverse of an orthogonal matrix is its transpose, so the inverse-transpose is the original matrix itself. This way you can save quite some work as calculating the inverse of a matrix is a pretty expensive operation.