Sign in to follow this  

Lights rotate with objects

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

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 

	glLoadIdentity();				
	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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 4591 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this