GLSL lighting using normals

Started by
2 comments, last by Ashaira 12 years ago
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!
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!
You have to glEnable the light and/or set its position right after glLoadIdentity().

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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)

This topic is closed to new replies.

Advertisement