Search code examples
openglrotationlight

OpenGL light with object rotation


I am trying to rotate dodecahedron with simple light.

The code in the display function is this:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5f);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.75f);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.4);
GLfloat black[] = { 0, 0, 0, 1 };
GLfloat blue[] = { 0, 1, 1, 1 };
glLightfv(GL_LIGHT1, GL_AMBIENT, black);
glLightfv(GL_LIGHT1, GL_DIFFUSE, blue);
glLightfv(GL_LIGHT1, GL_SPECULAR, blue);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);

glTranslatef(0, 0, -50);
glRotatef(angle, 0, 1.0, 0);
glutSolidDodecahedron();

The angle change by the arrow keys. But for some angles, the picture look odd like this:

enter image description here

and after more rotations, it looks like this: enter image description here

It seems the faces that was at the back are transparent. Why is that? How can I make this object to be printed correctly?


Solution

  • edit: The solution is to enable back-face culling via glEnable(GL_CULL_FACE). You may want to verify that your object's polygons are defined with the correct winding order and ensure back-face culling is set (glCullFace(GL_BACK_FACE)) if you don't intend to see inside the object.


    Original answer:

    The light is not touching the opposite side of the object and your ambient lighting is set to black:

     GLfloat black[] = { 0, 0, 0, 1 };
     glLightfv(GL_LIGHT1, GL_AMBIENT, black);
    

    Try setting your ambient light to a dark grey color to roughly approximate the ambient light contribution of global illumination:

     GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
     glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);