Robust Stenciled Shadow Volumes

61
Robust Stenciled Shadow Volumes Cass Everitt & Mark J. Kilgard Graphics/Visualization Seminar to the Center for Computational Visualization University of Texas March 24, 2003

description

Cass Everitt & Mark Kilgard; March 24, 2003; Graphics/Visualization Seminar to the Center for Computational Visualization, University of Texas

Transcript of Robust Stenciled Shadow Volumes

Page 1: Robust Stenciled Shadow Volumes

Robust Stenciled Shadow VolumesCass Everitt & Mark J. Kilgard

Graphics/Visualization Seminar to theCenter for Computational Visualization

University of TexasMarch 24, 2003

Page 2: Robust Stenciled Shadow Volumes

Stenciled Shadow Volumes in Practice

Notice the properself-shadowing!

Page 3: Robust Stenciled Shadow Volumes

Shadow Volume Basics

Shadowingobject

Lightsource Shadow

volume(infinite extent)

A shadow volume issimply the half-space definedby a light source and a shadowing object.

Page 4: Robust Stenciled Shadow Volumes

Shadow Volume Basics (2)

Partiallyshadowed object

Surface insideshadow volume(shadowed)

Surface outsideshadow volume(illuminated)

Simple rule:samples within ashadow volumeare in shadow.

Page 5: Robust Stenciled Shadow Volumes

Visualizing Shadow Volumes in 3D

Occluders and light source cast out a shadow volume

Objects within the volume should be shadowedLight

source

Scene with shadows from an NVIDIA logo casting a shadow

volume

Visualization of the shadow volume

Page 6: Robust Stenciled Shadow Volumes

Shadow Volume Advantages

Omni-directional approachNot just spotlight frustums as with shadow maps

Automatic self-shadowingEverything can shadow everything, including selfWithout shadow acne artifacts as with shadow maps

Window-space shadow determinationShadows accurate to a pixelOr sub-pixel if multisampling is available

Required stencil buffer broadly supported todayOpenGL support since version 1.0 (1991)Direct3D support since DX6 (1998)

Page 7: Robust Stenciled Shadow Volumes

Shadow Volume Disadvantages

Ideal light sources onlyLimited to local point and directional lightsNo area light sources for soft shadows

Requires polygonal models with connectivityModels must be closed (2-manifold)Models must be free of non-planar polygons

Silhouette computations are requiredCan burden CPUParticularly for dynamic scenes

Inherently multi-pass algorithmConsumes lots of GPU fill rate

Page 8: Robust Stenciled Shadow Volumes

Render scene to initialize depth bufferDepth values indicate the closest visible fragments

Use a stencil enter/leave counting approachDraw shadow volume twice using face culling

1st pass: render front faces and increment when depth test passes2nd pass: render back faces and decrement when depth test passes

Don’t update depth or colorAfterward, pixel’s stencil is non-zero if pixel in shadow, and zero if illuminated

Counting Enter/Leaves With aStencil Buffer (Zpass approach)

Page 9: Robust Stenciled Shadow Volumes

Visualizing the Stencil Buffer Counts

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

Shadowed scene Stencil buffer contents

GLUT shadowvol example credit: Tom McReynolds

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

Page 10: Robust Stenciled Shadow Volumes

Why Eye-to-Object Stencil Counting Approach Works

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Page 11: Robust Stenciled Shadow Volumes

Illuminated,Behind Shadow Volumes (Zpass)

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Unshadowedobject

+ ---+ +

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

Page 12: Robust Stenciled Shadow Volumes

Shadowed, Nested in Shadow Volumes (Zpass)

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadowedobject

+ -+ +

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

Page 13: Robust Stenciled Shadow Volumes

Illuminated, In Front of Shadow Volumes (Zpass)

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadowedobject

Shadow Volume Count = 0 (no depth tests pass)

Page 14: Robust Stenciled Shadow Volumes

Nested Shadow VolumesStencil Counts Beyond One

Shadowed scene Stencil buffer contents

green = stencil value of 0red = stencil value of 1darker reds = stencil value > 1

Page 15: Robust Stenciled Shadow Volumes

