PDA

View Full Version : Spotlight - I'm so confused...after 19 hrs...



12-09-2005, 11:21 PM
I'm trying to get multiple spotlights to shine on the z and x axes, in both the positive and negative direction.

GLfloat LightPosition[]= { 0.0f, 0.0f, 0.0f, 1.0f };

GLfloat spot_direction[] = { 0.0f, 0.0f, -1.0f };

In my init() function I have:
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0f); // Set up spotlight
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 20.0f);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glEnable(GL_LIGHT0);

I tried creating the others in a similar fashion, with the spot_direction values changing accordingly. But it didn't work, hehe obviously. So to experiment why, I played around with the first spotlight, which worked. Surprisingly, I'm not messing with the MatrixMode stuff and it's following my viewer as it looks anywhere where the heading is towards the negative z-axis. But if I change the spot_direction to anything where the z parameter is not a negative value, I get complete darkness.
Another confusing thing is that the spotlight is following the viewer throughout my 3D world - even through textured walls - and I'm not including any lighting code in my draw(). I'm a complete openGL newbie, but from searching for the past 5 hrs for clues, it seems that my spotlight is doing things it shouldn't and also not corresponding correctly to changes =/. I don't know if I made any sense, but I need some help as this is for a final project of mine due in a few days. This one problem has bogged me down for the past 19 hrs I've spent on it.

dorbie
12-10-2005, 12:19 AM
A few things you should know:

1) All lights are transformed to eye space through the MODELVIEW matrix, this includes the position and orientation. If you position a light only once it will not move w.r.t. the eye until you specify the position again. If you position it each frame but before you have placed the viewing transofrmation on the MODELVIEW matrix (i.e. while you have an identity matrix on the modelview stack) you will always position the light directly in eye space ignoring any viewer positioning in your software.

2) Lighting operates on OpenGL vertices without regard to other stuff drawn. An object will not cast shadows on other objects unless you implement another higher level algorithm using things like projected textures and stencil buffers. OpenGL knows nothing about occlusion and shadow casting of light sources. The only thing OpenGL does like this is depth buffer visibility occlusion from the viewer, again not related to lighting.

3) A light positioned at 0.0, 0.0, 0.0 while the MODELVIEW matrix is set to identity will be at the eye, the only way it will iluminate anything visible is to point it down the Z axis. Moving the eye will have no effect on position or orientation unless you position the light after you move the eye (i.e. after you put the viewing transformation on the modelview stack). To position it elsewhere you can change the position in eye space or position it once the viewing matrix is placed on the MODELVIEW stack.

Points 1 and 3 above are basically saying the same thing if you didn't notice.