OpenGL:  Lighting
from http://www.eecs.tulane.edu/www/Terry/OpenGL/
 
 

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.

OpenGL uses Phong Lighting Model for Specular reflected light.

 

Issues in Lighting with OpenGL

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
    (NOTE: ABOVE CODE IS IN TK windowing NOT GLUT windowing system)
 

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. 

 

 

The STEPS

1) Enable Lighting

glEnable(GL_LIGHTING);

2) Setup each Light Source you want to create GL_LIGHTx

(where 0<=x<=GL_MAX_LIGHTS)

 

glLightfv(GL_LIGHT1, GL_POSITION, position1);

glLightfv(GL_LIGHT1, GL_AMBIENT, lightambient1);

glLightfv(GL_LIGHT1, GL_DIFFUSE, lightdiffuse1);

3) Enable each Light Created in Step 2

glEnable(GL_LIGHTx)

 

Spotlights

Spotlights are point sources but emit light over a narrow range of angles. Imagine a cone of light coming from the spotlight. What must we do to create one is to alter the following parameters using the glLightx() function on the light under question:

  • We have to specify its spread. Specify the angle between the axis of the cone and a ray along its edge. GL_SPOT_CUTOFF default 180 giving a spread of 360 - no cone Values for the cutoff angle must lie between 0 and 90 (unless it is 180 in which case it is not a spotlight).

  • We also specify a direction for spotlights. They have position and direction.
    GL_SPOT_DIRECTION default 0.0,0.0,-1.0 (pointing down negative z-axis)

  • Finally, we can control the intesity of the distribution of light within the cone. This controls how concentrated the light is. The intensity of the light is highest in the centre of the cone. It is attenuated towards the edges. The higher we set the spot exponent the more focused the light source (appears - brighter, shinier, more intense)
    GL_SPOT_EXPONENT default=0