Shadow Mapping and Shadow Volumes

78
Shadow Mapping and Shadow Mapping and Shadow Volumes Shadow Volumes Mark J. Kilgard Mark J. Kilgard NVIDIA Corporation NVIDIA Corporation SIGGRAPH 2002 Course 31: Interactive Geometric Computations Using Graphics Hardware

description

SIGGRAPH 2002 Course 31: Interactive Geometric Computations Using Graphics Hardware. Shadow Mapping and Shadow Volumes. Mark J. Kilgard NVIDIA Corporation. Shadows as a Shading Computation. Shadow determination problem - PowerPoint PPT Presentation

Transcript of Shadow Mapping and Shadow Volumes

Page 1: Shadow Mapping and Shadow Volumes

Shadow Mapping andShadow Mapping andShadow Volumes Shadow Volumes

Shadow Mapping andShadow Mapping andShadow Volumes Shadow Volumes

Mark J. KilgardMark J. Kilgard

NVIDIA CorporationNVIDIA CorporationMark J. KilgardMark J. Kilgard

NVIDIA CorporationNVIDIA Corporation

SIGGRAPH 2002 Course 31: Interactive Geometric Computations Using Graphics Hardware

Page 2: Shadow Mapping and Shadow Volumes

Shadows as a Shading ComputationShadows as a Shading Computation• Shadow determination problemShadow determination problem

• Visual formulation of the problem

“What is in the shadow of a given light source?”

• Standard applicationStandard application• Shading, don’t allow an obscured light to contribute to

a shadowed surface’s illumination

• Shadow determination problemShadow determination problem• Visual formulation of the problem

“What is in the shadow of a given light source?”

• Standard applicationStandard application• Shading, don’t allow an obscured light to contribute to

a shadowed surface’s illumination

Page 3: Shadow Mapping and Shadow Volumes

Shadows as a Geometric Computation

Shadows as a Geometric Computation• View-independent occlusion determinationView-independent occlusion determination

• Geometric formulation of the problem

“What is not visible (obscured) from an arbitrary viewpoint?”

• Depth buffer solves the problem (to pixel precision)– But only for the rendering viewpoint– What about for alternate viewpoints?

• Fundamentally, the visual & geometric Fundamentally, the visual & geometric problems are the sameproblems are the same

• View-independent occlusion determinationView-independent occlusion determination• Geometric formulation of the problem

“What is not visible (obscured) from an arbitrary viewpoint?”

• Depth buffer solves the problem (to pixel precision)– But only for the rendering viewpoint– What about for alternate viewpoints?

• Fundamentally, the visual & geometric Fundamentally, the visual & geometric problems are the sameproblems are the same

Page 4: Shadow Mapping and Shadow Volumes

Example Geometric ProblemExample Geometric Problem• Guard posts in an art museumGuard posts in an art museum

• Are there enough guard posts so that all wall regions in the art museum can be observed by at least one stationary guard?

• Same as “Are any wall regions in shadow of all guard posts?”

• Guard posts in an art museumGuard posts in an art museum• Are there enough guard posts so that all wall regions

in the art museum can be observed by at least one stationary guard?

• Same as “Are any wall regions in shadow of all guard posts?”

Guards

Non-visible regions All regions visible

Page 5: Shadow Mapping and Shadow Volumes

Shadow MappingShadow Mapping

• Image-space shadow determinationImage-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– Basic shadowing technique for Toy Story, etc.

• Image-space shadow determinationImage-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– Basic shadowing technique for Toy Story, etc.

Page 6: Shadow Mapping and Shadow Volumes

Shadow MappingReferencesShadow MappingReferences

• Important SIGGRAPH papersImportant 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

• Important SIGGRAPH papersImportant 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

Page 7: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (1)Visualizing the ShadowMapping Technique (1)

• A fairly complex scene with shadowsA fairly complex scene with shadows• A fairly complex scene with shadowsA fairly complex scene with shadows

the pointthe pointlight sourcelight source

Page 8: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (2)Visualizing the ShadowMapping Technique (2)

• Compare with and without shadowsCompare with and without shadows• Compare with and without shadowsCompare with and without shadows

with shadowswith shadows without shadowswithout shadows

Page 9: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (3)Visualizing the ShadowMapping Technique (3)

• The scene from the light’s point-of-viewThe scene from the light’s point-of-view• The scene from the light’s point-of-viewThe scene from the light’s point-of-view

FYI: from theFYI: from theeye’s point-of-vieweye’s point-of-viewagainagain

Page 10: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (4)Visualizing the ShadowMapping Technique (4)

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

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

FYI: from theFYI: from thelight’s point-of-viewlight’s point-of-viewagainagain

Page 11: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (5)

• Project the depth map onto the eye’s Project the depth map onto the eye’s viewview

FYI: depth map forFYI: depth map forlight’s point-of-viewlight’s point-of-viewagainagain

Page 12: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (6)Visualizing the ShadowMapping Technique (6)

• Project light’s planar distance onto eye’s viewProject light’s planar distance onto eye’s view• Project light’s planar distance onto eye’s viewProject light’s planar distance onto eye’s view

Page 13: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (6)Visualizing the ShadowMapping Technique (6)

• Comparing light distance to light depth map Comparing light distance to light depth map • Comparing light distance to light depth map Comparing light distance to light depth map

Green is Green is where the where the

light planar light planar distance and distance and

the light the light depth map depth map

are are approximatelapproximatel

y equaly equal

Non-green Non-green is where is where shadows shadows should beshould be

Page 14: Shadow Mapping and Shadow Volumes

Visualizing the ShadowMapping Technique (7)Visualizing the ShadowMapping Technique (7)

• Complete scene with shadowsComplete scene with shadows• Complete scene with shadowsComplete scene with shadows

Notice how Notice how specular specular

highlights highlights never never

appear in appear in shadowsshadows

Notice how Notice how curved curved surfaces surfaces cast cast shadows on shadows on each othereach other

Page 15: Shadow Mapping and Shadow Volumes

Shadow Mapping Hardware Support (1)Shadow Mapping Hardware Support (1)

• OpenGL 1.4 has shadow mapping support standardOpenGL 1.4 has shadow mapping support standard• Now part of OpenGL’s core functionality

• Embraces official ARB-standard shadow mapping Embraces official ARB-standard shadow mapping extensions extensions • Approved February 2002!

• ARB_depth_texture – adds depth texture formats

• ARB_shadow – adds “percentage closer” filtering for depth textures

• The two extensions are used together

• Based on prior proven SGI proprietary extensionsBased on prior proven SGI proprietary extensions• SGIX_depth_texture & SGIX_shadow

• OpenGL 1.4 has shadow mapping support standardOpenGL 1.4 has shadow mapping support standard• Now part of OpenGL’s core functionality

