Lighting in OpenGL

CS 481/681 2007 Lecture, Dr. Lawlor

Kinds of Light that Reflect off Surfaces

Examples of Lighting

To actually render lighting, you need a zillion unit-length direction vectors starting at the point on the surface you're shading:
	vec3 N = normalize(normal); // Points away from surface
vec3 L1 = normalize(vec3(0,1,0)); // Points toward light 1
vec3 C = normalize(vec3(cameraloc)-position); // Points toward camera
vec3 H1 = normalize(C+L1); // Blinn's "halfway vector" for light 1
Then you need to compute (clamped) dot products between them:
	float A=0.2; // Ambient illumination
float D=clamp(dot(N,L1),0.0,1.0); // Diffuse light fraction
float S=pow(clamp(dot(N,H1),0.0,1.0),50.0); // Specular fraction. Constant controls highlight size
And finally you just combine these together--you can get lots of different effects by combining them in different ways.  Here I've added in "M" for the object ("Material") color, and "K" for a checkerboard pattern.


A- Ambient, 0.2

D- Diffuse, dot(N,L)

M- Material Color, gl_Color

S- Specular, pow(dot(N,H),...)


M*A
Typical Pure Ambient

M*D
Typical Pure Diffuse

M*(D+A)
Typical Lambertian

M*(D+A)+S
Classic Phong Lighting


K- Checkerboard, using step(fract(position))

K*M*(D+A)+S
Checkerboard controls overall diffuse color.

M*(D+A)+K*S
Checkerboard controls shininess.

M*(D+A)+S+0.5*K
Gently glowing checkerboard.

Try these out with the 481_lighting example program on the main page.