Animation of Stencil Buffer Updatesfor a Single Light’s Shadow Volumes

Fully shaded scene

Final stencil state

Every frame is 5 additional stencil shadow volume polygon updates. Note how various intermediate stencil values do not reflect the final state.

Page 16: Robust Stenciled Shadow Volumes

Problem Created byNear Clip Plane (Zpass)

zero

zero

+1+1+2

+2+3

Near clipplane

Far clipplane

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

Page 17: Robust Stenciled Shadow Volumes

Render scene to initialize depth bufferDepth values indicate the closest visible fragments

Use a stencil enter/leave counting approachDraw shadow volume twice using face culling

1st pass: render back faces and increment when depth test fails2nd pass: render front faces and decrement when depth test fails

Don’t update depth or colorAfterward, pixel’s stencil is non-zero if pixel in shadow, and zero if illuminated

Alternative Approach: Zfail

Page 18: Robust Stenciled Shadow Volumes

Illuminated,Behind Shadow Volumes (Zfail)

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Unshadowedobject

Shadow Volume Count = 0 (zero depth tests fail)

Page 19: Robust Stenciled Shadow Volumes

Shadowed, Nested inShadow Volumes (Zfail)

Shadowing objectLightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadow Volume Count = +1+1 = 2

+ +

Shadowedobject

Page 20: Robust Stenciled Shadow Volumes

Illuminated, In Front of Shadow Volumes (Zfail)

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadowedobject

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

- +- - + +

Page 21: Robust Stenciled Shadow Volumes

Problem Created byFar Clip Plane (Zfail)

zero

Near clipplane

Far clipplane

Missed shadow volume intersection due to far clip plane clipping; leads to mistaken count

+1

+1 +2+2+3

zero

Page 22: Robust Stenciled Shadow Volumes

Problem Solved byEliminating Far Clip

zero

+1

+1 +2+2+3

Near clipplane

Page 23: Robust Stenciled Shadow Volumes

Avoiding Far Plane Clipping

Usual practice for perspective GL projection matrixUse glFrustum (or gluPerspective)Requires two values for near & far clip planes

Near plane’s distance from the eyeFar plane’s distance from the eye

Assumes a finite far plane distanceAlternative projection matrix

Still requires near plane’s distance from the eyeBut assume far plane is at infinity

What is the limit of the projection matrix whenthe far plane distance goes to infinity?

Page 24: Robust Stenciled Shadow Volumes

Standard glFrustum Projection Matrix

Only third row depends on Far and Near

0100

200

020

002

NearFarNearFar

NearFarNearFar

BottomTopBottomTop

BottomTopNear

LeftRightLeftRight

LeftRightNear

P

Page 25: Robust Stenciled Shadow Volumes

Limit of glFrustum Matrix asFar Plane is Moved to Infinity

First, second, and fourth rows are the same as in PBut third row no longer depends on Far

Effectively, Far equals ∞

01002100

020

002

lim

NearBottomTopBottomTop

BottomTopNear

LeftRightLeftRight

LeftRightNear

Far infPP

Page 26: Robust Stenciled Shadow Volumes

Verifying Pinf Will Not ClipInfinitely Far Away Vertices (1)

What is the most distant possible vertex in front of the eye?

Ok to use homogeneous coordinatesOpenGL convention looks down the negative Z axisSo most distant vertex is (0,0,-D,0) where D>0

Transform (0,0,-D,0) to window spaceIs such a vertex clipped by Pinf?No, it is not clipped, as explained on the next slide

Page 27: Robust Stenciled Shadow Volumes

Verifying Pinf Will Not ClipInfinitely Far Away Vertices (2)

Transform eye-space (0,0,-D,0) to clip-space

0

00

01002100

020

002

DNear

BottomTopBottomTop

BottomTopNear

LeftRightLeftRight

LeftRightNear

wzyx

DDyx

c

c

c

c

c

c

15.05.05.05.0

DD

wzzc

cw

Then, assuming glDepthRange(0,1), transform clip-space position to window-space position

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)

Page 28: Robust Stenciled Shadow Volumes

