Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Two sided lighting won't work

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2010
    Posts
    3

    Two sided lighting won't work

    I drew this truncated isocohedron where only pentagons are faces, everything else is just wireframe. Now I want these polygons to be lit from both sides by different light sources. This is what I've done:
    Code :
      GL11.glLightModeli (GL11.GL_LIGHT_MODEL_TWO_SIDE, 17);
      FloatBuffer position1 = BufferUtils.createFloatBuffer (4).put (new float[] {0, 0, 0, 1});
      position1.flip ();
      GL11.glLight (GL11.GL_LIGHT0, GL11.GL_POSITION, position1);
      GL11.glColorMaterial (GL11.GL_FRONT_AND_BACK, GL11.GL_DIFFUSE);
      GL11.glColor3d (0, 1, 1);
      normale = normale (2 + fi, 2 * fi, 1,
                         2, 1 + 2 * fi, fi,
                         1, 3 * fi, 0);
      GL11.glNormal3d (normale[0], normale[1], normale[2]);
      GL11.glBegin (GL11.GL_POLYGON);
        GL11.glVertex3d (1, 3 * fi, 0);
        GL11.glVertex3d (2, 1 + 2 * fi, fi);
        GL11.glVertex3d (2 + fi, 2 * fi, 1);
        GL11.glVertex3d (2 + fi, 2 * fi, -1);
        GL11.glVertex3d (2, 1 + 2 * fi, -fi);
      GL11.glEnd ();
    By the way, it is in Java I only show how I draw a single polygon of my football, the lighting source is at the center of it. Every one of two polygons that are on opposite sides are lit incorrectly. For instance the mirror image of this polygon on X axis is lit how it should be. This one, on the other hand, has his outer side visible, not inner. I know the order of vertices is different in my program, that's why I specify normal vector for each polygon. I checked, these vectors are calculated correctly.

    What am I doing wrong?

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421

    Re: Two sided lighting won't work

    The thing that stands out is the 17 in
    GL11.glLightModeli (GL11.GL_LIGHT_MODEL_TWO_SIDE, 17);
    I know that the specification says if this is 0, 2 sided lighting is disabled and for any other value it is enabled, but try to give it 1 or TRUE.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2010
    Posts
    3

    Re: Two sided lighting won't work

    Tried it, didn't work too... I noticed, that the problem is solved if I point the normal vector the other way or change the order vertices are listed. Yet, I don't get why... If I choose to use two side lighting model and set same material properties for both sides what difference does a normal vector direction make?.. Why does it matter which is the front face? And why changing the order of how vertices are listed makes a difference if the normal vector is set?

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2010
    Posts
    3

    Re: Two sided lighting won't work

    Astounding discovery - Z axis is pointing away from me, not towards me. And I even have a book that says otherwise. Since my model is symmetric I didn't notice it earlier. This explains why my normals where pointing the wrong direction, but why it is enough to change the order of the way vertices are listed? I always thought it only determines normal vector direction when I don't specify it myself...

  5. #5
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421

    Re: Two sided lighting won't work

    GL uses the polygon orientation to know if the polygon is front facing or back facing with respect to the screen.
    If it is back facing, it flips the normal and does the lighting calculations.
    Your normals must be in the right direction.

    Also, you seem to be under the impression that GL computes normals if you don't specify it yourself. It does not. It uses the last normal it has in memory.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posting Permissions

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