PDA

View Full Version : Lighting & Transformations



03-19-2001, 07:15 AM
Hi all..
Ok. Here's my problem:
I'm doing sort of a game whice includes space flight, asteroids, starships, space station etc... Anyway, I'm got my asteroids and my camera (pseudo code soon to come) and I'm trying to add a directional light but it rotates according to the asteroids which is quite annoying... http://www.opengl.org/discussion_boards/ubb/wink.gif What I want is to have the light at a fixed direction and the rest - rotating (btw: the camera can also move...).

so here's the code (of the drawing routine);

glLoadIdentity();
glRotate(camera rotation (x,y,z));
glTranslate(-camera position);
glLightfv(LightPosition) //which is 1,0,0,0
for(i=0 i<number_of_asteroids i++)
{
glPushMaterix();
glTranslate(asteroid position);
glRotate(asteroid rotation (x,y,z));
DrawAsteroid();
glPopMatrix();
}

thanks in advance
Shahar.

Relic
03-19-2001, 07:48 AM
The light's position is transfomed by the modelview matrix like a vertex.
Do this and the light position is fixed:
glLoadIdentity();
glLightfv(LightPosition);
glRotate(camera rotation (x,y,z));
glTranslate(-camera position);

03-20-2001, 01:12 AM
I've tried it already.. It simply won't work http://www.opengl.org/discussion_boards/ubb/mad.gif The asteroids rotate and the light also rotates with them instead of staying fixed. Probably because the light is multiplied by the Matrix when the actual drawing occurs ???? (a wild theory...)
Still I'm stuck.
Thanks
Shahar

Relic
03-20-2001, 03:36 AM
Take little steps and try it with a simple working example (e.g. from the redbook samples) to figure it out.
If that's working on your implementation, than do it the same way in your program and all is well.

If you don't be that patient, just check all places where you put the light position into your program and fix the matrix at that point to contain the identity. If that's not making the light completely static, than something else is broken in your program.