Robust Shadow Volumes sans Near (or Far) Plane Capping

Use Zfail Stenciling ApproachMust render geometry to close shadow volume extrusion on the model and at infinity (explained later)

Use the Pinf Projection MatrixNo worries about far plane clippingLosses some depth buffer precision (but not much)

Draw the infinite vertices of the shadow volume using homogeneous coordinates (w=0)

Page 29: Robust Stenciled Shadow Volumes

Rendering Closed, but Infinite,Shadow Volumes

To be robust, the shadow volume geometry must be closed, even at infinityThree sets of polygons close the shadow volume1. Possible silhouette edges extruded to infinity away from

the light2. All of the occluder’s back-facing (w.r.t. the light) triangles

projected away from the light to infinity3. All of the occluder’s front-facing (w.r.t. the light) triangles

We assume the object vertices and light position are homogeneous coordinates, i.e. (x,y,z,w)

Where w0

Page 30: Robust Stenciled Shadow Volumes

1st Set ofShadow Volume Polygons

AssumingA and B are vertices of an occluder model’s possible silhouette edgeAnd L is the light position

For all A and B on silhouette edges of the occluder model, render the quad

What is a possible silhouette edge?One polygon sharing an edge faces toward LOther faces away from L

0,,,0,,,

,,,,,,

wzwzwywywxwx

wzwzwywywxwx

wzyx

wzyx

BLLBBLLBBLLBALLAALLAALLA

AAAABBBB

Homogenous vector differences

Page 31: Robust Stenciled Shadow Volumes

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

Page 32: Robust Stenciled Shadow Volumes

2nd and 3rd Set ofShadow Volume Polygons

2nd set of polygonsAssuming A, B, and C are each vertices of occluder model’s back-facing triangles w.r.t. light position LThese vertices are effectively directions (w=0)

3rd set of polygonsAssuming A, B, and C are each vertices of occluder model’s front-facing triangles w.r.t. light position L

0,,,0,,,0,,,

wzwzwywywxwx

wzwzwywywxwx

wzwzwywywxwx

CLLCCLLCCLLCBLLBBLLBBLLBALLAALLAALLA

Homogenous vector differences

wzyx

wzyx

wzyx

CCCCBBBBAAAA

,,,,,,,,,

Page 33: Robust Stenciled Shadow Volumes

Complete Stenciled Shadow Volume Rendering Technique

See our paper “Practical and Robust Stenciled Shadow Volumes for Hardware-Accelerated Rendering”

In the accompanying course notesAnd on-line at developer.nvidia.com

Paper has pseudo-code for rendering procedureOpenGL state settings & rendering commandsSupports multiple per-vertex lightsAssumes application computes object-space determination of occluder model’s polygons orientation w.r.t. each light

Page 34: Robust Stenciled Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (1)

1. Models must be composed of triangles only (avoiding non-planar polygons)

2. Models must be closed (2-manifold) and have a consistent winding order

Bergeron [’86] approach could be used to handle “open” models if necessary

3. Homogeneous object coordinates are permitted, assuming w0

If not, (x, y, z, -1) = (-x, -y, -z, 1)4. Ideal light sources only

Directional or positional, assuming w0

Page 35: Robust Stenciled Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (2)

5. Connectivity information for occluding models must be available

So silhouette edges w.r.t. light positions can be determined at shadow volume construction time

6. Projection matrix must be perspectiveNot orthographicNV_depth_clamp extension provides orthographic support (more later)

7. Render must guarantee “watertight” rasterizationNo double hitting pixels at shared polygon edgesNo missed pixels at shared polygon edges

Page 36: Robust Stenciled Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (3)

8. Enough stencil bitsN stencil bits where 2N is greater than the maximum shadow depth count ever encounteredScene dependent8-bits is usually quite adequate & what all recent stencil hardware providesWrapping stencil increment/decrement operations (i.e. OpenGL’s EXT_stencil_wrap) permit deeper shadow counts, modulo aliasing with zeroRealize that shadow depths > 255 imply toomuch fill rate for interactive applications

Page 37: Robust Stenciled Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (4)

