View Full Version : gl_NormalMatrix : Inverse?

09-06-2007, 12:29 PM
I have a situation where I need to have the untransformed normal in the pixel shader. But only the transformed normal ( gl_NormalMatrix * gl_Normal ) is available to me. (to make a long story short: passing the untransform normal from the vertex shader to the pixel shader is not an option)

I'm a little confused about the matrix math needed to transform the eye normal back to world normal.

I've tried different things but can't seem to get it right.

09-06-2007, 12:47 PM
You can use the gl_NormalMatrixInverse or gl_NormalMatrixTranspose. A problem is that both requires a higher GLSL version.

A GLSL 1.0 compatible variant is to transpose the normal Matrix in a shader, it doesn't cost any instructions (the compiler will use 3 dot3 instructions instead Mul+Madd+Madd)

mat3 tnormalmat = transpose(gl_NormalMatrix);

or use that variant:

vec3 untransformed = vec3(dot(normal, gl_NormalMatrix[0]),dot(normal, gl_NormalMatrix[1]),dot(normal, gl_NormalMatrix[2]));