Shadows

Shadows

Acknowledgement: Images and many slides from presentations by Mark J. Kilgard and other Nvidia folks, from slides on developer.nvidia.com

Practical & Robust Stenciled Shadow Volumes for Hardware-Accelerated Rendering, Cass Everitt & Mark J. Kilgard, GDC 2002 Shadow Mapping with Today’s OpenGL Hardware, Mark Kilgard, CEDEC 2001, Tokyo, Japan Robust Stencil Shadow Volumes, Mark Kilgard, CEDEC 2001, Tokyo, Japan Shadow Mapping, Cass Everitt Reflections, Shadows,Transparency, and Fog, Mark Kilgard, GDC 2000 Tutorial Shadow Mapping with Todays OpenGL Hardware, Mark Kilgard, GDC 2000 Tutorial

Shadows

T Important visual cue S All real world scenes have shadows S Occlusion from light’s point-of-view instead of viewer’s R Cue for light-object-object relationships

CS 4451 Fall 2002 1 Shadows

Local Illumination & Shadows

T Typically lack shadow support S Ignore occlusion of lights that creates shadows T Global lighting models account for shadows S Too expensive for interactive use

T Interactive shadow algorithms typically operate independent of local lighting models

Interactive Shadow Simplifications

T True shadows are expensive, cut corners S Simple geometry tricks R Projection R Volume intersection S Pre-compute static shadow results where possible S Make simplifying assumptions R Treat area light sources as points S Exploit hardware features such as stencil

CS 4451 Fall 2002 2 Shadows

Common Real-time Shadow Techniques

Projected Projected Shadow planar volumes shadows

Hybrid approaches

Light maps

Problems with Common Techniques

T Mostly tricks with lots of limitations S Light maps R totally unsuited for dynamic shadows S Projected planar shadows R well works only on flat surfaces S Stenciled shadow volumes R determining the shadow volume is hard work S Shadow Maps R only works with spotlight frustums R shadow acne artifacts

CS 4451 Fall 2002 3 Shadows

Pre-computed Shadow Textures (Light maps, 8.5 in book)

T are static shadow textures S Compute lightmaps off-line with radiosity solver R local lighting models evaluated on tight grid can work too S Lightmaps with soft shadows can be built faster with convolution approach, accelerated using the Fast Fourier Transform [Soler & Silion 98] S Pre-computed shadow textures work well for building interior with lots of diffuse surfaces

Aside: Stencil Buffers

T An extra test for fine-grain pixel control S Standard OpenGL and DirectX 6 feature S Per-pixel test similar to depth buffering R Tests fragment against pixel’s stencil value, rejects fragments that fail R Also, can modify pixel’s value based on stencil/depth test results S Hardware accelerates stencil testing R Typically free when depth testing too

CS 4451 Fall 2002 4 Shadows

Stencil Testing

T Similar to Depth Testing but S Compares current reference value to pixel’s stencil buffer value S Same comparison functions as depth test T Stencil values controlled by stencil ops S “stencil” side effects of stencil & depth tests S Possible operations R Increment, Decrement R Keep, Replace, Zero, Invert

Projected Planar Shadows

CS 4451 Fall 2002 5 Shadows

Projected Planar Shadows

T Classic computer graphics trick S Given R Ground plane equation, Ax + By + Cz + D = 0 R Light position (x, y, z, w) S Construct projective transform that “squishes” vertices into ground plane based on light position R Concatenate this with view transform S Rendering 3D object creates shadow-like pile of polygons in ground plane

Projected Planar Shadow Issues

T Shadow must be cast on infinite planes T “Pile of polygons” creates Z-fighting artifacts S Polygon offset fixes this, but disturbs Z values T Difficult to blend shadow with ground texture S Just blending creates “double blends” S Specular highlights can show in shadow

T Stencil testing can fix most of these issues

CS 4451 Fall 2002 6 Shadows

Projected Planar Shadow Artifacts

Bad Good

extends off ground region

Z fighting double blending

Shadow Volumes

CS 4451 Fall 2002 7 Shadows

The Shadow Volume Concept

T Volumetric shadows, not just planar S A single point light source splits the world in two R shadowed regions R unshadowed regions S A shadow volume is the boundary between these shadowed and unshadowed regions S First described by [Crow 77]

Visualizing the Shadow Volume

T Occluders and light source cast out a shadow volume S Objects within the volume should be shadowed Light source

CS 4451 Fall 2002 8 Shadows

Shadow Volume Result

T Objects within the volume are shadowed

Shadow Volume Algorithm

T High-level view of the algorithm S Given the scene and a light source position, determine the shadow volume S Render the scene in two passes R Draw scene with the light enabled, updating only fragments in unshadowed region R Draw scene with the light disabled, updated only fragments in shadowed region S But how to control update of regions?