9. Rendering features provided by OpenGL 1.0 or DirectX 6 (or subsequent versions)

Transformation & clipping of homogenous positionsFront- and back-face cullingMasking color and depth buffer writesDepth buffering (i.e. conventional Z-buffering)Stencil-testing support

In practice, these are quite reasonable requirements for nearly any polygonal-based 3D game or application

Page 38: Robust Stenciled Shadow Volumes

Our Approach in Practice (1)

Scene with shadows. Yellow light is embedded in the green three-holed object. Pinf is used for all the following scenes.

Same scene visualizingthe shadow volumes.

Page 39: Robust Stenciled Shadow Volumes

Our Approach in Practice (2)Details worth noting . . .

Fine details: Shadowsof the A, N, and T letters on the knight’s armor and shield.

Hard case: The shadow volume from the front-facing hole would definitely intersectthe near clip plane.

Page 40: Robust Stenciled Shadow Volumes

Our Approach in Practice (3)

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

Shadow volumes from the alternate view.

Page 41: Robust Stenciled Shadow Volumes

Our Approach in Practice (4)

Clip-space view. Original view’s scene seen from clip space. The back plane is “at infinity” with very little effective depth precision near infinity.

Clip-space view of shadow volumes. Back-facing triangles w.r.t. light are seen projectedonto far plane at infinity.

Page 42: Robust Stenciled Shadow Volumes

Another Example (1)

Original eye’s view. Again, yellow light is embedded in the green three-holed object. Pinf is used for all the following scenes.

Eye-space view of previous eye’s view. Clipped to the previous eye’s Pinf viewfrustum. Shows knight’sprojection to infinity.

Page 43: Robust Stenciled Shadow Volumes

Another Example (2)

Clip-space view of previous eye’s view. Shows shadow volume closed at infinity and other shadow volume’s intersection with the near clip plane.

Original eye’s far clip plane

Original eye’s near clip plane

Page 44: Robust Stenciled Shadow Volumes

Stenciled Shadow Volumes &Multiple Lights

Three colored lights. Diffuse/specular bump mapped animated characters with shadows. 34 fps on GeForce4 Ti 4600;80+ fps for one light.

Page 45: Robust Stenciled Shadow Volumes

Stenciled Shadow Volumes forSimulating Soft Shadows

Cluster of 12 dim lights approximating an area light source. Generates a soft shadow effect; careful about banding. 8 fps on GeForce4 Ti 4600.

The cluster of point lights.

Page 46: Robust Stenciled Shadow Volumes

Shadows in a Real Game Scene

Abducted game images courtesyJoe Riedel at Contraband Entertainment

Page 47: Robust Stenciled Shadow Volumes

Scene’s VisibleGeometric Complexity

Primary light source location

Wireframe shows geometric complexity of visible geometry

Page 48: Robust Stenciled Shadow Volumes

Blow-up of Shadow Detail

Notice cable shadows on player model

Notice player’s own shadow on floor

Page 49: Robust Stenciled Shadow Volumes

Scene’s Shadow VolumeGeometric Complexity

Wireframe shows geometric complexity of shadow volume geometry

Shadow volume geometry projects away from the light source

Page 50: Robust Stenciled Shadow Volumes

Visible Geometry vs.Shadow Volume Geometry

<<

Visible geometry Shadow volume geometry

Typically, shadow volumes generate considerably more pixel updates than visible geometry

Page 51: Robust Stenciled Shadow Volumes

Other Example Scenes (1 of 2)

Visible geometry

Shadow volume geometry

Dramatic chase scene with shadows

Abducted game images courtesyJoe Riedel at Contraband Entertainment

Page 52: Robust Stenciled Shadow Volumes

Other Example Scenes (2 of 2)

Visible geometry

Shadow volume geometry

Scene with multiple light sources

Abducted game images courtesyJoe Riedel at Contraband Entertainment

Page 53: Robust Stenciled Shadow Volumes

Situations WhenShadow Volumes Are Too Expensive

Chain-link fence’s shadow appears on truck & ground with shadow maps

Chain-link fence is shadow volume nightmare!

