# Lights rotate with objects

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

## Recommended Posts

Hi, I have a real problem with lighting that is probably really easy to solve, but I can't find the solution everywhere. To explain, I currently have a rendering process that is something like this (simplified a bit with some pseudo code) ...
for each render pass
{
// POSITION CAMERA

gluLookAt(view.x,       view.y,       view.z,
view.x - cdx, view.y - cdy, view.z - cdz,
0.0f,         1.0f,                       0.0f);

// POSITION LIGHT

sun_pos[0] = 0.7f;
sun_pos[1] = 0.7f;
sun_pos[2] = 0.7f;
sun_pos[3] = 0.0f;

glLightfv(GL_LIGHT0, GL_POSITION, sun_pos);

// DRAW OBJECTS

for  each object in scene
{
// POSITION OBJECT CORRECTLY

glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glRotatef(angle, 0.0f, 1.0f, 0.0f);

// DRAW OBJECT USING VERTEX ARRAYS

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glTexCoordPointer(2, GL_FLOAT, 0, texs);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, norms);

glDrawArrays(GL_TRIANGLES, 0, ver_count);

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

glPopMatrix();
}
}


Now this all works fine except for one thing ... the lighting rotates with the object so that the same faces are lit regardless of the value 'angle' in the object rotation glRotate() when what I want is for the light to remain stationary ... it is supposed to be the sun after all! Can anyone help with this as I've been trying to sort this out for ages without success. Many thanks for any advice, Jon.

##### Share on other sites
The only thing that looks wrong to me is that you gluLookAt before you glLightfv(GL_LIGHT0, GL_POSITION, sun_pos). This means that the suns position is relative to the viewers position, when presumably it should be fixed.

Enigma

##### Share on other sites
Anything you do to modify the GLMatrix, like LookAt, rotations, etc.. will effect any drawing afterwards, including positioning of the lights. This is inreality a good thing, so you don't have to calculate the positions when you want them to rotate. But when you don't, like now, position the light before any LookAts or rotations.

##### Share on other sites
Problem solved (more or less) ... in the end the algorithm I was using was okay, but the normals were being corrupted during the object loading (Y and Z values were being swapped).

Thanks for the advice and I'll try swapping the glulookat and the light set up to see if this improves things further.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633384
• Total Posts
3011598
• ### Who's Online (See full list)

There are no registered users currently online

×