• Embraces official ARB-standard shadow mapping Embraces official ARB-standard shadow mapping extensions extensions • Approved February 2002!

• ARB_depth_texture – adds depth texture formats

• ARB_shadow – adds “percentage closer” filtering for depth textures

• The two extensions are used together

• Based on prior proven SGI proprietary extensionsBased on prior proven SGI proprietary extensions• SGIX_depth_texture & SGIX_shadow

Page 16: Shadow Mapping and Shadow Volumes

Shadow Mapping Hardware Support (2)Shadow Mapping Hardware Support (2)

• SGIX_depth_texture & SGIX_shadow supportSGIX_depth_texture & SGIX_shadow support• SGI’s RealityEngine & InfiniteReality

• Brian Paul’s Mesa3D OpenGL work-alike

• NVIDIA’s GeForce3, GeForce4 Ti, andQuadro 4 XGL

– Software emulation for GeForce1 & 2

• ARB extensions now implementedARB extensions now implemented• Latest NVIDIA drivers and Mesa 4.0

• OpenGL 1.4 drivers very soon!OpenGL 1.4 drivers very soon!• Way more detail in course notesWay more detail in course notes

• SGIX_depth_texture & SGIX_shadow supportSGIX_depth_texture & SGIX_shadow support• SGI’s RealityEngine & InfiniteReality

• Brian Paul’s Mesa3D OpenGL work-alike

• NVIDIA’s GeForce3, GeForce4 Ti, andQuadro 4 XGL

– Software emulation for GeForce1 & 2

• ARB extensions now implementedARB extensions now implemented• Latest NVIDIA drivers and Mesa 4.0

• OpenGL 1.4 drivers very soon!OpenGL 1.4 drivers very soon!• Way more detail in course notesWay more detail in course notes

Page 17: Shadow Mapping and Shadow Volumes

Stenciled Shadow VolumesStenciled Shadow Volumes

Notice the properself-shadowing!

Page 18: Shadow Mapping and Shadow Volumes

2D Cutaway of a Shadow Volume2D Cutaway of a Shadow Volume

Shadowingobject

Partiallyshadowed object

Lightsource

Eye position(note that shadows are independent of the eye position)

Surface insideshadow volume(shadowed)

Surface outsideshadow volume(illuminated)

Shadowvolume(infinite extent)

Page 19: Shadow Mapping and Shadow Volumes

Shadow Volume AdvantagesShadow Volume Advantages• Omni-directional approachOmni-directional approach

• Not just spotlight frustums as with shadow maps

• Automatic self-shadowingAutomatic self-shadowing• Everything can shadow everything, including self

• Without shadow acne artifacts as with shadow maps

• Window-space shadow determinationWindow-space shadow determination• Shadows accurate to a pixel

• Or sub-pixel if multisampling is available

• Required stencil buffer broadly supported Required stencil buffer broadly supported todaytoday• OpenGL support since version 1.0 (1991)

• Direct3D support since DX6 (1998)

• Omni-directional approachOmni-directional approach• Not just spotlight frustums as with shadow maps

• Automatic self-shadowingAutomatic self-shadowing• Everything can shadow everything, including self

• Without shadow acne artifacts as with shadow maps

• Window-space shadow determinationWindow-space shadow determination• Shadows accurate to a pixel

• Or sub-pixel if multisampling is available

• Required stencil buffer broadly supported Required stencil buffer broadly supported todaytoday• OpenGL support since version 1.0 (1991)

• Direct3D support since DX6 (1998)

Page 20: Shadow Mapping and Shadow Volumes

Shadow Volume DisadvantagesShadow Volume Disadvantages• Ideal light sources onlyIdeal light sources only

• Limited to local point and directional lights

• No area light sources for soft shadows

• Requires polygonal models with Requires polygonal models with connectivityconnectivity• Models must be closed (2-manifold)

• Models must be free of non-planar polygons

• Silhouette computations are requiredSilhouette computations are required• Can burden CPU

• Particularly for dynamic scenes

• Inherently multi-pass algorithmInherently multi-pass algorithm• Consumes lots of GPU fill rateConsumes lots of GPU fill rate

• Ideal light sources onlyIdeal light sources only• Limited to local point and directional lights

• No area light sources for soft shadows

• Requires polygonal models with Requires polygonal models with connectivityconnectivity• Models must be closed (2-manifold)

• Models must be free of non-planar polygons

• Silhouette computations are requiredSilhouette computations are required• Can burden CPU

• Particularly for dynamic scenes

• Inherently multi-pass algorithmInherently multi-pass algorithm• Consumes lots of GPU fill rateConsumes lots of GPU fill rate

Page 21: Shadow Mapping and Shadow Volumes

Visualizing Shadow Volumes in 3DVisualizing Shadow Volumes in 3D

Lightsource

Scene with shadows from an NVIDIA logo

casting a shadow volume

Visualization of the shadow volume

• Occluders and light source cast out a shadow Occluders and light source cast out a shadow volumevolume• Objects within the volume should be Objects within the volume should be

shadowedshadowed

• Occluders and light source cast out a shadow Occluders and light source cast out a shadow volumevolume• Objects within the volume should be Objects within the volume should be

shadowedshadowed

Page 22: Shadow Mapping and Shadow Volumes

Visualizing the Stencil Buffer CountsVisualizing 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 23: Shadow Mapping and Shadow Volumes

• Render scene to initialize depth bufferRender scene to initialize depth buffer• Depth values indicate the closest visible fragments

• Use a stencil enter/leave counting approachUse 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

• Don’t update depth or color

• Afterward, pixel’s stencil is non-zero if pixel Afterward, pixel’s stencil is non-zero if pixel in shadow, and zero if illuminatedin shadow, and zero if illuminated

• Render scene to initialize depth bufferRender scene to initialize depth buffer• Depth values indicate the closest visible fragments

• Use a stencil enter/leave counting approachUse 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

• Don’t update depth or color

• Afterward, pixel’s stencil is non-zero if pixel Afterward, pixel’s stencil is non-zero if pixel in shadow, and zero if illuminatedin shadow, and zero if illuminated

Counting Enter/Leaves With aStencil Buffer (Zpass approach)

Counting Enter/Leaves With aStencil Buffer (Zpass approach)

Page 24: Shadow Mapping and Shadow Volumes

Why Eye-to-Object Stencil Counting Approach WorksWhy Eye-to-Object Stencil Counting Approach Works

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Page 25: Shadow Mapping and Shadow Volumes

Illuminated,Behind Shadow Volumes (Zpass)

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 26: Shadow Mapping and Shadow Volumes

Shadowed, Nested in Shadow Volumes (Zpass)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 27: Shadow Mapping and Shadow Volumes

Illuminated, In Front of Shadow Volumes (Zpass)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 passe)

