Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Lighting problem

  1. #1
    Member Contributor
    Join Date
    Jul 2008
    Posts
    52

    Lighting problem

    Hi people, I was just wondering how to draw a lit polygon correctly. I draw a plate in clockwise orientation and doing this I'm telling Opengl that the face I'm looking is back. This way is right because my "sun" is in the other side of the plate and all light goes well. But, if I rotate the plate, the back side should be lit, but because it is the backside, even in front of the "sun" it is not enlightened. In addition, the side that doesn't face the sun, the front one, is enlightened.

    My question is, what is the secret to do Opengl control this automatically, or, how can I draw a polygon in such a manner that the lighting can lit the faces correctly? In real life it doesn't matter what side is the front or back one, what matter is that if a side faces light, it will be enlightened. How to do it?

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2006
    Posts
    20

    Re: Lighting problem

    Lighting is governed by normals, do you supply them for your polygon? (glNormal3f)

  3. #3
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Lighting problem

    Can you put some screen shot of your plate with normals visible? For my part, it is clearly not understandable.

    All normals should point out of the objet if you want that its surface is correctly lit.

  4. #4
    Member Contributor
    Join Date
    Jul 2008
    Posts
    52

    Re: Lighting problem

    I supply the normals. But I can't see at where it points. All I did was:

    Code :
     
    GLfloat v1[] = {0,0,0};
    GLfloat v2[] = {3,0,0};
    GLfloat v3[] = {3,3,0};
    GLfloat v4[] = {0,3,0};
     
    glBegin(GL_POLYGON);
     
    //counterclockwise
     
    glNormal3f(v1);
    glVertex3f(v1);
     
    glNormal3f(v2);
    glVertex3f(v2);
     
    glNormal3f(v3);
    glVertex3f(v3);
     
    glNormal3f(v4);
    glVertex3f(v4);
     
    glEnd();

    Obs: it is in a display list.

    The light source is at (0,30,-100) and the cam is at (0,0,5). In that way it is correct. But, if I rotate the object 180 degrees, the other side, the one who was hidden from the light, now faces the light, right? But, instead of being lit, it stays unlit.





  5. #5
    Junior Member Newbie
    Join Date
    Nov 2006
    Posts
    20

    Re: Lighting problem

    GLfloat v1[] = {0,0,0};
    glNormal3f(v1);
    glVertex3f(v1);
    See this? Normal is vector with length of 1.0, not zero, not 3. Supply proper normals (0, 0, 1).

  6. #6
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Lighting problem

    Your quad is in the XY plane so normals should be by definition orthogonal to this plane and point in the Z or -Z direction in your case.

  7. #7
    Member Contributor
    Join Date
    Jul 2008
    Posts
    52

    Re: Lighting problem

    hm.. the normal should be defined in a point different from the vertex? I though the function glnormal calculates automatically this. So, in my example above, what is the correct normal coordinates I should pass?

    and if a point doesn't lie only in XY plane, but in the Z too?

    thanks

  8. #8
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Lighting problem

    I think you need to read about 3D rendering basics. Without wanting to be offensive, you are trying to draw some 3D stuff without knowing what you really do.

    A normal is a vector orthogonal to a polygon and be should normalized for lighting computation. So if you had read the glNormal spec, you would have seen that you need to supply a normal vector which is independant of vertices position.

  9. #9
    Member Contributor
    Join Date
    Jul 2008
    Posts
    52

    Re: Lighting problem

    No, I know about 3d basics, I just don't know how the function glnormal actually works. I know what is a normal vector, I know how to find it manually (cross product betwenn 2 vectors in a plane). Please, correct my example above, giving me the proper coordinates to normal in that case so I can figure out by myself how I have to proceed with glnormal.

    By the way, I normalize automatically all vertices by enabling GL_NORMALIZE.

  10. #10
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Lighting problem

    Yes you can normalize normals with glEnable(GL_NORMALIZE) but that does not work for normals with a length equal to zero.

    glNormal is very easy to use, you have 3 parameters that are the normal coordinates. In you example, normals are all eaqual to (0,0,1).
    So you set normals for each vertex like this:

    glNormal3f(0,0,1);

    this is what you would have found calculating the cross product... as you said.

    http://www.opengl.org/sdk/docs/man/xhtml/glNormal.xml


    By the way, your glNormal3f and glVertex3f calls are wrong, you should call glNormal3fv and glVertex3fv since you set parameters as arrays.

Page 1 of 2 12 LastLast

Similar Threads

  1. Lighting problem
    By stefkos in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-09-2012, 11:36 AM
  2. Lighting problem,
    By dreamspaced in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-18-2005, 11:46 PM
  3. Replies: 4
    Last Post: 09-27-2004, 06:27 AM
  4. Odd problem with lighting...
    By Baltor in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-12-2004, 03:49 PM
  5. Lighting problem (opengl + shader lighting)
    By ox1d_47 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-19-2004, 09:43 PM

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