PDA

View Full Version : Spot Light / Specular Lighting seems incorrect



Seth Berggren
05-24-2011, 09:00 PM
Hi, I am programming OpenGL with Python.

Basically I have a spot light pointing at a wall. The direction of the spotlight, the direction of the camera, and the direction of the normal on the wall are collinear.

As I move the camera away from the wall, the light (originally in the middle of the wall), moves upward and to the right.

I attached 3 pictures of what my camera looks like at various distances from the wall, with the camera coordinates displayed.

Why doesn't the light stay in the middle of the wall as I move away from it?


Here is my lighting function:
(I can show the rest of my code if needed)

def lighting():
glShadeModel(GL_SMOOTH)
glEnable(GL_LIGHTING)

glLightfv(GL_LIGHT1, GL_POSITION, [5.0,0.0,0.0,1.0])
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,[-1.0,0.0,0.0])
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 60.0)
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 128.0)
glLightfv(GL_LIGHT1,GL_SPECULAR,[0.0,0.0,1.0,1.0])
glEnable(GL_LIGHT1)

glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,[1.0,1.0,1.0,1.0])

glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0)

glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

glEnable(GL_COLOR_MATERIAL)

Alfonse Reinheart
05-24-2011, 09:15 PM
The light position is multiplied with the modelview matrix at the moment glLightf is called with GL_LIGHT#_POSITION. If you are passing world-space lights, then the modelview matrix at that point needs to have a world-to-eye/camera/etc matrix on it.

Seth Berggren
05-25-2011, 10:45 AM
Awesome, I fixed the problem by putting the position and direction functions in my display/render function.

Now the position and direction of the light are reset every frame. Before they were only set once at the beginning of the program.

Thanks