PDA

View Full Version : GLM to create gl_NormalMatrix



Schnulla
06-01-2010, 08:22 PM
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_ModelView Matrix)));

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

Thanks!

Groovounet
06-02-2010, 05:53 AM
It should work!

If you care a bit about efficiency:


#include <glm/gtx/inverse_transpose.hpp>

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

Schnulla
06-02-2010, 06:54 AM
Great thanks! :)

Kopelrativ
06-26-2011, 11:20 PM
It should work!

If you care a bit about efficiency:


#include <glm/gtx/inverse_transpose.hpp>

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




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?

Alfonse Reinheart
06-27-2011, 12:50 AM
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.

aqnuep
06-27-2011, 04:17 AM
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.