# GLSL normals problem?

This topic is 2331 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 have to rotate the normal like:
[source lang="cpp"]mat4 rotate_x(float theta)
{
return mat4(
1.0, 0.0, 0.0, 0.0,
0.0, cos(theta), sin(theta), 0.0,
0.0, -sin(theta), cos(theta), 0.0,
0.0, 0.0, 0.0, 1.0
);
}

void main()
{
....
....

mat4 tRotation = rotate_x(originalRotation.x);
vec4 newTranslation = vec4(tRotation * PositionFromAttributes);
vec3 eyeNormal = NormalMatrix3x3 * vec3(newTranslation) * NormalFromAttributes;
....
....
}[/source]

##### Share on other sites
The issue is that there are three common matrices: the model, the view, and the projection. The model matrix transforms the object from object space into world space. The view matrix transforms from world space into eye space (everything relative to camera). The projection matrix transforms from eye space to clip space (everything perspective correct). OpenGL 2 and before combine the model and view matrices into the modelview matrix.

Multiplying by gl_NormalMatrix as in your original post is correct (gl_NormalMatrix is only the rotational part of the model matrix).

But remember, because OpenGL combines the model and view matrices into the modelview matrix, when you multiplied by gl_NormalMatrix, you also incidentally multiplied by the view matrix! This is why the view screwed up the normals. The classic solution? Transform the light too!

To do this, put the light position AFTER your camera call. Something like this pseudo-C-like language:[source lang="c"]//Begin frame

//Set camera
gluLookAt(/*stuff*/);
//Set light
glLightfv(GL_LIGHTn,GL_POSITION,/*something*/);

//...

//Flip buffers
//...

//End frame[/source]-G

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633755
• Total Posts
3013706
×