Page 28: Shadow Mapping and Shadow Volumes
Page 29: Shadow Mapping and Shadow Volumes

Problems Created byNear Clip Plane (Zpass)Problems 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 30: Shadow Mapping and Shadow Volumes

• Render scene to initialize depth bufferRender scene to initialize depth buffer• Depth values indicate the closest visible fragments

• Use a stencil enter/leave counting Use a stencil enter/leave counting approachapproach• 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

• Don’t update depth or color

• Afterward, pixel’s stencil is non-zero Afterward, pixel’s stencil is non-zero if pixel in shadow, and zero if if pixel in shadow, and zero if illuminatedilluminated

• Render scene to initialize depth bufferRender scene to initialize depth buffer• Depth values indicate the closest visible fragments

• Use a stencil enter/leave counting Use a stencil enter/leave counting approachapproach• 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

• Don’t update depth or color

• Afterward, pixel’s stencil is non-zero Afterward, pixel’s stencil is non-zero if pixel in shadow, and zero if if pixel in shadow, and zero if illuminatedilluminated

Alternative Approach: ZfailAlternative Approach: Zfail

Page 31: Shadow Mapping and 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 32: Shadow Mapping and Shadow Volumes

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadow Volume Count = +1+1 = 2

+ +

Shadowedobject

Shadowed, Nested inShadow Volumes (Zfail)

Page 33: Shadow Mapping and Shadow Volumes

Shadowing object Lightsource

Eyeposition

zero

zero

+1

+1+2 +2

+3

Shadowedobject

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

- +- - + +

Illuminated, In Front of Shadow Volumes (Zfail)

Page 34: Shadow Mapping and Shadow Volumes

Zfail versus Zpass Comparison (1)Zfail versus Zpass Comparison (1)• When stencil increment/decrements When stencil increment/decrements

occuroccur•Zpass: on depth test passZpass: on depth test pass•Zfail: on depth test failZfail: on depth test fail

• Increment onIncrement on•Zpass: front facesZpass: front faces•Zfail: back facesZfail: back faces

• Decrement onDecrement on•Zpass: front facesZpass: front faces•Zfail: back facesZfail: back faces

• When stencil increment/decrements When stencil increment/decrements occuroccur•Zpass: on depth test passZpass: on depth test pass•Zfail: on depth test failZfail: on depth test fail

• Increment onIncrement on•Zpass: front facesZpass: front faces•Zfail: back facesZfail: back faces

• Decrement onDecrement on•Zpass: front facesZpass: front faces•Zfail: back facesZfail: back faces

Page 35: Shadow Mapping and Shadow Volumes

Zfail versus Zpass Comparison (2)Zfail versus Zpass Comparison (2)• Both cases order passes based stencil Both cases order passes based stencil

operationoperation• First, render increment pass

• Second, render decrement pass

• Why?

– Because standard stencil operations saturate– Wrapping stencil operations can avoid this

• Which clip plane creates a problemWhich clip plane creates a problem• Zpass: near clip plane

• Zfail: far clip plane

• Either way is foiled by view frustum clippingEither way is foiled by view frustum clipping• Which clip plane (front or back) changes

• Both cases order passes based stencil Both cases order passes based stencil operationoperation• First, render increment pass

• Second, render decrement pass

• Why?

– Because standard stencil operations saturate– Wrapping stencil operations can avoid this

• Which clip plane creates a problemWhich clip plane creates a problem• Zpass: near clip plane

• Zfail: far clip plane

• Either way is foiled by view frustum clippingEither way is foiled by view frustum clipping• Which clip plane (front or back) changes

Page 36: Shadow Mapping and Shadow Volumes

Insight!Insight!

• If we could avoid If we could avoid eithereither near plane near plane oror far far plane view frustum clipping, shadow plane view frustum clipping, shadow volume rendering could be robustvolume rendering could be robust

• Avoiding near plane clippingAvoiding near plane clipping• Not really possible

• Objects can always be behind you

• Moreover, depth precision in a perspective view goes to hell when the near plane is too near the eye

• Avoiding far plane clippingAvoiding far plane clipping• Perspective make it possible to render at infinity

• Depth precision is terrible at infinity, butwe just care about avoiding clipping

• If we could avoid If we could avoid eithereither near plane near plane oror far far plane view frustum clipping, shadow plane view frustum clipping, shadow volume rendering could be robustvolume rendering could be robust

• Avoiding near plane clippingAvoiding near plane clipping• Not really possible

• Objects can always be behind you

• Moreover, depth precision in a perspective view goes to hell when the near plane is too near the eye

• Avoiding far plane clippingAvoiding far plane clipping• Perspective make it possible to render at infinity

• Depth precision is terrible at infinity, butwe just care about avoiding clipping

Page 37: Shadow Mapping and Shadow Volumes

Avoiding Far Plane ClippingAvoiding Far Plane Clipping• Usual practice for perspective GL projection Usual practice for perspective GL projection

matrixmatrix• 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 matrixAlternative 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 What is the limit of the projection matrix whenwhenthe far plane distance goes to infinity?the far plane distance goes to infinity?

• Usual practice for perspective GL projection Usual practice for perspective GL projection matrixmatrix• 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 matrixAlternative 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 What is the limit of the projection matrix whenwhenthe far plane distance goes to infinity?the far plane distance goes to infinity?

Page 38: Shadow Mapping and Shadow Volumes

Standard glFrustum Projection MatrixStandard glFrustum Projection Matrix

0100

200

02

0

002

NearFar

NearFar

NearFar

NearFarBottomTop

BottomTop

BottomTop

NearLeftRight

LeftRight

LeftRight

Near

P

•Only third row depends on Far and NearOnly third row depends on Far and Near

Page 39: Shadow Mapping and Shadow Volumes

0100

2100

02

0

002

lim

NearBottomTop

BottomTop

BottomTop

NearLeftRight

LeftRight

LeftRight

Near

FarinfPP

•First, second, and fourth rows are the same as in PFirst, second, and fourth rows are the same as in P•But third row no longer depends on FarBut third row no longer depends on Far

•Effectively, Far equals Effectively, Far equals

Limit of glFrustum Matrix asFar Plane is Moved to InfinityLimit of glFrustum Matrix asFar Plane is Moved to Infinity

Page 40: Shadow Mapping and Shadow Volumes

Verifying Pinf Will Not ClipInfinitely Far Away Vertices (1)

Verifying Pinf Will Not ClipInfinitely Far Away Vertices (1)• What is the most distant possible vertex in What is the most distant possible vertex in

