Jump to content
  • Advertisement
Sign in to follow this  
AdamVanner

Ab-Normals!!

This topic is 3170 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

I'm going through all my code and replacing the fixed function matrix functions (glPushMatrix/gl_ModelViewMatrix, gl_Normal/glNormalMatrix) with user uniforms as per the GL4.0 specs and am coming across what appears to be an anomaly when I transform the Normals as vectors in the VP on a ATI 5870 card using gl 1.5. The odd thing is that even when I use the IT of the MVP I'm not getting the same result as gl_NormalMatrix and the normals are incorrect, even though I'm pretty sure I'm transforming them correctly. EG:
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

  vec4 vv = transpose(inverse(gl_ModelViewProjectionMatrix))  * vec4(gl_Normal,0.0);

  if( gl_Vertex.y > 0 ) // just to highlight differences as both cases should be identical
  _CurShader.N=normalize(gl_NormalMatrix  * gl_Normal); // works
  else
  _CurShader.N=normalize(vv.xyz); // doesn't :(
(btw, of course in my eventual code I wont use MVP and certainly wont invert matrices in the VP! But this gives the same result) so, here, just as a test, I have the normals where y>0 set to the old method that works fine, but the IT of the MVP doesnt xform the normals correctly,.. even though it should, right? I've experimented with about a trillion combinations (dividing by w, transposing/not,inverting/not, to 3x3/rotational 3x3, inv. scaling etc etc). Am I not seeing the wood for the trees here? Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
OK,.. despite spending hours on this, have fixed it in 2 mins by extracting the top left 3x3.


vec3 vv = mat3((transpose(inverse(gl_ModelViewMatrix)))) * gl_Normal;


So, use the IT of the ModelViewMatrix rather than the gl_ModelViewProjectionMatrix?!?...

How, then can normals obey perspective projection?? very confused!


Share this post


Link to post
Share on other sites
Quote:
Original post by AdamVanner
How, then can normals obey perspective projection?? very confused!
Normals are used for your lighting calculations, which are usually calculated in world-space or view-space (not in post-projection-space). So the projection should be irrelevant to them.

Share this post


Link to post
Share on other sites
Ah yes, of course, thanks!
I did say "Am I not seeing the wood for the trees here?"!

I should have remembered that. Its amazing how code you wrote 5 years ago can appear so alien!

thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!