Sign in to follow this  
RenegadeKoolAid

Lighting + Camera problems

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
see if you could have the light move in the oppisite direction of the camera when the camera is moved. that might work

Share this post


Link to post
Share on other sites
Hamster    247
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.

Share this post


Link to post
Share on other sites
Wingman    167
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
NoahAdler    290
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hamster    247
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?

Share this post


Link to post
Share on other sites
Weird, my problem was actually these lines:


glEnable( GL_COLOR_MATERIAL );
glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
glMaterialfv( GL_FRONT, GL_SPECULAR, matAmbient );


When I took those out it worked fine.

Thanks for the help guys

Share this post


Link to post
Share on other sites
Wingman    167
Hamster: You're right. Everything actually depends on how you draw - sorry for being unclear. Check OpenGL FAQ for details:

http://www.opengl.org/resources/faq/technical/viewing.htm (first three questions)

http://www.opengl.org/resources/faq/technical/lights.htm (first question, last paragraph and 18.050)

Share this post


Link to post
Share on other sites

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