Multiple Lights
  • OpenGL specifies a minimum of 8 light sources (you may have more)
  •  Remember that excessive light sources can have a detrimental affect on performance

  •  

     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Controlling a Light's Position
  • Light positions are treated the same a geometric vertices
    • These values are subject to the same matrix manipulations
  •  The modelview matrix will affect the effective location a the light
  • Three techniques to cover:
    • Fixed light position
    • A moving light around a fixed object
    • A light that moves along with the viewpoint (headlight)

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Keeping the light stationary
  • To keep the light stationary set the light position after all modeling and viewing transformations 

  •  

     
     
     
     
     

    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity(); 
    if (w <= h) 
      glOrtho (-1.5, 1.5, -1.5*h/w, 1.5*h/w, -10.0, 10.0); 
    else 
      glOrtho (-1.5*w/h, 1.5*w/h, -1.5, 1.5, -10.0, 10.0); 
    glMatrixMode (GL_MODELVIEW); 
    glLoadIdentity();

    /* later in init() */ 
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 }; 
    glLightfv(GL_LIGHT0, GL_POSITION, position);
     
     

  •  Notice that the light position is defined after:
    • projection and viewport matrices are defined
    • modelview matrix is loaded with the identity matrix

     
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Independently Moving the Light Source
     
    static GLdouble spin; 

    void display(void) { 
      GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; 
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
      glPushMatrix(); 
      gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 
      glPushMatrix(); 
        glRotated(spin, 1.0, 0.0, 0.0); 
        glLightfv(GL_LIGHT0, GL_POSITION, light_position); 
      glPopMatrix(); 
      glutSolidTorus (0.275, 0.85, 8, 15); 
      glPopMatrix(); 
      glFlush(); 
    }
     
     

  •  glPushMatrix/glPopMatrix isolate the modelview matrix manipulation of the light source
  • This effectively rotates the light source position

  •  

     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Moving the Light Source Together with the Viewpoint
  • The light position must be set before the viewing transformation

  •  

     
     
     

    GLfloat light_position() = { 0.0, 0.0, 0.0, 1.0 }; 
    glViewport(0, 0, (GLint) w, (GLint) h); glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glLightfv(GL_LIGHT0, GL_POSITION, light_position); 

          ... 
     

    static GLdouble ex, ey, ez, upx, upy, upz; 
    void display(void) { 
      glClear(GL_COLOR_BUFFER_MASK | GL_DEPTH_BUFFER_MASK);
      glPushMatrix(); 
      gluLookAt (ex, ey, ez, 0.0, 0.0, 0.0, upx, upy, upz);
      glutSolidTorus (0.275, 0.85, 8, 15); 
      glPopMatrix(); 
      glFlush(); 
    }
     
     

  •  Both the light source and the viewpoint are moved to the same location
  • The effective light position is changed because the eye coordinate system has changed

  •  

     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Selecting a Lighting Model
  • An OpenGL lighting model has 3 components:
    • The global ambient light intensity
    • Whether the viewpoint position is local to the scene or infinite
    • Whether there is separate front and backside lighting
  • The lighting model is defined with glLightModel()

  •  

     
     
     

    void glLightModel{if}(GLenum pname, TYPEparam); 
    void glLightModel{if}v(GLenum pname, TYPE *param);


     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Global Ambient Light
  • Individual lights each have an ambient component
  • A global ambient light may be defined for the scene


GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 }; 
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

     
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Local of Infinate Viewpoint
  • Lighting calculations can be affected by the distance from a vertext to the viewpoint.
  • By default the viewpoint is assumed to be at infinity (all vertices are the same distance from the eyepoint)
  • Specular highlights are effected by switching to a local viewpoint:
    • glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Two-Sided Lighting
  • Lighting calcuations are performed for all polygons whether they face forward or backwards
    • Normals are reversed for back-facing
  • Two-sided lighting is enabled by:
    • glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
     
     
     
     
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Defining Material Properties
  • Object material will affect the amount an color of reflected light
  • Material properties are defined with glMaterial()

  •  

     
     
     

    void glMaterial{if}(GLenum face, GLenum pname, TYPEparam); 
    void glMaterial{if}v(GLenum face, GLenum pname, TYPE *param);

    face can be: GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK