# GLSL lighting using normals

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

## 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?

##### Share on other sites
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 on other sites
You have to glEnable the light and/or set its position right after glLoadIdentity().

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

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009398
• ### Who's Online (See full list)

There are no registered users currently online

×