Results 1 to 3 of 3

Thread: [RESOLVED] Problem with Lights and Normals in OpenGL

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Resolved [RESOLVED] Problem with Lights and Normals in OpenGL

    Hi,

    I want to create an openGL program where a fixed light source shines on a rotating cube. The problem is that the cube is not light up correctly.
    Different sides of the cube seem to light up randomly as the cube rotates instead.

    The cube is placed at position x,y = 1.0 and z = -10.0 using glTranslate. Then it is rotated 45 degress on each axis using glRotate.

    The light source is placed at position 1.0,1.0,-5.0. So that it shines right onto the cube. White is used for ambient light while red is used for diffuse light.
    glLightfv(GL_LIGHT0,GL_POSITION,lightDir); is used to position the light.
    glLightfv(GL_LIGHT0,GL_AMBIENT,white); is used for ambient light.

    Then i enabled the light source using glEnable.
    glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);

    For the normals I used a normal which is perpendicular to each side of the cube.

    For the front and back face,the normal is along the z axis.
    glNormal3f(0.0,0.0,1.0); for front face and glNormal3f(0.0f,0.0f,1.0f); for back face.

    For the left and right face, the normal is along the x axis.
    glNormal3f(1.0f,0.0f,0.0f); for left and glNormal3f(-1.0f,0.0f,0.0f); for the right.

    For the top and bottom face, the normal is along the y axis.
    glNormal3f(0.0f,1.0f,0.0f); for the top and glNormal3f(0.0f,-1.0f,0.0f); for the bottom.

    Is it the normals which is giving the problem? The rotating cube appears correctly but the lighting isnt applied correctly.

  2. #2
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Problem with Lights and Normals in OpenGL

    Your normals look correct at first sight, however ...

    Code:
    glNormal3f(0.0,0.0,1.0); for front face and glNormal3f(0.0f,0.0f,1.0f); for back face.
    They aren't supposed to point in the same direction of course. But I guess that's just a typing mistake here in the forum...

    Code:
    glNormal3f(1.0f,0.0f,0.0f); for left and glNormal3f(-1.0f,0.0f,0.0f); for the right.
    Here, shouldn't it be the other way round?

    If they light up randomly that should be indeed because you took a negative instead of a positive normal. So maybe it's a mistake like this after all.

    If you continue having problems, let us know. ;-) ...
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    108

    Re: Problem with Lights and Normals in OpenGL

    Hi,

    I've made the changes and yes, the mistake lies with the normals having the wrong sign. THanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width