Fuel game image courtesy Nathan d’Obrenan at Firetoad Software

Page 54: Robust Stenciled Shadow Volumes

Shadow Volumes vs. Shadow Maps

Shadow mapping via projective texturingThe other prominent hardware-accelerated shadow techniqueStandard part of OpenGL 1.4

Shadow mapping advantagesRequires no explicit knowledge of object geometryNo 2-manifold requirements, etc.View independent

Shadow mapping disadvantagesSampling artifactsNot omni-directional

Page 55: Robust Stenciled Shadow Volumes

Stenciled Shadow Volumes Optimizations

Fill Rate OptimizationsDynamic Zpass vs. Zfail determinationBounds

Culling OptimizationsPortal-based cullingOccluder and cap culling

Silhouette Determination OptimizationsEfficient data structures for static occluder & dynamic lightCache shadow volumes, update only when necessarySimplified occluder geometry

Shadow Volume Rendering OptimizationsVertex programs for shadow volume renderingTwo-sided stencil testingDirectional lights

Page 56: Robust Stenciled Shadow Volumes

Shadow Volume History (1)

Invented by Frank Crow [’77]Software rendering scan-line approach

Brotman and Badler [’84] Software-based depth-buffered approachUsed lots of point lights to simulate soft shadows

Pixel-Planes [Fuchs, et. al. ’85] hardwareFirst hardware approachPoint within a volume, rather than ray intersection

Bergeron [’96] generalizationsExplains how to handle open modelsAnd non-planar polygons

Page 57: Robust Stenciled Shadow Volumes

Shadow Volume History (2)

Fournier & Fussell [’88] theoryProvides theory for shadow volume counting approach within a frame buffer

Akeley & Foran invent the stencil bufferIRIS GL functionality, later made part of OpenGL 1.0Patent filed in ’92

Heidmann [IRIS Universe article, ’91]IRIS GL stencil buffer-based approach

Deifenbach’s thesis [’96]Used stenciled volumes in multi-pass framework

Page 58: Robust Stenciled Shadow Volumes

Shadow Volume History (3)

Dietrich slides [March ’99] at GDCProposes Zfail based stenciled shadow volumes

Kilgard whitepaper [March ’99] at GDCInvert approach for planar cut-outs

Bilodeau slides [May ’99] at Creative seminarProposes way around near plane clipping problemsReverses depth test function to reverse stencil volume ray intersection sense

Carmack [unpublished, early 2000]First detailed discussion of the equivalence ofZpass and Zfail stenciled shadowvolume methods

Page 59: Robust Stenciled Shadow Volumes

Shadow Volume History (4)

Kilgard [2001] at GDC and CEDEC JapanProposes Zpass capping scheme

Project back-facing (w.r.t. light) geometry to the near clip plane for cappingEstablishes near plane ledge for crack-freenear plane capping

Applies homogeneous coordinates (w=0) for rendering infinite shadow volume geometryRequires much CPU effort for cappingNot totally robust because CPU and GPU computations will not match exactly,resulting in cracks

Page 60: Robust Stenciled Shadow Volumes

Shadow Volume History (5) –Our Contribution

Everitt & Kilgard [2002] Integrate Multiple Ideas into a robust solution

Dietrich, Bilodeau, and Carmack’s Zfail approachKilgard’s homogeneous coordinates (w=0) for rendering infinite shadow volume geometrySomewhat-obscure [Blinn ’93] infinite far plane projection matrix formulationDirectX 6’s wrapping stencil increment & decrement

OpenGL’s EXT_stencil_wrap extensionNVIDIA Hardware Enhancements

Depth clamping [2001]: better depth precisionTwo-sided stencil testing [2002]: performanceDepth/stencil-only hyper-rasterization: performanceDepth bounds test [2003]: culling support

Page 61: Robust Stenciled Shadow Volumes

The Future

Expect many games to have dynamic “everything shadows everything” shadowsExpect hardware vendors to optimize hardware for this technique

The “Doom3 effect”Expect research to optimize shadow volume culling/rendering/etc.Other applications

Hardware-accelerated collision detectionComputational Geometry

Problems related to the visibility problem