Results 1 to 3 of 3

Thread: Light positioning question

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2005
    Location
    Rochester, NY
    Posts
    5

    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);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    In draw():

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

    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.
    Controls:
    Use the arrow keys to move Pacman around. Use the spacebar to turn on and off Pacman's gravity.

    Thanks in advance for help!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    314

    Re: Light positioning question

    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
    Code :
     light_position[3]=1.0;

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2005
    Location
    Rochester, NY
    Posts
    5

    Re: Light positioning question

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

Similar Threads

  1. Problem with positioning of light.
    By Paras_wolf in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-30-2011, 03:09 AM
  2. Problem with positioning of light.
    By Paras_wolf in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-29-2011, 01:04 AM
  3. question about light
    By suhu in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-08-2002, 03:37 PM
  4. A question about light
    By Zw in forum OpenGL: Advanced Coding
    Replies: 21
    Last Post: 01-16-2002, 10:13 AM
  5. Light Positioning
    By MrShoe in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-27-2001, 02:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean