Sign in to follow this  

gl_NormalMatrix in GLSL

This topic is 3737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 3737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this