gl_NormalMatrix in GLSL

Started by
5 comments, last by me_here_me 16 years, 7 months ago
Hi, In the FS, I am calculating the normals over the surface of a 3D cube dataset and then displaying the normals though gl_FragColor. As expected, 3 sides of the cube show up as red, blue and green, while the rest of the 3 are black. As I rotate the dataset, all the sides keep their colors and this is what should be happening. :) Now I want that the color of the normals to be updated while rotating. The side facing the camera is always blue. As I rotate the, values of the normals are updated. Problem: this is not happening. I multiplied the normals with the gl_NormalMatrix before displaying them through gl_FragColor, but I am getting garbage values. And the color of the sides of the cube are updated randomly. any advice on the problem regards
Advertisement
You can try

vec3 mynormal=vec3(gl_ModelViewMatrix * vec4(gl_Normal, 0.0));

since it will most likely give the same result. If your not using a non uniform scaling like glScalef(1.0, 2.0, 3.0) then you should be fine.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
if you want the side in front of camera to be always blue try this
gl_FragColor.rgb = gl_NormalMatrix * norm;
thanks for the replies

I am using non-uniform scaling and
Quote:
vec3 mynormal=vec3(gl_ModelViewMatrix * vec4(gl_Normal, 0.0));

shows the same results as before :(

Quote:
gl_FragColor.rgb = gl_NormalMatrix * norm;

This is what I am doing and does not work. I cant figure out why. program works correctly if i do:

gl_FragColor = vec4(norm,1.0);

as soon as I am doing:
norm = gl_NormalMatrix * norm;
gl_FragColor = vec4(norm,1.0);

it outputs garbage :(

It's possible that those matrices are not being updated properly and that depends on your drivers.
Another thing to try is to generate that matrix yourself (gl_NormalMatrix is just the inverse transpose of the modelview) and upload it as a uniform in your shader.

Also, have you tried gl_NormalMatrix * norm; in your vertex shader? Does it work from there?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
I cannot try the gl_NormalMatrix in the vertex shader as I am doing volume rendering. Normals are calculated in the fragment shader and can be used there.

I will try to generate the matrix myself now.

hope that works :)
Good news: its solved
bad one, i dont know why its solved

I tried to take the inverse transpose of the model view matrix in the C code and pass it on to the shader using a uniform variable and there was no change in results. I was getting the same image.

I then tried to multiply the normal with gl_ModelViewProjectionMatrix, just to see what happens, and it works :)

So I finalized it by multiplying the normal with the gl_ModelViewProjectionMatrixInverseTranspose and it works as it should.

but i cannot get why do I have to multiply it with gl_ModelViewProjectionMatrixInverseTranspose, as I think I should have got results by multiplying it with either gl_NormalMatrix or gl_ModelViewMatrixInverseTranspose.

any ideas on it

regards

This topic is closed to new replies.

Advertisement