Lighting + Camera problems

Started by
11 comments, last by Hamster 19 years, 9 months ago
Im trying to put lighting into my terrain editor, but of course I have the problem of the light moving when the camera does. I need it to stay stationary. I searched on this topic and read every single thread on it, but still havent figured it out. Heres some code: In my editor, there is a menu that the user can use to load a heightmap. After the heightmap is loaded I am initializing lighting at a spot above the heightmap, and have it pointing straight down.

if( GetOpenFileName( &file ) )
{
  //create the terrain, enable menu options, etc
			

        glPushMatrix( );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity( );

        glEnable( GL_LIGHTING );
			
	GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat lightDir[] = { 0.0f, -1.0f, -1.0f };
	GLfloat matAmbient[] = { 1.0f, 1.0f, 1.0f, 1.0f };

	glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
	glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight );
						
	glLightfv( GL_LIGHT0, GL_POSITION, g_lightPos2 );

	glEnable( GL_LIGHT0 );
		
	glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 40.0f );
	glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 30.0f );
	glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, lightDir );

	glEnable( GL_COLOR_MATERIAL );
	glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
	glMaterialfv( GL_FRONT, GL_SPECULAR, matAmbient );
			
	glPopMatrix( );
}
g_lightPos2 is global for now, this is all pretty unorganized right now. I just wanted to get it working then clean it up. In my render function I have:


GLfloat g_lightPos2[]      = { 125.0f, 300.0f, -125.0f, 1.0f };

GLvoid renderGame( )    			                 	
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	
  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity( );
	
  //update camera
  g_camera->update( g_shiftPressed );

  glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
	
  if( g_terrainLoaded )
  {
	glPushMatrix( );
	glLoadIdentity( );
	glLightfv( GL_LIGHT0, GL_POSITION, g_lightPos2 );
	glPopMatrix( );
		
	g_terrain->displayTerrain( g_mode, g_detailTexture );

        //other stuff
  }
}
And still, the light will move with the camera. Anyone have any ideas? Thanks!
Advertisement
see if you could have the light move in the oppisite direction of the camera when the camera is moved. that might work
Hrmm.. I don't really see the need for the

glPushMatrix();
glLoadIdentity();
...
glPopMatrix();

surrounding the setting of the light position. You could try getting rid of them.
You over-complicated it. :)
In your render function, call:
glPushMatrix ();		glLightfv (GL_LIGHT0, GL_POSITION, light0_pos);glPopMatrix ();

Be sure to push/pop *modelview* matrix. This will make your light position immune to camera movement (I assume you're using gluLookAt, which affects modelview matrix, that's why your light moves).
DO NOT reset your modelview matrix to identity here. You lose all your current settings this way.

Another option is to set your light position in a display list -it won't be affected by modelview matrix changes becasue it will be called only once (with glCallList()) and won't be updated every frame (so there is no need to push/pop matrix):
glNewList (list, GL_COMPILE);		glLightfv (GL_LIGHT0, GL_POSITION, light0_pos);glEndList ();



Also, your code SHOULD be changed here:
glPushMatrix( );glMatrixMode( GL_MODELVIEW );glLoadIdentity( );...glPopMatrix ();


It should be:
glMatrixMode( GL_MODELVIEW );glLoadIdentity( );glPushMatrix( );...glPopMatrix ();

so that you push/pop MODELVIEW matrix for sure (you set your matrix state to modelview matrix first).
Im sorry I should have said I pretty much tried every variation of what I have in my code there (I was short on time when posting :( ).

When I change to what Wingman suggests, it will not move with the camera, but the light will rotate with the camera. I have wasd keys for forward, backward and strafes, and mouse for looking around. movement works fine but the light will rotate with the camera when I use the mouse.

The display list is a very good idea, thank you, I will try that right now.
You need to set the light position after setting your camera view. Yes, this means you need to set the light parameters every frame. Weird, I know, but that's the way this particular API works. Hope this helps.
-bodisiw
Quote:You need to set the light position after setting your camera view. Yes, this means you need to set the light parameters every frame. Weird, I know, but that's the way this particular API works. Hope this helps.


Ok, do you mean EVERY light parameter, or are you talking just position. From the threads I have searched on I have only found that people needed to reset the position after the camera update, not every parameter. Right now I am resetting the light position every frame after camera update, but not every parameter.
You'll need to set GL_SPOT_DIRECTION again.
Oh yes, I overlooked that you also have a spot direction. You should push/pop matrix for it as well.
Wingman: just wanted to ask you this for my own benefit, as you seem to know what you're talking about. Why push and pop the matrix? I thought the light position and direction were transformed by the inverse of the current modelview at the time they were set, and so I don't really see why this would require pushing and popping. Is there some other reason for doing this, or have I got things wrong?

This topic is closed to new replies.

Advertisement