CPEN 461/661, Spring 1997
OpenGL Tutorial

  1. Lighting
  2. OpenGL provides two types of light sources: directional and positional. A directional light source is considered to be an infinite distance away from the objects in the scene. Thus, its rays of light are considered parallel by the time they reach the object. A positional light is near or within the scene and the direction of its rays are taken into account in lighting calculations. Positional lights have a greater performance cost than directional lights due to these additional calculations.

    The command glLightfv() is used to specify the position of the light and whether it is directional or positional. It is also used to specify the values of the color components of the light source such as ambient color, diffuse color, specular color, emissive color, and shininess.

    Once the light sources are defined, normal vectors and material properties of the objects in the scene must also be defined. An object's normal vectors define its orientation relative to light sources. Normal vectors can be specified for each vertex and/or shared by vertices. The command glNormal() is used to make this assignment.

    The color components specified for lights have different meaning than those specified for materials. For lights, the numbers correspond to the percentage of full intensity of each color. Therefore, the brightest possible white light has RGB values of (1.0, 1.0, 1.0). For materials, the numbers correspond to the reflected proportions of those colors. The command glMaterialfv() is used to define material properties by specifying the values of the color components of materials.

    The command glColorMaterial() is used to minimize the performance costs associated with changing material properties. This command should be used whenever a single material property, such as diffuse color, must be changed for most vertices in the scene. Any change to the current color made by a call to glColor() immediately updates the material property specified by glColorMaterial(). The capability state variable GL_COLOR_MATERIAL must be enabled by the call glEnable(GL_COLOR_MATERIAL).

    Lastly, lighting must be enabled by a call to glEnable(GL_LIGHTING) and each light in the scene must be enabled by calls to glEnable(GL_LIGHTi) where GL_LIGHTi is the symbolic name of the light.


    This example is a modification of Polygons_List.c to use lighting. The three sided pyramid was changed to a four-sided pyramid to make calculating the normal vectors easier.

    Light.c Output



    Light.c Source Code

    Notice in the source code the order in which normals are defined for GL_QUAD_STRIP. In the GL_QUAD_STRIP section, Normal A applies to vertices 1, 2, 3, and 4; Normal B applies to vertices 3, 4, 5, and 6; Normal C applies to vertices 5, 6, 7, and 8; and Normal D applies to vertices 7, 8, 9, and 10. Due to the way that OpenGL renders objects, the correct normal value must be set before the last vertex in the current object is specified so that the correct normal value is assigned to that object.