Light positioning question

I am making a program in which a pacman sphere and a bunch of smaller spheres move about on the x-y plane, and you look down the z-axis at them. I want to be able to move a light source by dragging the mouse. I can find the mouse position on the x-y plane just fine, and I can move the light along with it, but I’m having issues with the light’s z-coordinate. I’m also having problems with each of my objects being lit from the same direction, even though they are scattered about. I would like to keep the light at 0.0, and have it light all the objects that are moved down the z axis to a farther plane.

Here is psuedo-code to show how I’m currently trying it:

In init():
zoomOut = -20;
light_position = {0.0, 0.0, 0.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

In draw():

light_position[0] = glMouseX;
light_position[1] = glMouseY;
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glTranslatef(0.0, 0.0, zoomOut);

for pacman and for each sphere, i do glPushMatrix(), transform on x and y, and draw the proper model, then glPopMatrix.

What currently happens is that everything is lit directly from side, as if the light is at the same depth that they are. Also, no matter where they are on the screen, when i draw the mouse around the center of the screen, everything is lit as if they were at the center of the screen.

Here is a link to the full source (not including the header)

Here is a link to the executable.
Use the arrow keys to move Pacman around. Use the spacebar to turn on and off Pacman’s gravity.

Thanks in advance for help!

You’re doing:

If the w component of the position is 0.0, the light is treated as a directional source. Diffuse and specular lighting calculations take the light’s direction, but not its actual position, into account, and attenuation is disabled. Otherwise, diffuse and specular lighting calculations are based on the actual location of the light in eye coordinates, and attenuation is enabled
(From the 1.0 reference manual)

In other words, when you have w at 0, the light is treated as infinitely far away.
Try it with


Thanks! That fixed my problem. Guess I should have read just a bit more…