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!