Professional Documents
Culture Documents
Chapter 6:
Lighting
and
Shading
Chapter 3 - 2 Interactive Computer Graphics
Overview
Local and global illumination
Phong reflectance model (local illumination)
Flat, Gouraud, and Phong
Shading on a polygonal mesh
Surface subdivisions
Shading in OpenGL
WebGL lighting demo
Chapter 3 - 3 Interactive Computer Graphics
Chapter 3 - 4 Interactive Computer Graphics
Illumination models
General approach:
model the world
simulate physics
Local illumination
To make lighting fast, we will initially restrict
our attention to:
Light source, one surface, and viewer (ignore
inter-object reflections, shadows)
Ambient, diffuse, and specular reflection (ignore
transparency, refraction, reflection, …)
Chapter 3 - 8 Interactive Computer Graphics
Chapter 3 - 9 Interactive Computer Graphics
Light sources
In general, a light source is a rather
complicated thing. It can emit different
amounts of light for each
Location (x, y, z)
Direction (, f)
Wavelength (l)
Illumination function:
I(x, y, z, , f, l)
Examples: ambient,
point, area, spot,
distant, …
Chapter 3 - 10 Interactive Computer Graphics
Colored lights
Intensity of emitted light can also be a
function of wavelength
We usually model as I = [Ir, Ig, Ib]
components
Some experiments have been done with a
whole spectrum of color values, giving more
realistic results in some cases
Chapter 3 - 11 Interactive Computer Graphics
Ambient light
Intensity doesn’t vary with x, y, z, , f
I = [Iar, Iag, Iab]
Chapter 3 - 12 Interactive Computer Graphics
Point lights
Point lights have a location (so farther
objects receive less light) but are not
directional
I(p0) = [Ir(p0), Ib(p0), Ig(p0)]
How would you compute the illumination at
point p?
Illumination proportional to
inverse square of distance
I(p, p0) =
(1/d2) [Ir(p0), Ib(p0), Ig(p0)]
Chapter 3 - 13 Interactive Computer Graphics
Spotlights
Directional, i.e. light is emitted
in a narrow range of angles, q
More realistic spotlights would
model a gradual fall-off of light
E.g. cose f
= (s • l)e if s is direction of
source, l direction to
source, both unit vectors
Chapter 3 - 16 Interactive Computer Graphics
Diffuse term
A perfectly diffuse reflector is so rough that it
scatters light equally in all directions
But note that when the
light comes in at an angle,
the same energy is spread
out over larger area
Diffuse shading
At noon, illum. is 1
As the angle q (u in
figure) decreases,
illumination goes to
zero
Illumination is
proportional to cos(q)
(Lambert’s law)
cos(q) = l • n
Id = kd l • n Ld
Chapter 3 - 22 Interactive Computer Graphics
Chapter 3 - 23 Interactive Computer Graphics
Specular Term
Specular term adds highlights
in the reflection direction
Note that the smoother and
shinier the object, the tigher and brighter the
highlight
Highlight power falls as viewer v moves away
from reflection dir, r. (cos f = v • r)
Modeled as cosa f, a is
shininess coefficient (1..200)
Is = ks Ls (r • v)a
Chapter 3 - 24 Interactive Computer Graphics
Chapter 3 - 25 Interactive Computer Graphics
Polygon shading
How do you use the Phong Illumination
Model to render an object with shading?
Consider a polygonal sphere approximation
How do you find the normals to the faces?
Shade a face with a constant color?
glShadeModel(GL_FLAT);
Gouraud shading
Gouraud shading:
Define vertex normals as average
of surrounding faces
Compute lighting equation at
each vertex
Interpolate colors across polygon
glShadeModel(GL_SMOOTH);
Computation required
Per pixel?
Per vertex?
Very fast! Especially with reasonably large polygons
and hardware color interpolation
Example
Chapter 3 - 30 Interactive Computer Graphics
Bilinear interpolation
Rasterization is done a scan-line at a time
Bilinear interpolation: given a color at each
vertex, how do you render a scan-line with
smooth shading?
interpolate vertex colors along each edge of the
polygon to get colors for start
and end of scanline
interpolate colors along the
scanline
Chapter 3 - 31 Interactive Computer Graphics
Chapter 3 - 32 Interactive Computer Graphics
Gouraud drawbacks
Drawbacks of Gouraud
shading?
Polygon edges are still visible
Brightness is modelled as
a linear function, but that’s
not really accurate
Real highlights are small
and bright and drop off sharply
If polygons are too large, highlights get distorted
and dimmed (notice the funny shape)
How to avoid these artifacts?
Chapter 3 - 33 Interactive Computer Graphics
Phong shading
To eliminate artifacts,
interpolate normals
Results: better shading,
much nicer highlights
Computation required
per pixel?
Gouraud shader
See demo
Chapter 3 - 36 Interactive Computer Graphics
Phong shader
See demo
Chapter 3 - 37 Interactive Computer Graphics
Shading summary
Don’t confuse Phong Illumination Model and
Phong Shading
Gouraud shading: compute illumination
model at each vertex. Interpolate colors.
(Often done in hardware)
Phong shading: interpolate vertex normals.
Compute illumination model at each vertex
Chapter 3 - 38 Interactive Computer Graphics
Surface subdivision
In real modelers…
one can usually define smooth curved surfaces
Eg spheres, quadrics, NURBS, smoothed polygons
Modeler renders with smoothness setting
Recursively split polygons into smaller pieces,
with new vertices on the smooth surface
Splitting a triangle can be done by
Bisecting angles
Computing the centrum
Bisecting sides
Of course, smoother surfaces take longer to draw
Chapter 3 - 40 Interactive Computer Graphics
What’s missing?
Now that the rendering pipeline is all
software, we can add additional effects
What’s missing?
Chapter 3 - 41 Interactive Computer Graphics
Fresnel Effect
Surface reflectance depends on viewing angle
Fresnel shaders: allow reflection, specularity, other
attributes to depend on viewing angle
Example
Chapter 3 - 42 Interactive Computer Graphics
Chapter summary
Phong illumination model has ambient,
diffuse, and specular terms
It can be used for Flat, Gouraud, and Phong
shading
OpenGL supports eight ambient, point,
distant, and spot lights
You must specify light and material
properties with many OpenGL function calls
Curved surfaces are modeled with polygon
subdivisions