CS 4451 Fall 2002 9 Shadows

2D Cutaway of a Shadow Volume

Surface outside Shadowing shadow volume object (illuminated) Light source Shadow volume (infinite extent)

Eye position (note that shadows Partially Surface inside are independent of shadowed shadow volume the eye position) object (shadowed)

Counting Shadow Volume Enter/Leaves With a Stencil Buffer

T Render scene to initialize depth buffer S Depth values indicate the closest visible fragments T Use a stencil enter/leave counting approach S Draw shadow volume twice using face culling R 1st pass: render front faces and increment when depth test passes R 2nd pass: render back faces and decrement when depth test passes S Don’t update depth or color T Afterward, pixel’s stencil is non-zero if pixel in shadow, and zero if illuminated

CS 4451 Fall 2002 10 Shadows

Why Eye-to-Object Stencil Enter/Leave Counting Approach Works

Light Shadowing object source

zero +1

zero

+2 +2 Eye +1 +3 position

Illuminated, Behind Shadow Volumes

Light Shadowing object source

zero +1

zero

+ + + - - - Unshadowed object +2 +2 Eye +1 +3 position

Shadow Volume Count = +1+1+1-1-1-1 = 0

CS 4451 Fall 2002 11 Shadows

Shadowed, Nested in Shadow Volumes

Light Shadowing object source

zero +1

zero

+ + + -

+2 +2 Shadowed Eye +1 object +3 position

Shadow Volume Count = +1+1+1-1 = 2

Illuminated, In Front of Shadow Volumes

Light Shadowing object source

zero +1

zero

+2 +2 Shadowed Eye +1 object +3 position

Shadow Volume Count = 0

CS 4451 Fall 2002 12 Shadows

Problems Created by Near Plane Clipping

Missed shadow volume intersection due to near clip plane clipping; Far clip leads to mistaken count plane zero

+1 +1 +2 zero +3 +2

Near clip plane

Visualizing the Stencil Buffer Counts

Shadowed scene Stencil buffer contents

Stencil counts beyond 1 are possible for multiple or complex occluders.

red = stencil value of 1 green = stencil value of 0

GLUT shadowvol example credit: Tom McReynolds

CS 4451 Fall 2002 13 Shadows

Computing Shadow Volumes

T Harder than you might think S Easy for a single triangle, just project out three infinite polygons from the triangle, opposite the light position S For complex objects, projecting object’s 2D silhouette is a good approximation (flat objects are easy) S Static shadow volumes can be pre-compiled

Stenciled Shadow Volumes in Practice (1)

Scene with shadows. Yellow Same scene visualizing light is embedded in the green the shadow volumes.

three-holed object. Pinf is used for all the following scenes.

CS 4451 Fall 2002 14 Shadows

Stenciled Shadow Volumes in Practice (2)

Details worth noting . . .

Hard case: The shadow volume Fine details: Shadows from the front-facing hole of the A, N, and T letters on would definitely intersect the knight’s armor and shield. the near clip plane.

Stenciled Shadow Volumes in Practice (3)

Alternate view of same scene Shadow volumes from the with shadows. Yellow lines alternate view. indicate previous view’s view frustum boundary. Recall shadows are view-independent.

CS 4451 Fall 2002 15 Shadows

Shadow Volume Issues

T Practical considerations [Bergeron 86] S If eye is in shadow volume, need to determine this and flip enabled & disabled lighting passes S Shadow volume only as good as its tessellation R Shadows tend to magnify limited tessellation of curved surfaces S Must cap the shadow volume’s intersection with the near clipping plane S Open models and non-planar polygons

Reconstructing Shadow Volume From a Depth Buffer

T Very clever idea [McCool 98] S Render scene from light source with depth testing S Read back the depth buffer S Use computer vision techniques to reconstruct the shadow volume geometry from the depth buffer image S Very reasonable results for complex scenes

CS 4451 Fall 2002 16 Shadows

Multiple Lights and Shadow Volumes

T Requires still more rendering passes, but can work!

Shadows from different light sources overlap correctly

Shadow Volumes from Area Light Sources

T Make soft shadows S Shadow volumes work for point light sources S Area light sources are common and make soft shadows S Model an area light source as a collection of point light sources [Brotman & Badler 84] S Use accumulation buffer or additive blending to accumulate soft shadow S Linear cost per shadow volume sample

CS 4451 Fall 2002 17 Shadows

Soft Shadow Example

T Eight samples (more would be better)

Note the banding artifacts

Combined Shadow Algorithm Example: Shadow Volume + Planar Projection

just logo shadow volume

logo lacks shadow on the teapot

just planar projected teapot lacks shadows shadow on the floor combined approach