front of the eye?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 spaceTransform (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

• What is the most distant possible vertex in What is the most distant possible vertex in front of the eye?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 spaceTransform (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

Page 41: Shadow Mapping and Shadow Volumes

Verifying Pinf Will Not ClipInfinitely Far Away Vertices (2)

Verifying Pinf Will Not ClipInfinitely Far Away Vertices (2)• Transform eye-space (0,0,-D,0) to clip-spaceTransform eye-space (0,0,-D,0) to clip-space• Transform eye-space (0,0,-D,0) to clip-spaceTransform eye-space (0,0,-D,0) to clip-space

0

0

0

0100

2100

02

0

002

DNear

BottomTop

BottomTop

BottomTop

NearLeftRight

LeftRight

LeftRight

Near

w

z

y

x

D

D

y

x

c

c

c

c

c

c

15.05.05.05.0

D

D

w

zz

c

cw

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

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

• So So in front of eye transforms to the in front of eye transforms to the maximum window-space Z value, but is still maximum window-space Z value, but is still within the valid depth range (i.e., not within the valid depth range (i.e., not clipped)clipped)

• So So in front of eye transforms to the in front of eye transforms to the maximum window-space Z value, but is still maximum window-space Z value, but is still within the valid depth range (i.e., not within the valid depth range (i.e., not clipped)clipped)

Page 42: Shadow Mapping and Shadow Volumes

Is Pinf Bad for Depth Buffer Precision?Is Pinf Bad for Depth Buffer Precision?• Naïve but good questionNaïve but good question

• Wouldn’t moving the far clip plane to infinity waste depth buffer precision? Seems plausible, but

• Answer: Not reallyAnswer: Not really• Minimal depth buffer precision is wasted in practice

• This is due to projective nature of perspective

• Say Say NearNear is 1.0 and is 1.0 and FarFar is 100.0 (typical is 100.0 (typical situation)situation)• P would transform eye-space infinity to only 1.01 in

window space

• Only a 1% compression of the depth rangeis required to render infinity without clipping

• Moving near closer would hurt precision

• Naïve but good questionNaïve but good question• Wouldn’t moving the far clip plane to infinity waste

depth buffer precision? Seems plausible, but

• Answer: Not reallyAnswer: Not really• Minimal depth buffer precision is wasted in practice

• This is due to projective nature of perspective

• Say Say NearNear is 1.0 and is 1.0 and FarFar is 100.0 (typical is 100.0 (typical situation)situation)• P would transform eye-space infinity to only 1.01 in

window space

• Only a 1% compression of the depth rangeis required to render infinity without clipping

• Moving near closer would hurt precision

Page 43: Shadow Mapping and Shadow Volumes

Pinf Depth Precision Scale FactorPinf Depth Precision Scale Factor

• Using Pinf with Using Pinf with NearNear instead of P with instead of P with NearNear and and FarFar compresses (scales) the depth precision by compresses (scales) the depth precision by

• Using Pinf with Using Pinf with NearNear instead of P with instead of P with NearNear and and FarFar compresses (scales) the depth precision by compresses (scales) the depth precision by

Far

NearFar )(

• The compression of depth precision is uniform, The compression of depth precision is uniform, butbut the depth precision itself is already non- the depth precision itself is already non-uniform on eye-space interval [Near,Far] due uniform on eye-space interval [Near,Far] due to perspectiveto perspective• So the discrete loss of precision is more towards So the discrete loss of precision is more towards

the far clip planethe far clip plane

• Normally, Far >> Near so the scale factorNormally, Far >> Near so the scale factoris usually less than but still nearly 1.0is usually less than but still nearly 1.0• So the compression effect is minorSo the compression effect is minor

• The compression of depth precision is uniform, The compression of depth precision is uniform, butbut the depth precision itself is already non- the depth precision itself is already non-uniform on eye-space interval [Near,Far] due uniform on eye-space interval [Near,Far] due to perspectiveto perspective• So the discrete loss of precision is more towards So the discrete loss of precision is more towards

the far clip planethe far clip plane

• Normally, Far >> Near so the scale factorNormally, Far >> Near so the scale factoris usually less than but still nearly 1.0is usually less than but still nearly 1.0• So the compression effect is minorSo the compression effect is minor

Page 44: Shadow Mapping and Shadow Volumes

• Use Use ZfailZfail Stenciling Approach Stenciling Approach• Must render geometry to close shadow volume

extrusion on the model and at infinity (explained later)

• Use the PUse the Pinfinf Projection Matrix Projection Matrix• No worries about far plane clipping

• Losses some depth buffer precision (but not much)

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

• Use Use ZfailZfail Stenciling Approach Stenciling Approach• Must render geometry to close shadow volume

extrusion on the model and at infinity (explained later)

• Use the PUse the Pinfinf Projection Matrix Projection Matrix• No worries about far plane clipping

• Losses some depth buffer precision (but not much)

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

Robust Shadow Volumes sans Near (or Far) Plane Capping

Robust Shadow Volumes sans Near (or Far) Plane Capping

Page 45: Shadow Mapping and Shadow Volumes

Rendering Closed, but Infinite,Shadow Volumes

Rendering Closed, but Infinite,Shadow Volumes• To be robust, the shadow volume geometry To be robust, the shadow volume geometry

must be closed, even at infinitymust be closed, even at infinity• Three sets of polygons close the shadow Three sets of polygons close the shadow

volumevolume1. Possible silhouette edges extruded to infinity away from

the light

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

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

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

• To be robust, the shadow volume geometry To be robust, the shadow volume geometry must be closed, even at infinitymust be closed, even at infinity

• Three sets of polygons close the shadow Three sets of polygons close the shadow volumevolume1. Possible silhouette edges extruded to infinity away from

the light

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

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

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

Page 46: Shadow Mapping and Shadow Volumes

1st Set ofShadow Volume Polygons1st Set ofShadow Volume Polygons• AssumingAssuming

• A and B are vertices of an occluder model’s possible silhouette edge

• And L is the light position

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

• AssumingAssuming• A and B are vertices of an occluder model’s possible

silhouette edge

• And L is the light position

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

0,,,

0,,,

,,,

,,,

wzwzwywywxwx

wzwzwywywxwx

wzyx

wzyx

BLLBBLLBBLLB

ALLAALLAALLA

AAAA

BBBB

Homogenous vector differences

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

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

Page 47: Shadow Mapping and Shadow Volumes

Examples of Possible Silhouette Edges for Quake2 ModelsExamples 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 48: Shadow Mapping and Shadow Volumes

2nd and 3rd Set ofShadow Volume Polygons2nd and 3rd Set ofShadow Volume Polygons

• 22ndnd set of polygons set of polygons• Assuming A, B, and C are each vertices of occluder

model’s back-facing triangles w.r.t. light position L

• 22ndnd set of polygons set of polygons• Assuming A, B, and C are each vertices of occluder

model’s back-facing triangles w.r.t. light position L

0,,,

0,,,

0,,,

wzwzwywywxwx

wzwzwywywxwx

wzwzwywywxwx

CLLCCLLCCLLC

BLLBBLLBBLLB

ALLAALLAALLA

wzyx

wzyx

wzyx

CCCC

BBBB

AAAA

,,,

,,,

,,,

Homogenous vector differences

• These vertices are effectively directions (w=0)These vertices are effectively directions (w=0)

• 33rdrd set of polygons set of polygons• Assuming A, B, and C are each vertices of Assuming A, B, and C are each vertices of

occluder model’s occluder model’s frontfront-facing triangles w.r.t. -facing triangles w.r.t. light position Llight position L

• These vertices are effectively directions (w=0)These vertices are effectively directions (w=0)

• 33rdrd set of polygons set of polygons• Assuming A, B, and C are each vertices of Assuming A, B, and C are each vertices of

occluder model’s occluder model’s frontfront-facing triangles w.r.t. -facing triangles w.r.t. light position Llight position L

Page 49: Shadow Mapping and Shadow Volumes

Complete Stenciled Shadow Volume Rendering TechniqueComplete Stenciled Shadow Volume Rendering Technique

• See our paper “Practical and Robust See our paper “Practical and Robust Stenciled Shadow Volumes for Hardware-Stenciled Shadow Volumes for Hardware-Accelerated Rendering”Accelerated Rendering”• In the accompanying course notes

• And on-line at developer.nvidia.com

• Paper has pseudo-code for rendering Paper has pseudo-code for rendering procedureprocedure• OpenGL state settings & rendering commands

• Supports multiple per-vertex lights

• Assumes application computes object-space determination of occluder model’s polygons orientation w.r.t. each light

• See our paper “Practical and Robust See our paper “Practical and Robust Stenciled Shadow Volumes for Hardware-Stenciled Shadow Volumes for Hardware-Accelerated Rendering”Accelerated Rendering”• In the accompanying course notes

• And on-line at developer.nvidia.com

• Paper has pseudo-code for rendering Paper has pseudo-code for rendering procedureprocedure• OpenGL state settings & rendering commands

• Supports multiple per-vertex lights

• Assumes application computes object-space determination of occluder model’s polygons orientation w.r.t. each light

Page 50: Shadow Mapping and Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (1)

Requirements for Our Stenciled Shadow Volume Technique (1)1.1. Models must be composed of triangles Models must be composed of triangles

only (avoiding non-planar polygons)only (avoiding non-planar polygons)2.2. Models must be closed (2-manifold) and Models must be closed (2-manifold) and

have a consistent winding orderhave a consistent winding order• Bergeron [’86] approach could be used to handle

“open” models if necessary

3.3. Homogeneous object coordinates are Homogeneous object coordinates are permitted, assuming wpermitted, assuming w00• If not, (x, y, z, -1) = (-x, -y, -z, 1)

4.4. Ideal light sources onlyIdeal light sources only• Directional or positional, assuming w0

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

2.2. Models must be closed (2-manifold) and Models must be closed (2-manifold) and have a consistent winding orderhave a consistent winding order• Bergeron [’86] approach could be used to handle

“open” models if necessary

3.3. Homogeneous object coordinates are Homogeneous object coordinates are permitted, assuming wpermitted, assuming w00• If not, (x, y, z, -1) = (-x, -y, -z, 1)

4.4. Ideal light sources onlyIdeal light sources only• Directional or positional, assuming w0

Page 51: Shadow Mapping and Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (2)

Requirements for Our Stenciled Shadow Volume Technique (2)5.5. Connectivity information for occluding Connectivity information for occluding

models must be availablemodels must be available• So silhouette edges w.r.t. light positions can be

determined at shadow volume construction time

6.6. Projection matrix must be perspectiveProjection matrix must be perspective• Not orthographic

• NV_depth_clamp extension provides orthographic support (more later)

7.7. Render must guarantee “watertight” Render must guarantee “watertight” rasterizationrasterization• No double hitting pixels at shared polygon edges

• No missed pixels at shared polygon edges

5.5. Connectivity information for occluding Connectivity information for occluding models must be availablemodels must be available• So silhouette edges w.r.t. light positions can be

determined at shadow volume construction time

6.6. Projection matrix must be perspectiveProjection matrix must be perspective• Not orthographic

• NV_depth_clamp extension provides orthographic support (more later)

7.7. Render must guarantee “watertight” Render must guarantee “watertight” rasterizationrasterization• No double hitting pixels at shared polygon edges

• No missed pixels at shared polygon edges

Page 52: Shadow Mapping and Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (3)

Requirements for Our Stenciled Shadow Volume Technique (3)8.8. Enough stencil bitsEnough stencil bits

• N stencil bits where 2N is greater than the maximum shadow depth count ever encountered

• Scene dependent

• 8-bits is usually quite adequate & what all recent stencil hardware provides

• Wrapping stencil increment/decrement operations (i.e. OpenGL’s EXT_stencil_wrap) permit deeper shadow counts, modulo aliasing with zero

• Realize that shadow depths > 255 imply toomuch fill rate for interactive applications

8.8. Enough stencil bitsEnough stencil bits• N stencil bits where 2N is greater than the maximum

shadow depth count ever encountered

• Scene dependent

• 8-bits is usually quite adequate & what all recent stencil hardware provides

• Wrapping stencil increment/decrement operations (i.e. OpenGL’s EXT_stencil_wrap) permit deeper shadow counts, modulo aliasing with zero

• Realize that shadow depths > 255 imply toomuch fill rate for interactive applications

Page 53: Shadow Mapping and Shadow Volumes

Requirements for Our Stenciled Shadow Volume Technique (4)

Requirements for Our Stenciled Shadow Volume Technique (4)9.9. Rendering features provided by OpenGL 1.0 Rendering features provided by OpenGL 1.0

or DirectX 6 (or subsequent versions)or DirectX 6 (or subsequent versions)• Transformation & clipping of homogenous positions

• Front- and back-face culling

• Masking color and depth buffer writes

• Depth buffering (i.e. conventional Z-buffering)

• Stencil-testing support

9.9. Rendering features provided by OpenGL 1.0 Rendering features provided by OpenGL 1.0 or DirectX 6 (or subsequent versions)or DirectX 6 (or subsequent versions)• Transformation & clipping of homogenous positions

• Front- and back-face culling

• Masking color and depth buffer writes

• Depth 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 54: Shadow Mapping and Shadow Volumes

Our Approach in Practice (1)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 55: Shadow Mapping and Shadow Volumes

Our Approach in Practice (2)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 56: Shadow Mapping and Shadow Volumes

Our Approach in Practice (3)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 57: Shadow Mapping and Shadow Volumes

Our Approach in Practice (4)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 seenprojected onto far plane at infinity.

Page 58: Shadow Mapping and Shadow Volumes

Another Example (1)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 59: Shadow Mapping and Shadow Volumes

Another Example (2)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 60: Shadow Mapping and Shadow Volumes

Stenciled Shadow Volumes &Multiple LightsStenciled 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 61: Shadow Mapping and Shadow Volumes

Stenciled Shadow Volumes forSimulating Soft Shadows

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 62: Shadow Mapping and Shadow Volumes

Stenciled Shadow Volume Optimizations (1)Stenciled Shadow Volume Optimizations (1)

• Use Use GL_QUAD_STRIPGL_QUAD_STRIP rather than rather than GL_QUADSGL_QUADS to render extruded shadow volume quadsto render extruded shadow volume quads• Requires determining possible silhouette loop

connectivity

• Mix Mix ZfailZfail and and ZpassZpass techniques techniques• Pick a single formulation for each shadow volume

• Zpass is more efficient since shadow volume does not need to be closed

• Mixing has no effect on net shadow depth count

• Zfail can be used in the hard cases

• Use Use GL_QUAD_STRIPGL_QUAD_STRIP rather than rather than GL_QUADSGL_QUADS to render extruded shadow volume quadsto render extruded shadow volume quads• Requires determining possible silhouette loop

connectivity

• Mix Mix ZfailZfail and and ZpassZpass techniques techniques• Pick a single formulation for each shadow volume

• Zpass is more efficient since shadow volume does not need to be closed

• Mixing has no effect on net shadow depth count

• Zfail can be used in the hard cases

Page 63: Shadow Mapping and Shadow Volumes

Stenciled Shadow Volume Optimizations (2)Stenciled Shadow Volume Optimizations (2)

• Pre-compute or re-use cache shadow Pre-compute or re-use cache shadow volume geometry when geometric volume geometry when geometric relationship between a light and occluder relationship between a light and occluder does not change between framesdoes not change between frames• Example: Headlights on a car have a static shadow

volume w.r.t. the car itself as an occluder

• Advanced shadow volume culling Advanced shadow volume culling approachesapproaches• Uses portals, Binary Space Partitioning trees, occlusion

detection, and view frustum culling techniques to limit shadow volumes

• Careful to make sure such optimizationsare truly correct

• Pre-compute or re-use cache shadow Pre-compute or re-use cache shadow volume geometry when geometric volume geometry when geometric relationship between a light and occluder relationship between a light and occluder does not change between framesdoes not change between frames• Example: Headlights on a car have a static shadow

volume w.r.t. the car itself as an occluder

• Advanced shadow volume culling Advanced shadow volume culling approachesapproaches• Uses portals, Binary Space Partitioning trees, occlusion

detection, and view frustum culling techniques to limit shadow volumes

• Careful to make sure such optimizationsare truly correct

Page 64: Shadow Mapping and Shadow Volumes

Stenciled Shadow Volume Optimizations (3)Stenciled Shadow Volume Optimizations (3)

• Take advantage of ad-hoc knowledge of Take advantage of ad-hoc knowledge of scenes whenever possiblescenes whenever possible• Example: A totally closed room means you do not

have to cast shadow volumes for the wall, floor, ceiling

• Limit shadows to important entitiesLimit shadows to important entities• Example: Generate shadow volumes for monsters and

characters, but not static objects

• Characters can still cast shadows on static objects

• Mix shadow techniques where possibleMix shadow techniques where possible• Use planar projected shadows or

light-maps where appropriate

• Take advantage of ad-hoc knowledge of Take advantage of ad-hoc knowledge of scenes whenever possiblescenes whenever possible• Example: A totally closed room means you do not

have to cast shadow volumes for the wall, floor, ceiling

• Limit shadows to important entitiesLimit shadows to important entities• Example: Generate shadow volumes for monsters and

characters, but not static objects

• Characters can still cast shadows on static objects

• Mix shadow techniques where possibleMix shadow techniques where possible• Use planar projected shadows or

light-maps where appropriate

Page 65: Shadow Mapping and Shadow Volumes

Stenciled Shadow Volume Optimizations (4)Stenciled Shadow Volume Optimizations (4)• Shadow volume’s extrusion for directional Shadow volume’s extrusion for directional

lights can be rendered with a lights can be rendered with a GL_TRIANGLE_FANGL_TRIANGLE_FAN• Directional light’s shadow volume always projects to a

single point at infinity

• Shadow volume’s extrusion for directional Shadow volume’s extrusion for directional lights can be rendered with a lights can be rendered with a GL_TRIANGLE_FANGL_TRIANGLE_FAN• Directional light’s shadow volume always projects to a

single point at infinity

Scene withdirectional light.

Clip-space view ofshadow volume

Page 66: Shadow Mapping and Shadow Volumes

Stenciled Shadow Volume Optimizations (5)Stenciled Shadow Volume Optimizations (5)

• Use Use glScissorglScissor to limit shadow to limit shadow volumes for local attenuated light volumes for local attenuated light sourcessources•Compute bounding box for un-attenuated region

of a point light source in window-space

•Scissor to this region

•Keeps shadow volumes from extending infinitely into regions that provably get an insignificant lighting contribution

• Use Use glScissorglScissor to limit shadow to limit shadow volumes for local attenuated light volumes for local attenuated light sourcessources•Compute bounding box for un-attenuated region

of a point light source in window-space

•Scissor to this region

•Keeps shadow volumes from extending infinitely into regions that provably get an insignificant lighting contribution

Page 67: Shadow Mapping and Shadow Volumes

Hardware Enhancements:Wrapping Stencil OperationsHardware Enhancements:Wrapping Stencil Operations

• Conventional OpenGL 1.0 stencil Conventional OpenGL 1.0 stencil operationsoperations• GL_INCR increments and clamps to 2N-1

• GL_DECR decrements and clamps to zero

• DirectX 6 introduced “wrapping” stencil DirectX 6 introduced “wrapping” stencil operationsoperations

• Exposed by OpenGL’s Exposed by OpenGL’s EXT_stencil_wrapEXT_stencil_wrap extensionextension• GL_INCR_WRAP_EXT increments modulo 2N

• GL_DECR_WRAP_EXT decrements modulo 2N

• Avoids saturation throwing off the shadowAvoids saturation throwing off the shadowvolume depth countvolume depth count• Still possible, though very rare, that 2N,

22N, 32N, etc. can alias to zero

• Conventional OpenGL 1.0 stencil Conventional OpenGL 1.0 stencil operationsoperations• GL_INCR increments and clamps to 2N-1

• GL_DECR decrements and clamps to zero

• DirectX 6 introduced “wrapping” stencil DirectX 6 introduced “wrapping” stencil operationsoperations

• Exposed by OpenGL’s Exposed by OpenGL’s EXT_stencil_wrapEXT_stencil_wrap extensionextension• GL_INCR_WRAP_EXT increments modulo 2N

• GL_DECR_WRAP_EXT decrements modulo 2N

• Avoids saturation throwing off the shadowAvoids saturation throwing off the shadowvolume depth countvolume depth count• Still possible, though very rare, that 2N,

22N, 32N, etc. can alias to zero

Page 68: Shadow Mapping and Shadow Volumes

Hardware Enhancements:Depth Clamp (1)Hardware Enhancements:Depth Clamp (1)

• What is depth clamping?What is depth clamping?• Boolean hardware enable/disable

• When enabled, disables the near & far clip planes

• Interpolate the window-space depth value

• Clamps the interpolated depth value tothe depth range, i.e. [min(n,f),max(n,f)]

– Assuming glDepthRange(n,f);

• Geometry “behind” the far clip plane is still rendered – Depth value clamped to farthest Z– Similar for near clip plane, as long as w>0,

except clamped to closest Z

• What is depth clamping?What is depth clamping?• Boolean hardware enable/disable

• When enabled, disables the near & far clip planes

• Interpolate the window-space depth value

• Clamps the interpolated depth value tothe depth range, i.e. [min(n,f),max(n,f)]

– Assuming glDepthRange(n,f);

• Geometry “behind” the far clip plane is still rendered – Depth value clamped to farthest Z– Similar for near clip plane, as long as w>0,

except clamped to closest Z

Page 69: Shadow Mapping and Shadow Volumes

Hardware Enhancements:Depth Clamp (2)Hardware Enhancements:Depth Clamp (2)

• Advantage for stenciled shadow volumesAdvantage for stenciled shadow volumes• With depth clamp, P (rather than Pinf) can be used

with our robust stenciled shadow volume technique

• Marginal loss of depth precision re-gained

• Orthographic projections can work with our technique with depth clamping

• NV_depth_clamp OpenGL extensionNV_depth_clamp OpenGL extension• Easy to use

glEnable(GL_DEPTH_CLAMP_NV); glDisable(GL_DEPTH_CLAMP_NV);

• GeForce3 & GeForce4 Ti support (soon)

• Advantage for stenciled shadow volumesAdvantage for stenciled shadow volumes• With depth clamp, P (rather than Pinf) can be used

with our robust stenciled shadow volume technique

• Marginal loss of depth precision re-gained

• Orthographic projections can work with our technique with depth clamping

• NV_depth_clamp OpenGL extensionNV_depth_clamp OpenGL extension• Easy to use

glEnable(GL_DEPTH_CLAMP_NV); glDisable(GL_DEPTH_CLAMP_NV);

• GeForce3 & GeForce4 Ti support (soon)

Page 70: Shadow Mapping and Shadow Volumes

Hardware Enhancements:Two-sided Stencil Testing (1)Hardware Enhancements:Two-sided Stencil Testing (1)

• Current stenciled shadow volumes required Current stenciled shadow volumes required rendering shadow volume geometry twicerendering shadow volume geometry twice• First, rasterizing front-facing geometry

• Second, rasterizing back-facing geometry

• Two-sided stencil testing requires only one Two-sided stencil testing requires only one passpass• Two sets of stencil state: front- and back-facing

• Boolean enable for two-sided stencil testing

• When enabled, back-facing stencil state is used for stencil testing back-facing polygons

• Otherwise, front-facing stencil state is used

• Rasterizes just as many fragments,but more efficient for CPU & GPU

• Current stenciled shadow volumes required Current stenciled shadow volumes required rendering shadow volume geometry twicerendering shadow volume geometry twice• First, rasterizing front-facing geometry

• Second, rasterizing back-facing geometry

• Two-sided stencil testing requires only one Two-sided stencil testing requires only one passpass• Two sets of stencil state: front- and back-facing

• Boolean enable for two-sided stencil testing

• When enabled, back-facing stencil state is used for stencil testing back-facing polygons

• Otherwise, front-facing stencil state is used

• Rasterizes just as many fragments,but more efficient for CPU & GPU

Page 71: Shadow Mapping and Shadow Volumes

Hardware Enhancements:Two-sided Stencil Testing (2)Hardware Enhancements:Two-sided Stencil Testing (2)

EXT_stencil_two_side OpenGL extensionEXT_stencil_two_side OpenGL extension• Enable applies if GL_STENCIL_TEST also enabled

glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);

• Control of front- and back-facing stencil state updateglActiveStencilFaceNV(GL_FRONT);glActiveStencilFaceNV(GL_BACK);

• Existing stencil routines (glStencilOp, glStencilMask, glStencilFunc) update the active stencil face state

• glClear and non-polygon primitives alwaysuse the front-facing stencil state

• Expect on future GPUsExpect on future GPUs

EXT_stencil_two_side OpenGL extensionEXT_stencil_two_side OpenGL extension• Enable applies if GL_STENCIL_TEST also enabled

glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);

