Advertisement

Lights rotate with objects

Started by May 16, 2005 06:05 AM
2 comments, last by Jon Alma 19 years, 4 months ago
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.
Jon.
_______________________________________
Legends from the Lost Realms
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
Advertisement
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.


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.
Jon.
_______________________________________
Legends from the Lost Realms

This topic is closed to new replies.

Advertisement