CS 4451 Fall 2002 18 Shadows

Shadow Volume Advantages

T Omni-directional approach S Not just spotlight frustums as with shadow maps T Automatic self-shadowing S Everything can shadow everything, including self S Without shadow acne artifacts as with shadow maps T Window-space shadow determination S Shadows accurate to a pixel S Or sub-pixel if multisampling is available T Required stencil buffer broadly supported today S OpenGL support since version 1.0 (1991) S support since DX6 (1998)

Shadow Volume Disadvantages

T Ideal light sources only S Limited to local point and directional lights S No area light sources for soft shadows T Requires polygonal models with connectivity S Models must be closed (2-manifold) S Models must be free of non-planar polygons T Silhouette computations are required S Can burden CPU S Particularly for dynamic scenes T Inherently multi-pass algorithm T Consumes lots of GPU fill rate

CS 4451 Fall 2002 19 Shadows

Shadow Mapping

Shadow Mapping

T Image-space shadow determination S Lance Williams published the basic idea in 1978 S Completely image-space algorithm R no knowledge of scene’s geometry is required R must deal with aliasing artifacts S Well known software rendering technique R Pixar’s RenderMan uses the algorithm R Basic shadowing technique for Toy Story, etc.

CS 4451 Fall 2002 20 Shadows

The Shadow Mapping Concept (1)

T Depth testing from the light’s point-of-view S Two pass algorithm S First, render depth buffer from the light’s point-of- view R the result is a “depth map” or “shadow map” R essentially a 2D function indicating the depth of the closest pixels to the light S This depth map is used in the second pass

The Shadow Mapping Concept (2)

T Shadow determination with the depth map S Second, render scene from the eye’s point-of-view S For each rasterized fragment R determine fragment’s XYZ position relative to the light R compare the depth value at light position XY in the depth map to fragment’s light position Z

CS 4451 Fall 2002 21 Shadows

The Shadow Mapping Concept (3)

T The Shadow Map Comparison S Two values R A = Z value from depth map at fragment’s light XY position R B = Z value of fragment’s XYZ light position S If B is greater than A, then there must be something closer to the light than the fragment R then the fragment is shadowed S If A and B are approximately equal, the fragment is lit

Shadow Mapping in 2D (1)

The A < B shadowed fragment case depth map image plane

depth map Z = A light source eye position

eye view image plane, a.k.a. the frame buffer fragment’s light Z = B

CS 4451 Fall 2002 22 Shadows

Shadow Mapping in 2D (2)

The A ≅ B unshadowed fragment case depth map image plane

depth map Z = A light source eye position

eye view image plane, a.k.a. the frame buffer fragment’s light Z = B

Visualizing the Shadow Mapping Technique (1)

T A fairly complex scene with shadows

the point light source

CS 4451 Fall 2002 23 Shadows

Visualizing the Shadow Mapping Technique (2)

T Compare with and without shadows

with shadows without shadows

Visualizing the Shadow Mapping Technique (3)

T The scene from the light’s point-of-view

FYI: from the eye’s point-of-view again

CS 4451 Fall 2002 24 Shadows

Visualizing the Shadow Mapping Technique (4)

T The depth buffer from the light’s point-of-view

FYI: from the light’s point-of-view again

Visualizing the Shadow Mapping Technique (5)

T Projecting the depth map onto the eye’s view

FYI: depth map for light’s point-of-view again

CS 4451 Fall 2002 25 Shadows

Visualizing the Shadow Mapping Technique (6)

T Projecting light’s planar distance onto eye’s view

Visualizing the Shadow Mapping Technique (6)

T 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

CS 4451 Fall 2002 26 Shadows

Visualizing the Shadow Mapping Technique (7)

T Scene with shadows

Notice how Notice how specular curved highlights surfaces cast never appear shadows on in shadows each other

More Examples

T Smooth surfaces with object self-shadowing

Note object self-shadowing

CS 4451 Fall 2002 27 Shadows

More Examples

T Complex objects all shadow

More Examples

T Even the floor casts shadow

Note shadow leakage due to infinitely thin floor

Could be fixed by giving floor thickness

CS 4451 Fall 2002 28 Shadows

Luxo Jr. in Real-time using Shadow Mapping

T Steve Jobs at MacWorld 2001 Japan shows this on a Mac with OpenGL using hardware shadow mapping

Luxo Jr. Demo Details

T Luxo Jr. has two animated lights and one overhead light S Three shadow maps dynamically generated per frame T Complex geometry (cords and lamp arms) all correctly shadowed T User controls the view, shadowing just works T Real-time Luxo Jr. is technical triumph for OpenGL T Only available in OpenGL.

(Images are from web cast video of Apple’s MacWorld Japan announcement.)

CS 4451 Fall 2002 29