• Control of front- and back-facing stencil state updateglActiveStencilFaceNV(GL_FRONT);glActiveStencilFaceNV(GL_BACK);

• Existing stencil routines (glStencilOp, glStencilMask, glStencilFunc) update the active stencil face state

• glClear and non-polygon primitives alwaysuse the front-facing stencil state

• Expect on future GPUsExpect on future GPUs

Page 72: Shadow Mapping and Shadow Volumes

Usage of NV_stencil_two_side & EXT_stencil_wrapUsage of NV_stencil_two_side & EXT_stencil_wrap

OLD SCHOOLglDepthMask(0);glColorMask(0,0,0,0);glEnable(GL_CULL_FACE);glEnable(GL_STENCIL_TEST);glStencilMask(~0);glStencilFunc(GL_ALWAYS, 0, ~0); // Increment for back facesglCullFace(GL_BACK);glStencilOp(GL_KEEP, // stencil test fail GL_INCR, // depth test fail GL_INCR); // depth test passrenderShadowVolumePolygons();// Decrement for front facesglCullFace(GL_FRONT);glStencilOp(GL_KEEP, // stencil test fail GL_DECR, // depth test fail GL_KEEP); // depth test passrenderShadowVolumePolygons();

NEW SCHOOLglDepthMask(0);glColorMask(0,0,0,0);glDisable(GL_CULL_FACE);glEnable(GL_STENCIL_TEST);glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);glActiveStencilFaceEXT(GL_BACK);glStencilOp(GL_KEEP, // stencil test fail GL_INCR_WRAP_EXT, // depth test fail GL_KEEP); // depth test passglStencilMask(~0);glStencilFunc(GL_ALWAYS, 0, ~0);glActiveStencilFaceEXT(GL_FRONT);glStencilOp(GL_KEEP, // stencil test fail GL_DECR_WRAP_EXT, // depth test fail GL_KEEP); // depth test passglStencilMask(~0);glStencilFunc(GL_ALWAYS, 0, ~0);renderShadowVolumePolygons();

New approach calls renderShadowVolumePolygons() just once.

Page 73: Shadow Mapping and Shadow Volumes

Shadow Volume History (1)Shadow Volume History (1)

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

• Brotman and Badler [’84] Brotman and Badler [’84] • Software-based depth-buffered approach

• Used lots of point lights to simulate soft shadows

• Pixel-Planes [Fuchs, et.al. ’85] hardwarePixel-Planes [Fuchs, et.al. ’85] hardware• First hardware approach

• Point within a volume, rather than ray intersection

• Bergeron [’96] generalizationsBergeron [’96] generalizations• Explains how to handle open models

• And non-planar polygons

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

• Brotman and Badler [’84] Brotman and Badler [’84] • Software-based depth-buffered approach

• Used lots of point lights to simulate soft shadows

• Pixel-Planes [Fuchs, et.al. ’85] hardwarePixel-Planes [Fuchs, et.al. ’85] hardware• First hardware approach

• Point within a volume, rather than ray intersection

• Bergeron [’96] generalizationsBergeron [’96] generalizations• Explains how to handle open models

• And non-planar polygons

Page 74: Shadow Mapping and Shadow Volumes

Shadow Volume History (2)Shadow Volume History (2)

• Fournier & Fussell [’88] theoryFournier & Fussell [’88] theory• Provides theory for shadow volume counting approach

within a frame buffer

• Akeley & Foran invent the stencil bufferAkeley & Foran invent the stencil buffer• IRIS GL functionality, later made part of OpenGL 1.0

• Patent filed in ’92

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

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

• Fournier & Fussell [’88] theoryFournier & Fussell [’88] theory• Provides theory for shadow volume counting approach

within a frame buffer

• Akeley & Foran invent the stencil bufferAkeley & Foran invent the stencil buffer• IRIS GL functionality, later made part of OpenGL 1.0

• Patent filed in ’92

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

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

Page 75: Shadow Mapping and Shadow Volumes

Shadow Volume History (3)Shadow Volume History (3)

• Dietrich slides [March ’99] at GDCDietrich slides [March ’99] at GDC• Proposes Zfail based stenciled shadow volumes

• Kilgard whitepaper [March ’99] at GDCKilgard whitepaper [March ’99] at GDC• Invert approach for planar cut-outs

• Bilodeau slides [May ’99] at Creative Bilodeau slides [May ’99] at Creative seminarseminar• Proposes way around near plane clipping problems

• Reverses depth test function to reverse stencil volume ray intersection sense

• Carmack [unpublished, early 2000]Carmack [unpublished, early 2000]• First detailed discussion of the equivalence of

Zpass and Zfail stenciled shadowvolume methods

• Dietrich slides [March ’99] at GDCDietrich slides [March ’99] at GDC• Proposes Zfail based stenciled shadow volumes

• Kilgard whitepaper [March ’99] at GDCKilgard whitepaper [March ’99] at GDC• Invert approach for planar cut-outs

• Bilodeau slides [May ’99] at Creative Bilodeau slides [May ’99] at Creative seminarseminar• Proposes way around near plane clipping problems

• Reverses depth test function to reverse stencil volume ray intersection sense

• Carmack [unpublished, early 2000]Carmack [unpublished, early 2000]• First detailed discussion of the equivalence of

Zpass and Zfail stenciled shadowvolume methods

Page 76: Shadow Mapping and Shadow Volumes

Shadow Volume History (4)Shadow Volume History (4)

• Kilgard [2001] at GDC and CEDEC JapanKilgard [2001] at GDC and CEDEC Japan• Proposes Zpass capping scheme

– Project back-facing (w.r.t. light) geometry to the near clip plane for capping

– Establishes near plane ledge for crack-freenear plane capping

• Applies homogeneous coordinates (w=0) for rendering infinite shadow volume geometry

• Requires much CPU effort for capping

• Not totally robust because CPU and GPU computations will not match exactly, resulting in cracks

• Kilgard [2001] at GDC and CEDEC JapanKilgard [2001] at GDC and CEDEC Japan• Proposes Zpass capping scheme

– Project back-facing (w.r.t. light) geometry to the near clip plane for capping

– Establishes near plane ledge for crack-freenear plane capping

• Applies homogeneous coordinates (w=0) for rendering infinite shadow volume geometry

• Requires much CPU effort for capping

• Not totally robust because CPU and GPU computations will not match exactly, resulting in cracks

Page 77: Shadow Mapping and Shadow Volumes

Our ContributionOur Contribution

• We Integrate Multiple Ideas into a Robust We Integrate Multiple Ideas into a Robust SolutionSolution• Dietrich, Bilodeau, and Carmack’s Zfail approach

• Kilgard’s homogeneous coordinates (w=0) for rendering infinite shadow volume geometry

• Somewhat-obscure [Blinn ’93] infinite far plane projection matrix formulation

• DirectX 6’s wrapping stencil increment & decrement– OpenGL’s EXT_stencil_wrap extension

• Hardware EnhancementsHardware Enhancements• Depth clamping: better depth precision

• Two-sided stencil testing: performance

• We Integrate Multiple Ideas into a Robust We Integrate Multiple Ideas into a Robust SolutionSolution• Dietrich, Bilodeau, and Carmack’s Zfail approach

• Kilgard’s homogeneous coordinates (w=0) for rendering infinite shadow volume geometry

• Somewhat-obscure [Blinn ’93] infinite far plane projection matrix formulation

• DirectX 6’s wrapping stencil increment & decrement– OpenGL’s EXT_stencil_wrap extension

• Hardware EnhancementsHardware Enhancements• Depth clamping: better depth precision

• Two-sided stencil testing: performance

Page 78: Shadow Mapping and Shadow Volumes

ConclusionsConclusions

• Cursory look at hardware shadow mappingCursory look at hardware shadow mapping• See the course notes for details

• Presented robust stenciled shadow volume techniquePresented robust stenciled shadow volume technique• Reasonable & well-defined requirements

• Almost impossible to buy PC in 2002 without stencil buffer support

• Still, shadow volumes require high fill rates

• Useful for shading Useful for shading AND AND geometric computationsgeometric computations• Consider other uses for two-sided stencil testing & depth clamp in

other geometric computation problems

• Cursory look at hardware shadow mappingCursory look at hardware shadow mapping• See the course notes for details

• Presented robust stenciled shadow volume techniquePresented robust stenciled shadow volume technique• Reasonable & well-defined requirements

• Almost impossible to buy PC in 2002 without stencil buffer support

• Still, shadow volumes require high fill rates

• Useful for shading Useful for shading AND AND geometric computationsgeometric computations• Consider other uses for two-sided stencil testing & depth clamp in

other geometric computation problems