The OpenGL Lighting Model
Andrew G. Zaferakis
UNC Chapel Hill
COMP 236 Spring 2000

Homework 5b - Lighting
    For this assignment we implemented the OpenGL lighting model in SoftGL.  The lighting is done in RGBA mode and not Color Index mode.  The goal of the assignment is to maintain pixel accurate images compared to OpenGL, which was a success.  SoftGL supports 8 lights, as the spec requires.  However, this can be increased to any number since the calculations are all done in software, however performance will scale linearly (negatively) with each additional light.  Material values may be specified for the polygons as well.  The lighting spec matches exactly to the OpenGL 1.1 spec, therefore not  supporting secondary lights which the 1.2 spec includes.
    Here is a brief description of the mathematics of the lighting model, a more in depth description can be found in the spec itself.  Simply put:

Vertex Color = Material Emission at the Vertex +
Ambient Property at that Vertex +
The Ambient, Diffuse, and Specular Contributions from all the Light Sources, properly Attentuated.

    Below is a short description of each component of the lighting computation.

Component
Contribution
Material Emission
RGBA value assigned to GL_EMISSION parameter
Scaled Global Ambient Light
ambientlight_model * ambientmaterial
where light model is GL_LIGHT_MODEL_AMBIENT
Attenuation Factor
 1 / (kc + kld + kqd2)
d = distance between light and vertex
kc = GL_CONSTANT_ATTENUATION
kl = GL_LINEAR_ATTENTUATION
kq = GL_QUADRATIC_ATTENUATION
If the light is directional, the attenuation valus is 1.
Spotlight
1 if GL_SPOT_CUTOFF = 180
0 if the vertex outside the cone of illumination
max{v dot d, 0}GL_SPOT_EXPONENT
v is unit vector from spotlight to vertex
d is the spotlight direction
Ambient
ambientlight * ambientmaterial
Diffuse
max{L dot n, 0}* diffuselight * diffusematerial
L is unit vector from vertex to light
n is the normal at the vertex
Specular
max{s dot n, 0}SHININESS * specularlight * specularmaterial
if {L dot n} < 0, the specular component is zero.
s is the sum of the two unit vectors that point between (1) the vertex and the light position (or direction) and (2) the vertex and the viewpoint (if GL_LIGHT_MODEL_LOCAL is true, else the vector is (0,0,1).
(Taken from the OpenGL Programming Guide, a.k.a The Red Book)

    Mathematically putting it together:

    Below are images taken of renderings in SoftGL.  The hardware images match per pixel.  Special thanks to Kenny Hoff for some of the models.  The deformable cube demo is very useful to show how much an improvement good hardware (full OpenGL support) performs compared to software, by changing the number of lights active.  The second demo uses the tie fighter model and shows how well software performs when using three moving lights.

(click on images for larger versions)

Tie Fighter
2998 Triangles
3 Moving Lights
Utah Teapot
1600 Triangles
3 Moving Lights
Stanford Bunny
69451 Triangles
3 Moving Lights
The bunny has only one normal per-triangle, instead of one normal per-vertex which yields a much smoother lighting.  The lack of vertex normals is shown in the faceting of the image, this is not an artifact of the rendering.
Human Head
2813 Triangles
3 Moving Lights
Deformable Cube
6 Bi-Cubic Bezier Patches
(~7500 Triangles)
8 Spotlights (7 Moving)
I created the cube using 56 control points in six patches.  The control points are then randomly moved around their origin a fixed difference creating a deforming effect.  Notice the specularity of the lights shown in the curves.
Here are two images of the cube, one using flat shading that shows the faceting.  The OpenGL spec says that one vertex normal is to be used for the entire triangle, which is what creates the faceting effect.
Smooth Shading

 
 
SoftGL Viewer Controls (Deformable Cube)
Mouse  Used to Rotate
Left Button Locks mouse movements to controls
, Zoom In
. Zoom Out
1-8 Toggles Lights 1-8 On/Off
q Speeds up the Moving Lights
w Slows down the Moving Lights
b Toggle Backface Culling
s Toggle Smooth/Flat Color Interpolation
l Toggle Lighting
d Toggle Control Points (OpenGL only)
p Toggle Test Plane
P Toggle Deformable Cube
\ Toggle Moving/Stationary Lights
` Toggle Deforming/Stationary Cube
; Decrease Spotlight Exponent
' Increase Spotlight Exponent
[ Decrease Spotlight Cutoff
] Increase Spotlight Cutoff
ESC Exit
SPACE Toggle SoftGL vs OpenGL mode
home

346 Sitterson Hall, CB #3175, Chapel Hill, NC 27599-3175, andrewz@cs.unc.edu