Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


gl_NormalMatrix in GLSL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 me_here_me   Members   -  Reputation: 102

Like
0Likes
Like

Posted 18 September 2007 - 02:19 AM

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

Sponsor:

#2 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 18 September 2007 - 09:30 PM

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.

#3 3dmaniac   Members   -  Reputation: 124

Like
0Likes
Like

Posted 18 September 2007 - 09:51 PM

if you want the side in front of camera to be always blue try this
gl_FragColor.rgb = gl_NormalMatrix * norm;

#4 me_here_me   Members   -  Reputation: 102

Like
0Likes
Like

Posted 18 September 2007 - 10:41 PM

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 :(



#5 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 18 September 2007 - 11:04 PM

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?

#6 me_here_me   Members   -  Reputation: 102

Like
0Likes
Like

Posted 19 September 2007 - 03:08 AM

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 :)

#7 me_here_me   Members   -  Reputation: 102

Like
0Likes
Like

Posted 20 September 2007 - 02:16 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS