PDA

View Full Version : Light always coming from viewpoint.



paros
12-07-2008, 11:52 PM
I've read numerous tuts and followed them to the T. However, no matter what I do with my code, when I run the program it always has a lightsource coming directly from the location of my viewpoint. My program has full-blown walk-through capability. But no matter where I move the camera, this stupid lightsource keeps shining directly at all the objects from my vantage point. This is driving me absolutely crazy. I have written and re-written my code a dozen times and still nothing seems to change. Do I need to reposition the lightsource every single frame? Do I have these glEnable() funcs called in the wrong order? What is the deal? Someone please help me! :(

Here is the initializing function for my opengl.



void COPENGL::InitializeOGL( float lx,
float ly,
float lz,
float * BGC)
{

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_POINT_SMOOTH);
glEnable (GL_LINE_SMOOTH);

glShadeModel (GL_FLAT);
glEnable (GL_CULL_FACE);
glCullFace (GL_BACK);

glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glEnable (GL_NORMALIZE);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);



background[0]=BGC[0];
background[1]=BGC[1];
background[2]=BGC[2];
background[3]=1.0f;

LightPos[0] = lx;
LightPos[1] = ly;
LightPos[2] = lz;
LightPos[3] = 1.0f;

glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);

glClearColor( BGC[0], BGC[1], BGC[2], BGC[3] );
glColorMask(1, 1, 1, 1);
glDepthMask(~0);
}




For those of you who are curious to know what these lightsource values are (even though this shouldn't matter at all) here they are:



COPENGL::COPENGL()
{
/*
Load defaults here.
*/
AMBIENTC = 0.3;
MatShin = 0.0f;

GlobalAmbient[0]= 0.25f;
GlobalAmbient[1]= 0.25f;
GlobalAmbient[2]= 0.25f;
GlobalAmbient[3]= 1.0f;


LightAmbient[0]= 0.0f;
LightAmbient[1]= 0.0f;
LightAmbient[2]= 0.0f;
LightAmbient[3]= 1.0f;


LightSpecular[0]= 1.0f;
LightSpecular[1]= 1.0f;
LightSpecular[2]= 1.0f;
LightSpecular[3]= 1.0f;


LightDiffuse[0]= 1.0f;
LightDiffuse[1]= 1.0f;
LightDiffuse[2]= 1.0f;
LightDiffuse[3]= 1.0f;

LightPos[0]= 0.0f;
LightPos[1]= 30.0f;
LightPos[2]= -3.0f;
LightPos[3]= 0.0f;


MatAmb[0]=0.3f;
MatAmb[1]=0.3f;
MatAmb[2]=0.3f;
MatAmb[3]=1.0f;

MatEmi[0]=0.0f;
MatEmi[1]=0.0f;
MatEmi[2]=0.0f;
MatEmi[3]=1.0f;

MatSpec[0]=0.2f;
MatSpec[1]=0.2f;
MatSpec[2]=0.2f;
MatSpec[3]=0.2f;

background[0]=0.0;
background[1]=0.0;
background[2]=0.0;
background[3]=1.0f;
}

paros
12-08-2008, 12:53 AM
Sorry to respond to my own message here.
I think I figured out the problem.
Some peculiarity of openGL requires that you perform these steps in order on each frame of animation.

1) Set the light position using glLightfv(GL_LIGHT0,GL_POSITION, p);
2) Set the camera position using gluLookAt()
3) Set the material for the surface.
4) Draw the object.

If you start moving these out of order, you will get things like the light sources being "latched" to the camera position, as if they are given in relative coordinates instead of absolute coordinates.

Komat
12-08-2008, 01:55 AM
If you start moving these out of order, you will get things like the light sources being "latched" to the camera position, as if they are given in relative coordinates instead of absolute coordinates.

That is because they are not absolute. The light position is transformed by modelview matrix which was set when the positioning glLightfv() call was made and the resulting eye space coordinates are stored and used for lighting.