2. CS 354 2
Today’s material
In-class quiz
On GPU architecture lecture
Lecture topic
Shadow Algorithms
Course status
Mid-term graded, but not yet recorded
Project #2 on texturing, shading, & lighting is
coming
3. CS 354 3
My Office Hours
Tuesday, before class
Painter (PAI) 5.35
8:45 a.m. to 9:15
Thursday, after class
ACE 6.302
11:00 a.m. to 12
Randy’s office hours
Monday & Wednesday
11 a.m. to 12:00
Painter (PAI) 5.33
4. CS 354 4
Last time, this time
Last lecture, we
Took the mid-term
This lecture
Shadow generation algorithms
How can we efficiently determine when and
how much light is occluded by other objects
during shading?
5. CS 354 5
On a sheet of paper
Daily Quiz • Write your EID, name, and date
• Write #1, #2 followed by its answer
Multiple choice: The effective Multiple choice: Programmable
memory bandwidth of a tessellation is good for
graphics processor can be
higher than its raw memory a) displacement mapping
bandwidth due to:
b) per-pixel lighting
a) more multiply-add
operations per clock c) increasing the appearance of
surface curvature
b) not showing pixels when
they are opaque d) feeding more triangles to the
graphics pipeline
c) domain-specific
compression techniques e) a, c, and d
d) off-loading work to the f) a, b, c, and d
network
g) b, c, and d
6. CS 354 6
Shadows
Important visual cue
All real world scenes have shadows
Lack of shadows = big clue that an image is
computer generated!
Occlusion from light’s point-of-view instead of
viewer’s
Cue for light-object-object relationships
Artistic effect
Soft vs. hard feel, dramatic or spooky feel
7. CS 354 7
Shadows Provide
Cues About Depth
Two scenes
Difference is in shadows
Balls resting on ground Balls floating off surface
8. CS 354 8
Computed Shadows aren’t
Necessarily Realistic
Shadows of Past – Lyubomir Bukov
9. CS 354 9
Shadows in Gaming
Shading + Shadows = More Drama
+ Bump mapping
+ Specular
Only Shadows + Shading
Diffuse + Decal Combined
+ Shadows
10. CS 354 10
Ray Tracing
Approach to Shadows
Shadows = global lighting effect
Involves more than light + surface
11. CS 354 11
Ray Tracing Shadow Algorithm
Cast primary rays from eye
Determine surface intersection
Then, cast a secondary (shadow) ray
From surface
To each light source
12. CS 354 12
Interactive
Lighting Models
Typically lack shadow support
So-called “local” lighting models ignore
occlusion of light sources that create
shadows
Except trivial self-shadowing (i.e., clamping L•N)
Global lighting models account for shadows
Examples: radiosity, ray tracing
But too expensive for interactive use
Interactive shadow algorithms typically
operate independent of local lighting models
13. CS 354 13
Interactive Shadow
Simplications
True shadows are expensive, cut corners
Simple geometry tricks
Projection
Volume intersection
Pre-compute static shadow results where
possible
Make simplifying assumptions
Treat area light sources as points
Exploit hardware features such as stencil and
depth textures
17. CS 354 17
Shadow Mapping
Image-space shadow determination
Lance Williams published the basic idea in 1978
By coincidence, same year Jim Blinn invented bump
mapping (a great vintage year for graphics)
Completely image-space algorithm
means no knowledge of scene’s geometry is required
must deal with aliasing artifacts
Well known software rendering technique
Pixar’s RenderMan uses the algorithm
Leverages GPU hardware
Depth buffering + texture mapping
Multi-pass algorithm: render depth maps, then
project depth maps as textures for eye view
18. CS 354 18
Shadow Mapping
References
Important SIGGRAPH papers
Lance Williams, “Casting Curved
Shadows on Curved Surfaces,”
SIGGRAPH 78
William Reeves, David Salesin, and Robert Cook
(Pixar), “Rendering antialiased shadows with depth
maps,” SIGGRAPH 87
Mark Segal, et. al. (SGI), “Fast Shadows and Lighting
Effects Using Texture Mapping,” SIGGRAPH 92
OpenGL extensions
ARB_shadow, ARB_depth_texture
Standardized in OpenGL 1.4 (2002)
19. CS 354 19
Shadow Map Example
Pixar’s Luxo Jr. – state-of-the-art for 1986
Running in real-time – GeForce3 in Japan, 2001
3 light sources =
3 depth map
renders per frame
20. CS 354 20
Shadow Maps for Fine
Shadows
Chain-link fence is
shadow volume
nightmare!
Chain-link fence’s
shadow appears on
truck & ground with
shadow maps
21. CS 354 21
The Shadow Mapping
Concept (1)
Depth testing from the light’s point-of-view
Two pass algorithm
First, render depth buffer from the light’s
point-of-view
the result is a “depth map” or “shadow map”
essentially a 2D function indicating the depth of
the closest pixels to the light
This depth map is used in the second pass
22. CS 354 22
The Shadow Mapping
Concept (2)
Shadow determination with the depth map
Second, render scene from the eye’s point-of-
view
For each rasterized fragment
determine fragment’s XYZ position relative to the
light
this light position should be setup to match the
frustum used to create the depth map
compare the depth value at light position XY in the
depth map to fragment’s light position Z
23. CS 354 23
The Shadow Mapping
Concept (3)
The Shadow Map Comparison
Two values
A = Z value from depth map at fragment’s light XY
position
B = Z value of fragment’s XYZ light position
If B is greater than A, then there must be
something closer to the light than the
fragment
then the fragment is shadowed
If A and B are approximately equal, the
fragment is lit
24. CS 354 24
Shadow Mapping
with a picture in 2D
The A < B shadowed fragment case
depth map image plane
depth map Z = A
light
source
eye
position
eye view image plane,
aka the frame buffer
fragment’s
light Z = B
25. 25
Shadow Mapping
CS 354
with a picture in 2D
The A ≅ B unshadowed fragment case
depth map image plane
depth map Z = A
light
source
eye
position
eye view image plane,
aka the frame buffer
fragment’s
light Z = B
26. CS 354 26
Shadow Mapping
with a picture in 2D
Note image precision mismatch!
The depth map
could be at a
different resolution
from the framebuffer
This mismatch can
lead to artifacts
27. 27
Avoiding Artifacts with
CS 354
Depth Map Bias
OpenGL supports glPolygonOffset that provides window-space depth bias
Used during shadow map construction
How much polygon offset bias depends
Too little bias, Too much bias, shadow
everything begins to starts too far back
shadow
Just right
28. CS 354 28
Visualizing the Shadow
Mapping Technique (1)
A fairly complex scene with shadows
the point
light source
29. CS 354 29
Visualizing the Shadow
Mapping Technique (2)
Compare with and without shadows
with shadows without shadows
30. CS 354 30
Visualizing the Shadow
Mapping Technique (3)
The scene from the light’s point-of-view
FYI: from the
eye’s point-of-view
again
31. CS 354 31
Visualizing the Shadow
Mapping Technique (4)
The depth buffer from the light’s point-of-view
FYI: from the
light’s point-of-view
again
32. CS 354 32
Visualizing the Shadow
Mapping Technique (5)
Projecting the depth map onto the eye’s view
FYI: depth map for
light’s point-of-view
again
33. CS 354 33
Shadow Map Eye Linear
Texture Coordinate Transform
glTexGen automatically
xe xo applies this when modelview
Eye matrix contains just the eye
ye view Modeling yo view transform
ze
= (look at) matrix zo
matrix
we wo
s 1/2 1/2 Inverse xe
Light Light
t 1/2 1/2 frustum view
eye ye
r
= (projection) (look at)
view
ze
1/2 1/2 (look at)
matrix matrix
q matrix we
1
Supply this combined transform to glTexGen or shader
34. CS 354 34
Projective Texturing
Mimic perspective divide but for texture
(s,t,r,q) → (s/q,t/q,r/q)
Light’s view frustum needs perspective
An intuition for projective texturing
The slide projector analogy
Source: Wolfgang Heidrich [99]
35. CS 354 35
Visualizing the Shadow
Mapping Technique (5)
Projecting the depth map onto the eye’s view
FYI: depth map for
light’s point-of-view
again
36. CS 354 36
Visualizing the Shadow
Mapping Technique (6)
Projecting light’s planar distance onto eye’s view
Essentially r/q in the [0,1] range
Analogous to window-space depth for depth buffering
37. CS 354 37
Visualizing the Shadow
Mapping Technique (7)
Comparing light distance to light depth map
Green is where
the light planar
distance and Non-green is
the light depth where shadows
map are should be
approximately
equal
38. CS 354 38
Visualizing the Shadow
Mapping Technique (8)
Scene with shadows
Notice how Notice how
specular curved
highlights surfaces cast
never appear shadows on
in shadows each other
39. CS 354 39
More Examples
Smooth surfaces with object self-
shadowing
Note object self-shadowing
40. CS 354 40
More Examples
Complex objects all shadow
41. CS 354 41
Shadow Mapping Artifacts
Even the floor casts shadow, but…
Note shadow
leakage due to
infinitely thin
floor
Could be fixed by
giving floor
thickness
43. CS 354 43
Four Images of
Dueling Frusta Case
Eye’s View with
projection
of color-coded
Eye’s mipmap levels
View from light:
Red = minification
Blue = magnification
Light’s Light’s View with
View re-projection
of above image
from the eye
44. CS 354 44
Interpretation of the Images
of the Dueling Frusta Case
Region that is smallest in
the light’s view is a region
Eye’s
that is very large in the
View
eye’s view. This implies
that it would require a very
high-resolution shadow
map to avoid obvious
blocky shadow edge
artifacts.
Light’s
View
45. CS 354 45
Dueling Frusta Blocky
Shadow Edge Artifacts
Notice that
shadow edge
is well defined
in the
distance. Light position
out here pointing
towards the
viewer.
Blocky
shadow edge
artifacts.
46. CS 354 46
Shadow Acne
Ambiguous shadow map comparisons cause very
unsightly artifacts
Moiré patterns form; animates poorly
“shadow acne” polygon offset
depth comparison is provides sufficient depth offset
ambiguous for unambiguous depth
comparisons
47. CS 354 47
Planar Projected
Shadows
So-called “fake” shadows
Uses projective geometry trick
Flattens 3D model into ground plane
Shadow effect only can be applied to planes
Supports either positional or directional lights
49. CS 354 49
Projected Planar
Shadows
Classic computer graphics trick
Given
Ground plane equation, Ax + By + Cz + D = 0
Light position (x, y, z, w)
Construct projective transform that “squishes”
vertices into ground plane based on light
position
Concatenate this with view transform
Rendering 3D object creates shadow-like pile
of polygons in ground plane
50. CS 354 50
Projected Planar
Shadow Matrix
4x4 Projective Matrix
P - Lx A -Ly A -Lz A -Lw A
-Lx B P - Ly B -Lz B -Lw B
-Lx C -Ly C P - Lz C -Lw C
-Lx D -Ly D -Lz D P - Lw D
where P = Lx A + Ly B + Lz C + Lw D
51. CS 354 51
Careful about Reverse Projection
Projection can cast a shadow of an object “behind” the
light w.r.t. the plane
Make sure occluders are between light and ground plane
Good Bad, reverse projection
52. CS 354 52
Projected Planar
Shadow Issues
Shadow must be cast on infinite planes
Stencil can limit shadow to finite planar region
“Pile of polygons” creates Z-fighting artifacts
Polygon offset fixes this, but disturbs Z values
Stencil testing is better solution
Difficult to blend shadow with ground texture
Just blending creates “double blends”
But stencil testing can eliminate “double blends”
Specular highlights can show in shadow
53. CS 354 53
Planar Projected
Shadow Artifacts
Bad Good
extends off
ground region
Z fighting double blending
54. CS 354 54
Quick Tutorial on
Stencil Testing
An extra test for fine-grain pixel control
Standard OpenGL and Direct3D feature
Per-pixel test similar to depth buffering
Tests fragment against pixel’s stencil value, rejects
fragments that fail
Also, can modify pixel’s stencil buffer value based on stencil/
depth test results
Hardware accelerates stencil testing
Typically allocated with depth buffer
8-bit stencil value paired with 24-bit depth value
Invented by Kurt Akeley and Jim Foran (SGI)
55. CS 354 55
Stencil Testing in the
Fragment Pipeline
Fragment Pixel
+ Scissor Alpha
Ownership
Test Test
Associated Test
Data
Depth Stencil
Test Test
Blending Dithering Logic Frame
Op buffer
56. CS 354 56
Stencil Testing
Similar to Depth Testing but
Compares current reference value to pixel’s
stencil buffer value
Same comparison functions as depth test:
NEVER, ALWAYS
LESS, LEQUAL
GREATER, GEQUAL
EQUAL, NOTEQUAL
Stencil bit mask controls comparison
(( ref & mask ) op ( svalue & mask )
57. CS 354 57
Stencil Operations
Way stencil buffer values are controlled
Stencil side effects of
Stencil test fails
Depth test fails
Depth test passes
Possible operations
Increment, Decrement (saturates)
Increment Wrap, Decrement Wrap (a.k.a. modulo)
Keep, Replace, Zero, Invert
58. CS 354 58
OpenGL API
Stencil function
Controls when stencil test passes or fails
glStencilFunc(GLenum stencil_function,
GLint reference_value,
GLuint read_mask)
Stencil operations
Controls how the stencil buffer is updated
glStencilOp(GLenum stencil_fail,
GLenum depth_fail,
GLenum depth_pass)
Stencil mask
Controls bit-mask for writing stencil
glStencilMask(GLuint write_mask)
Also two-sided stencil API
Provides different stencil operations for front- and back-facing polygons
glStencilFuncSeparate, glStencilOpSeparate, glStencilMaskSeparate
59. CS 354 59
The Shadow Volume
Concept
Volumetric shadows, not just planar
A single point light source splits the world in two
shadowed regions
unshadowed regions
A shadow volume is the boundary between these
shadowed and unshadowed regions
First described by [Crow 77]
Professor at University of Texas!
Discrete framebuffer support
Shadow count in the framebuffer from Fournier and
Dr. Fussell (1988)
Translates natrually to the stencil buffer…
60. CS 354 60
Visualizing
Shadow Volumes
Occluder and light projects a shadow volume
61. CS 354 61
Shadow Volume
Result
Objects within the volume are shadowed
62. CS 354 62
Shadow Volume
Algorithm
High-level view of the algorithm
Given the scene and a light source position,
determine the shadow volume (harder than it
sounds)
Render the scene in two passes
Draw scene with the light enabled,
updating only fragments in unshadowed region
Draw scene with the light disabled,
updated only fragments in shadowed region
But how to control update of regions?
64. CS 354 64
Tagging Pixels as
Shadowed or Unshadowed
Using stencil testing
High-level algorithm does not say how to update
only either pixels in or out of the shadow volume!
The stenciling approach
Clear stencil buffer to zero and depth buffer to 1.0
Render scene to leave depth buffer with closest Zs
Render shadow volume into frame buffer with depth
testing but without updating color and depth, but inverting
a stencil bit
This leaves stencil bit set within shadow!
65. CS 354 65
Stencil Inverting of
Shadow Volume
Why it works
shadowing
light object
source
two inverts, left zero
one invert, left one
eye zero inverts, left zero
position
66. CS 354 66
Visualizing Stenciled
Shadow Volume Tagging
Shadowed scene Stencil buffer
red = stencil value of 1
green = stencil value of 0
67. CS 354 67
Computing
Shadow Volumes
Harder than you might think
Easy for a single triangle, just project out
three infinite polygons from the triangle,
opposite the light position
But shadow volume polygons should not
intersect each other for invert trick to work
This makes things hard
For complex objects, projecting object’s 2D
silhouette is a good approximation (flat
objects are easy)
Static shadow volumes can be pre-compiled
68. CS 354 68
For Shadow Volumes
With Intersecting Polygons
Use a stencil enter/leave counting approach
Draw shadow volume twice using face culling
1st pass: render front faces and increment when
depth test passes
2nd pass: render back faces and decrement when
depth test passes
This two-pass way is more expensive than
invert
And burns more fill rate drawing shadow
volumes
Inverting is better if no polygon intersections
69. CS 354 69
Why Eye-to-Object Stencil
Counting Approach Works
Light Shadowing object
source
zero +1
zero
Example in 2D
+2 +2
Eye +1
+3
position
71. CS 354 71
Shadowed, Nested in Shadow
Volumes (Zpass)
Light Shadowing object
source
zero +1
zero
+ + + -
+2 +2 Shadowed
Eye +1 object
+3
position
Shadow Volume Count = +1+1+1-1 = 2
72. CS 354 72
Illuminated, In Front of Shadow
Volumes (Zpass)
Light Shadowing object
source
zero +1
zero
+2 +2 Shadowed
Eye +1 object
+3
position
Shadow Volume Count = 0 (no depth tests pass)
73. CS 354 73
Shadow Volume
Issues
Practical considerations [Bergeron 86]
If eye is in shadow volume, need to
determine this and flip enabled & disabled
lighting passes
Shadow volume only as good as its
tessellation
Shadows tend to magnify limited tessellation of
curved surfaces
Open models and non-planar polygons
Must cap the shadow volume’s intersection
with the near clipping plane
74. CS 354 74
Problem Created by
Near Clip Plane (Zpass)
Missed shadow volume
intersection due to near
clip plane clipping; leads
to mistaken count Far clip
plane
zero
+1
+1
+2
zero
+3
+2
Near clip
plane
75. CS 354 75
Alternative Approach: Zfail
Render scene to initialize depth buffer
Depth values indicate the closest visible
fragments
Use a stencil enter/leave counting
approach
Draw shadow volume twice using face
culling
1st pass: render back faces and increment when
depth test fails
2nd pass: render front faces and decrement when
depth test fails
77. CS 354 77
Shadowed, Nested in
Shadow Volumes (Zfail)
Light Shadowing object
source
zero +1
zero
+ +
+2 +2 Shadowed
Eye +1 object
+3
position
Shadow Volume Count = +1+1 = 2
78. CS 354 78
Illuminated, In Front of Shadow
Volumes (Zfail)
Light Shadowing object
source
zero +1
zero
- - - + + +
+2 +2 Shadowed
Eye +1 object
+3
position
Shadow Volume Count = -1-1-1+1+1+1 = 0
79. CS 354 79
Problem Created by
Far Clip Plane (Zfail)
Far clip
plane
Missed shadow volume
intersection due to far
clip plane clipping;
zero
leads to mistaken count
+1
zero
+1 +2
+2
+3
Near clip
plane
80. CS 354 80
Problem Solved by
Eliminating Far Clip
zero
+1
+1 +2
+2
+3
Near clip
plane
81. CS 354 81
Avoiding Far Plane Clipping
Usual practice for perspective GL projection
matrix
Use glFrustum (or gluPerspective)
Requires two values for near & far clip planes
Near plane’s distance from the eye
Far plane’s distance from the eye
Assumes a finite far plane distance
Alternative projection matrix
Still requires near plane’s distance from the eye
But assume far plane is at infinity
What is the limit of the projection matrix when
the far plane distance goes to infinity?
82. CS 354 82
Standard glFrustum Projection
Matrix
2 × Near Right + Left
Right − Left 0 0
Right − Left
2 × Near Top + Bottom
0 0
P= Top − Bottom Top − Bottom
Far + Near 2 × Far × Near
0 0 − −
Far − Near Far − Near
0 0 −1 0
Only third row depends on Far and Near
83. CS 354 83
Limit of glFrustum Matrix as
Far Plane is Moved to Infinity
2 × Near Right + Left
Right − Left 0 0
Right − Left
2 × Near Top + Bottom
lim P = Pinf = 0 0
Far →∞ Top − Bottom Top − Bottom
0 0 −1 − 2 × Near
0 0 −1 0
First, second, and fourth rows are the same as
in P
But third row no longer depends on Far
Effectively, Far equals ∞
84. 84
Verifying Pinf Will Not Clip
CS 354
Infinitely Far Away Vertices (1)
What is the most distant possible vertex in
front of the eye?
Ok to use homogeneous coordinates
OpenGL convention looks down the negative Z
axis
So most distant vertex is (0,0,-D,0) where D>0
Transform (0,0,-D,0) to window space
Is such a vertex clipped by Pinf?
No, it is not clipped, as explained on the next
slide
85. 85
Verifying Pinf Will Not Clip
CS 354
Infinitely Far Away Vertices (2)
Transform eye-space (0,0,-D,0) to clip-space
2 × Near Right + Left
0 0
xc xc Right − Left Right − Left 0
y y 2 × Near Top + Bottom
0
c = c = 0 0
− D zc Top − Bottom Top − Bottom − D
0 0 −1 − 2 × Near
− D wc 0
0 0 −1 0
Then, assuming glDepthRange(0,1), transform clip-
space position to window-space position
zc −D
z w = 0.5 × + 0.5 = 0.5 × + 0.5 = 1
wc −D
So ∞ in front of eye transforms to the maximum
window-space Z value, but is still within the valid depth
range (i.e., not clipped)
86. CS 354 86
Robust Shadow Volumes sans
Near (or Far) Plane Capping
Use Zfail Stenciling Approach
Must render geometry to close shadow volume extrusion
on the model and at infinity (explained later)
Use the Pinf Projection Matrix
No worries about far plane clipping
Losses some depth buffer precision (but not much)
Draw the infinite vertices of the shadow volume
using homogeneous coordinates (w=0)
87. CS 354 87
Visualize Net Stencil Rendering
Non-zero stencil state marks shadowed pixels
w.r.t. the light source
Fully shaded scene Final stencil state
89. CS 354 89
Shadow Volumes Add “Invisible”
Geometric Complexity
Visible geometry
Scene with multiple light sources Shadow volume
geometry
Abducted game images courtesy
Joe Riedel at Contraband Entertainment
90. CS 354 90
Examples of Possible Silhouette
Edges for Quake2 Models
An object viewed from the
same basic direction that
the light is shining on the
object has an identifiable
light-view silhouette
An object’s light-view
silhouette appears quite
jumbled when viewed form
a point-of-view that does
not correspond well with
the light’s point-of-view
91. CS 354 91
Multiple Lights and
Shadow Volumes
Requires still more rendering passes,
but can work!
Shadows from different
light sources overlap
correctly
92. CS 354 92
Shadow Volumes from
Area Light Sources
Make soft shadows
Shadow volumes work for point light sources
Area light sources are common and make
soft shadows
Model an area light source as a collection of
point light sources [Brotman & Badler 84]
Use accumulation buffer or additive blending
to accumulate soft shadow
Linear cost per shadow volume sample
93. CS 354 93
Area Light Sources Cast Soft
Shadows
area light
source
umbra
94. CS 354 94
Soft Shadow Example
Eight samples (more would be better)
Note the banding
artifacts
95. CS 354 95
Combined
Shadow Approaches
Two Approaches at Once
just logo shadow volume
logo lacks shadow
on the teapot
just planar projected
teapot lacks
shadows
shadow
on the floor
combined
approach
96. CS 354 96
Pre-computed
Shadow Textures
Lightmaps are static shadow textures
Compute lightmaps off-line with radiosity
solver
local lighting models evaluated on tight grid can
work well too
Lightmaps with soft shadows can be built
faster with convolution approach, accelerated
using the Fast Fourier Transform [Soler &
Silion 98]
Pre-computed shadow textures work well for
building interior with lots of diffuse surfaces
97. CS 354 97
Light maps can encode
Static Pre-computed Shadows
decal only
×
(modulate)
light maps only
=
* Id Software’s Quake 2
combined scene circa 1997
98. CS 354 98
Rendering
Soft Shadow Textures
Fast soft shadows [Heckbert & Herf 96]
Render a shadow texture
For each shadowed polygon, project all light-source-
occluding polygons into the shadowed polygon’s plane
* Similar to projected planar shadows
* Model area light sources as multiple jittered points
* Use additive blending to accumulate all contributions
Copy resulting image to the shadow texture
Draw polygon in final scene with shadow texture
99. CS 354 99
Rendered
Soft Shadow Texture Example
Shadow Texture and Final Result
Shadow texture, accumulation Final resulting scene, shadow
of nine jittered occluder texture modulated with
projections wooden floor
100. CS 354 100
Next Class
Mid-term
Return graded mid-term
Review class statistics
Project 2 assigned
On texturing, shading, & lighting
Study GLSL
Next lecture
Scene graphs
How do applications organize data structures for
efficient rendering of scenes?