PDA

View Full Version : Light Problem



Pody
07-18-2002, 05:22 AM
In my program i load a DXF model, create a directional light and a camera, and move/rotate/translate/scale each of them.
My problem is that the objects are positioned well, but are lit as if they still were in the origin, and not relative to their real position.

This is the code of the rendeing part:

el=list;
while (el)
{
if (!el->specialFlag)
{
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_NORMALIZE);
glLoadMatrixf(el->matrix);
glNormalPointer(GL_FLOAT,0,el->normalArray);
glVertexPointer(3,GL_FLOAT,0,el->vertArray);
glDrawArrays(el->type,0,(GLsizei)el->vertArrayCur/3);
}
el=el->next;
}

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glLoadIdentity();
el=list;
while (el)
{
if (el->specialFlag==POINTLIGHT)
{
for (i=0;i<4;i++)
lightPos[i]=el->lightPos[i];
multMatrVect(el->matrix,lightPos);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
}
el=el->next;
}

(el->matrix holds the tranform matrix associated with that element;
el->specialFlag is 0 for objects, and !=0 for camera and light)

Pody
07-18-2002, 05:49 AM
mmm, I have the same problem if I move the object and not the light: the illumination doesn't change

Michael Steinberg
07-18-2002, 08:41 AM
Remember that light positions are transformed with the MODELVIEW matrix exactly like the rest of geometry. You may need to push/pop the matrix stack for the lights in order to not transform them with the geometry.

Pody
07-19-2002, 12:02 AM
The problem is when i move the object: the light is not moved, but the object is lit as if it was still in the origin.
I tried with some glPushMAtrix and glPopMatrix, but it didn't help http://www.opengl.org/discussion_boards/ubb/frown.gif

Michael Steinberg
07-19-2002, 02:46 AM
try this:

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

// put the light definitions here

glPopMatrix();

Pody
07-19-2002, 02:49 AM
It doesn't work. http://www.opengl.org/discussion_boards/ubb/frown.gif
it seems to me that openGL applies the lighting to the object centered in the origin, and then places it in the real position.

Deiussum
07-19-2002, 04:28 AM
Are you by chance using a directional light?

You don't show what values you are using for GL_POSITION, but if you are setting the 4th number in the array to 0 (or worse yet, only have 3 components in the array) you will have a directional light, not a positional light.

Example:

float lightpos1[4] = {0.0, 0.0, 1.0, 0.0}; // directional
float lightpos2[4] = {0.0, 0.0, 1.0, 1.0}; // positional

Pody
07-19-2002, 09:38 AM
YESSSSSS!!!!
THANK YOU SO MUCH!!!!!
I'M SOOOO DUMB!!!!
I tried doing a thousand changes except that fourth parameter which i left to 0.0!
I didn't understand well the difference between a spot and a directional light http://www.opengl.org/discussion_boards/ubb/smile.gif
THANK YOU SO MUCH!

Michael Steinberg
07-19-2002, 02:00 PM
Ahhhhhhh, it's always nice to see new error-symptoms http://www.opengl.org/discussion_boards/ubb/smile.gif nice it's solved now, and sorry for my stupidity.