Rendering  Illuminated Objects
  • To render lit surfaces OpenGL must:
    • Define normal vectors for each vertex of all the objects 
    • Create, select, and position one or more light sources. 
    • Create and select a lighting model
    • Define material properties for the objects


     (walk through code)
     
     

  • Programming Notes:
    • Make sure each light is enabled after defining
    • Make sure lighting is enabled
    • Changes in lighting may be utilized with display lists for potential performance inprovements.

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Creating light souces
     
void glLight{if}(GLenum light, GLenum pname, TYPE param); 
void glLight{if}v(GLenum light, GLenum pname, TYPE *param); 
    Creates the light specified by light, which can be GL_LIGHT0, GL_LIGHT1, ... , or GL_LIGHT7.
     
     
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Lighting Parameters
     

    NOTE!  Default values of GL_DIFFUSE and GL_SPECULAR apply only to GL_LIGHT0
    (0.0, 0.0, 0.0, 1.0) for the other lights
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Specifying the position and color of a light 
     
    GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; 
    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; 
    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; 
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); 
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    Note: Remember to turn on each light with glEnable(). (See "Enabling Lighting" for more information about how to do this.)
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Color
  • OpenGL allows you to associate color with three different paramters:
  • GL_DIFFUSE color correlates to the "typical" color of a light
    • Defines the color and intensity that a light adds to the scene
  • GL_SPECULAR color defines what color specular highlights will be
  • GL_AMBIENT color defines the amount of ambient light that a source adds to a scene

  • (picture of angular reflectivity)
     

    NOTE!  We are ignoring the contribution of the alpha parameter until we discuss blending
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Light Position
  • Lights can be considered directional or positional
    • Rays of directional lights can be considered to be parallel
    • The sun is an example of a real-world directional light 
    • A desklamp is an example of a positional light source
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; 
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

The light position is defined as (x, y, z, w)

w  = 0.0:    (x, y, z) = light direction
w  != 0.0:   (x, y, z) = light position
     
  •  By Default GL_POSITION = {0, 0, 1, 0} (directional, along the -Z axis)
  • Positional lights radiate in all directions by default
    • may be defined as spotlights
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Programming Note:
  • Large Polygons with local (positional) lights will interpolate the light values across the polygon
    • A problem for LARGE polygons (they may appear darker)
    • Break the polygon into smaller polygonal pieces



     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Attenuation
  • With real-world lights, intensity decreases for objects which are further away from the source
  • Directional lights are infinately far away -- attenuation is disabled
  • Attenuation is customizable for local lights

  •  

     
     
     
     
     
     
     

    k c = GL_CONSTANT_ATTENUATION 
    k l = GL_LINEAR_ATTENUATION 
    k q = GL_QUADRATIC_ATTENUATION

    By default kc = 1.0 kl = 0.0 and kq = 0.0
     
     

  •  NOTE:  All lighting values are attenuated exceptglobal ambient and emmisive
  • Because of the extra math, these calculations have lower performance -- use spairingly

  •  

     
     
     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Spotlights
  • Positional lights may have their effect constrained similiar to a spotlight

  •  

     
     
     
     
     
     
     


     
     

  •  GL_SPOT_CUTOFF selects the angle for the spotlight
  • By default the value is 180.0 -- omnidirectional
  • The definable range is 0.0 -> 90.0 (no definition above 90.0)
  • Define by:
    • glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
  • A direction must be defined also for spotlights [(0, 0, -1) default]

  •  

     
     
     
     
     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Spotlights (cont) 
  • Spotlights attenuation may be defined as described previously
  • Spotlight concentration is controled by GL_SPOT_EXPONENT
    • Vertex intensity is attenuated by the cosine of the angle from the light source  raised to the power of the exponent