Download Understanding Shadows in Computer Graphics: From Hard to Soft Shadows and more Study notes Computer Graphics in PDF only on Docsity!
Adding Shadows to the World
Our illumination model does not incorporate shadows
- only diffuse, specular, and ambient reflection
- we always incorporate contribution of light sources
- even if there’s an object in the way
How do we get shadows?
- more advanced rendering methods
- e.g., ray tracing and radiosity
- or do some clever things
cos cos
n
L d L s a a
I = I k! + I k "+ I k
The Structure of Shadows
Point Light Source
Occluder
or
Blocker
Shadow
Starting with a Restricted Solution
Let’s make some simplifying assumptions about the world
- the world contains objects + a ground plane
- only care about shadows on this ground plane
- so we are willing to ignore
- shadows of one object on another
- self-shadowing by a single object
What problem do we need to solve?
- given a point and a plane
- project an object onto this plane
- sounds kind of familiar
Projective Shadows
The point p lies at the intersection of
Substitute ray into plane equation
- solve for t
- substitute back into ray equation for p
This gives us a linear equation for p
plane: n! u + d = 0
L
v
p
ray L + t ( v! L ) and plane n " u + d = 0
n! L + t ( v " L ) + d = 0
! d + # L "
n
p L v L
n v L
The OpenGL Stencil Buffer
Another buffer, alongside the frame buffer and depth buffer
- typically <= 8 bits per pixel
- can subject fragments to a stencil test during rasterization
- those that fail the test don’t get written to frame buffer
- turned on with glEnable(GL_STENCIL_TEST)
- test selected with glStencilFunc()
- control writing into stencil buffer with glStencilOp()
Among other things, can be used to mask drawing operations
- fill in the stencil pixels where drawing is allowed (say with 1)
- draw scene with glStencilFunc(GL_EQUAL, 1, 1)
- fragments where stencil is not 1 are discarded
- see Red Book for more details
Another Problem: Anti-Shadows
Projective shadows can actually produce “anti-shadows”
- everything is fine if the light is above the blocker
- relative to the ground plane
- problematic if lower than topmost point of the blocker
What Points are in Shadow?
All points for which rays to/from light source are blocked
In other words: all the points that the light can’t see
- this sounds like visible surface determination
Casting More General Shadows
Suppose we have a scene like the one below
- we want to be able to cast shadows on any object
Drawing with Shadow Buffers and OpenGL
First, compute the shadow buffer for the light source
Next, render the scene with only ambient lighting
- we only want to deal with visible surfaces
Compare z values in view with z values in shadow buffer
- but remember, these are depths from two different points
- if this is the nearest object, set pixel α = 1
- otherwise, set pixel α = 0
Finally, render the entire scene again with lighting
- set things up so that colors are multiplied by α
- just set up glBlendFunc() appropriately
- objects in shadow will have their colors zeroed
The More General Lesson
Rendering is not just for producing pictures
- we can use it to acquire information about the world
- clever combinations of rendering passes are powerful tools
Shadow maps are a good example of this
- “render” the world to produce shadow maps
- make use of blending to combine results
We can also make good use of additional OpenGL buffers
- in particular, we’ve seen a simple use of the stencil buffer
From Hard to Soft Shadows
These techniques can help us generate shadows
- but these shadows have perfectly sharp boundaries
- real shadows are “soft” rather than “hard”
- because lights in the real world are not point light sources
The Structure of Shadows
Area Light Source
Occluder
or
Blocker
Umbra
(complete shadow)
Penumbra
(partial shadow)
Penumbra
(partial shadow)
“Removing” Hidden Lines with 2 Passes
Notice that we see all the lines
- even for triangles in back
- which can be confusing
There are many algorithms for this
- was once an important problem
But OpenGL provides us a nice trick
- first, draw all the polygons
- but in the background color
- now draw all the wireframe lines
To make this look nice
- see glPolygonOffset()
- make lines sit on top of polygons
Another Simple Multipass Example: Mirrors
Suppose we’re writing a racing simulator
- all our cars have rear-view mirrors
- how do we draw the right thing in them?
For every frame, draw the world twice
- on the first pass, draw the world facing forward
- now restrict drawing area to the rear-view mirror
- would generally use the stencil buffer for this
- set up camera to look back through mirror
- and execute a second drawing pass
And we’ve got a “reflected” image of the world behind us
- unlike environment mapping, this reflection is actually correct
Accumulation Buffer
Many multipass rendering methods have a similar structure:
- render scene multiple times in different ways
- sum/average corresponding pixels in the resulting images
OpenGL’s accumulation buffer makes this easy
- one downside is that it’s not always implemented in hardware
Accumulation Buffer Operations
glAccum(GL_ACCUM, s )
- copy contents of frame buffer, scaling color values by s and
add into current contents of accumulation buffer
glAccum(GL_LOAD, s )
- same as above, but replace rather than sum values
glAccum(GL_RETURN, s )
- copy contents of accumulation buffer into frame buffer, scaling
each color component by s
glAccum(GL_ADD, s )
glAccum(GL_MULT, s )
- sum/multiply each color component with s