Jump to content
  • Advertisement
Sign in to follow this  
Phynix

GLSL lighting using normals

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

So, I've been trying to add very simple normal lighting..if a face is facing a light, then it's illuminated. otherwise, it progressively gets dimmer, and when it's behind the face, then the face isn't illuminated at all. Now, this works perfectly when I have a cube set up at the origin.

However, when I started to rotate the cube, the light seemed static in the cube--for example, a light spot on the cube would stay in the same place, even while the cube is rotating. I found the problem to be that the normals were not being rotated with the object.

So, do you guys have any idea how to do this?

I tried normal=normalize(gl_NormalMatrix*gl_Normal);

but the problem is that the normal is translated into camera coordinates (with the camera being (0,0,0); so a face would always have a normal of (0,0,1) if the camera was looking at it). So, my question is...how do I rotate an object, and have the normals rotated as well?

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Hi!

You could carry out the lighting calculations in view space by transforming your light vector to view space, too (by using the gl_ModelViewMatrix).
If you transform direction vectors, e.g. the vector to the light and the normal, don’t forget to set the w-component to zero.

Cheers!

Share this post


Link to post
Share on other sites
Im fairly new to this as well but when i was messing around with lighting in opengl es everything was messed up because i was doing calculations for the normals in diff space. so i made sure that everything was in the same space (i used worldspace for mine). try to go through ur shader and make sure ur calculations are done in the same space and make sure u normalize in the vertex shader AND in the fragment shader.

hope it helps.

EDIT: upon further consideration i think u may be rotating the light together with the cube which is why the light stays on the same face (used to happen to me all the time as i kept messing up the push pop matrix order)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!