Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows...

73
Shadows

Transcript of Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows...

Page 1: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadows

Page 2: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

What for?

Shadows tell us about the relative locations and motions of objects

Vienna University of Technology 2

Page 3: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

What for?

Shadows tell us about the relative locations and motions of objects

And about light positions

Vienna University of Technology 3

Page 4: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

What for?

Objects look like they are “floating”

Shadows can fix that!

Vienna University of Technology 4

Page 5: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Motivation

Shadows contribute significantly to realism of rendered images

Anchors objects in scene

Global effect expensive!

Light source behaves very similar to camera

Is a point visible from the light source?

shadows are “hidden” regions

Shadow is a projection of caster on receiver

projection methods

Best done completely in hardware through shaders

Vienna University of Technology 5

Page 6: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Algorithms

Static shadow algorithms (lights + objects)

Radiosity, ray tracing lightmaps

Approximate shadows

Projected shadows (Blinn 88)

Shadow volumes (Crow 77)

Object-space algorithm

Shadow maps (Williams 78)

Projective image-space algorithm

Soft shadow extensions for all above algorithms

Still hot research topic (500+ shadow publications)

Vienna University of Technology 6

Page 7: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Terms

Vienna University of Technology 7

receiver

(occludee)

light source

creator

(occluder, blocker, caster) creator and

receiver

Page 8: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Hard vs. Soft Shadows

Vienna University of Technology 8

point source

hard shadow

area source

umbra

penumbra penumbra+fast

-only good for localized lights

(sun, projectors)

+fake soft shadow through

filtering

+ very realistic

- very expensive

+ becomes more and more

usable

Page 9: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Static Shadows

Glue to surface whatever we want

Idea: incorporate shadows into light maps

For each texel, cast ray to each light source

Bake soft shadows in light maps

Not by texture filtering alone, but:

Sample area light sources

Vienna University of Technology 9

Page 10: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Static Soft Shadow Example

Vienna University of Technology 10

no filtering filtering

1 sample

n samples

Page 11: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Approximate Shadows

Handdrawn approximate geometry

Perceptual studies suggest:shape not so important

Minimal cost

Vienna University of Technology 11

Page 12: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Approximate Shadows

Dark polygon (maybe with texture)

Cast ray from light source through object center

Blend polygon into frame buffer at location of hit

May apply additional rotation/scale/translation

Incorporate distance and receiver orientation

Problem with zquantization:

Vienna University of Technology 12

Blend at hit polygon

Z-test equal

z-buffer quantization errors!

viewerlight

Page 13: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Approximate Shadows

Vienna University of Technology 13

viewerlightElevate above hit polygon

Z-test less or equal

shadow too big

may appear floating

No z-test, only one eye ray

shadow too big,

maybe in wrong place

Page 14: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Projection Shadows (Blinn 88)

Shadows for selected large planar receivers

Ground plane

Walls

Projective geometry:flatten 3D model ontoplane

and “darken” using framebuffer blend

Vienna University of Technology 14

Page 15: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Projection for Ground Plane

Use similar-triangle tricks

Vienna University of Technology 15

p

ly

y=0

v

0=

-

-=

-

-=

-=

-

-

y

yy

yzzy

z

yy

yxxy

x

yy

y

xx

xx

p

vl

vlvlp

vl

vlvlp

vl

l

lv

lp

Page 16: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Projection Matrix

Projective 4x4 matrix:

Arbitrary plane:

Intersect line p = l – (v – l)

with plane n x + d = 0

Express result as a 4x4 matrix

Append this matrix to view transformVienna University of Technology 16

l

ll

ll

M

y

yz

xy

-

-

-

=

010

00

0000

00

l

p

v

n

Page 17: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Projection Shadow Algorithm

Render scene (full lighting)

For each receiver polygon

Compute projection matrix M

Append to view matrix

Render selected shadow caster

With framebuffer blending enabled

Vienna University of Technology 17

Page 18: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Projection Shadow Artifacts

Vienna University of Technology 18

Bad Good

extends off

ground region

Z fighting double blending

Page 19: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Stencil Buffer Projection Shadows

Stencil can solve all of these problems

Separate 8-bit frame buffer for numeric ops

Stencil buffer algorithm (requires 1 bit):

Clear stencil to 0

Draw ground polygon last and withglStencilOp(GL_KEEP, GL_KEEP, GL_ONE);

Draw shadow caster with no depth test butglStencilFunc(GL_EQUAL, 1, 0xFF);

glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);

Every plane pixel is touched at most onceVienna University of Technology 19

fail zfail pass

Page 20: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Stencil Buffer Planar Reflections

Draw object twice, second time with:

glScalef(1, -1, 1)

Reflects through floor

Vienna University of Technology 20

Good, stencil

used to limit reflection.

Bad

Page 21: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Projection Shadow Summary

Easy to implement

GLQuake first game to implement it

Only practical for very few, large receivers

No self shadowing

Possible remaining artifacts: wrong shadows

Objects behind light source

Objects behind receiver

Vienna University of Technology 21

Page 22: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volumes (Crow 1977)

Occluders and light source cast out a 3D shadow volume

Shadow through new geometry

Results in Pixel correct shadows

Vienna University of Technology 22

Light

source

Visualization of

shadow volumeShadowed scene

Page 23: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volumes (Crow 1977)

Heavily used in Doom3

Vienna University of Technology 23

Page 24: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

2D Cutaway of Shadow Volume

Occluder polygons extruded to semi-infinite volumes

Vienna University of Technology 24

shadowing

object

shadow

volume

(infinite extent)

partiallyshadowed object

light

source

eye

position surface inside

shadow volume

(shadowed)

surface outside

shadow volume

(illuminated)

Page 25: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

3D point-in-polyhedron inside-outside test

Principle similar to 2D point-in-polygon test

Choose a point known to be outside the volume

Count intersection on ray from test point to known point with polyhedron faces

Front face +1

Back face -1

Like non-zero winding rule!

Known point will distinguish algorithms:

Infinity: “Z-fail” algorithm

Eye-point: “Z-pass” algorithm

Vienna University of Technology 25

Page 26: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Enter/Leave Approach

Increment on enter, decrement on leave

Simultaneously test all visible pixels

Stop when hitting object nearest to viewer

Vienna University of Technology 26

shadowing objectlight

source

eye

position

zero

zero

+1

+1+2 +2

+3

Page 27: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

Shadow volumes in object precision

Calculated by CPU/Vertex Shaders

Shadow test in image precision

Using stencil buffer as counter!

Vienna University of Technology 27

Page 28: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

Vienna University of Technology 28

Step 1: Render scene Z-values

Page 29: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

Vienna University of Technology 29

Front face: +1

Step 2: Render shadow volume faces

Back face: -1

Page 30: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

Vienna University of Technology 30

Front face: ±0 (Depth test)Back face: ±0 (Depth test)

= ±0

Page 31: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

Vienna University of Technology 31

Front face: +1Back face: ±0 (Depth test)

= +1

±0

Page 32: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

Vienna University of Technology 32

Front face: +1Back face: -1

= ±0

±0+1

Page 33: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm

Vienna University of Technology 33

±0

+1

±0

Step 3: Apply shadow mask to scene

Page 34: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Algorithm (Zpass)

Render scene to establish z-buffer

Can also do ambient illumination

For each light

Clear stencil

Draw shadow volume twice using culling

Render front faces and increment stencil

Render back faces and decrement stencil

Illuminate all pixels not in shadow volume

Render testing stencil = 0

Use additive blend

Vienna University of Technology 34

Page 35: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zpass Technique (Before Shadow)

Vienna University of Technology 35

Shadowing objectLight

source

Eye

position

zero

zero

+1

+1

+2 +2

+3

Unshadowed

object

Shadow Volume Count = 0 (no depth tests passes)

Page 36: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zpass Technique (In Shadow)

Vienna University of Technology 36

Shadowing objectLight

source

Eye

position

zero

zero

+1

+1

+2 +2

+3

Shadowed

object

+ -+ +

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

Page 37: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zpass Technique (Behind Shadow)

Vienna University of Technology 37

Shadowing objectLight

source

Eye

position

zero

zero

+1

+1

+2 +2

+3

Unshadowed

object

+ ---+ +

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

Page 38: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zpass Near Plane Problem

Vienna University of Technology 38

zero

zero

+1+1

+2

+2

+3

Near clip

plane

Far clip

plane

Missed shadow volume

intersection due to

near clip plane

clipping; leads to

mistaken count

Page 39: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Alternative: Zfail Technique

Zpass near plane problem difficult to solve

Have to “cap” shadow volume at near plane

Expensive and not robust, many special cases

Try reversing test order Zfail technique(also known as Carmack’s reverse)

Start from infinity and stop at nearest intersection

Render shadow volume fragments only whendepth test fails

Render back faces first and increment

Then front faces and decrement

Need to cap shadow volume at infinity or light extent

Vienna University of Technology 39

Page 40: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zfail, Behind Shadow

Vienna University of Technology 40

Shadowing objectLight

source

Eye

position

zero

zero

+1

+1

+2 +2

+3

Unshadowed

object

Shadow Volume Count = 0 (zero depth tests fail)

Page 41: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zfail, in Shadow

Vienna University of Technology 41

Shadowing objectLight

source

Eye

position

zero

zero

+1

+1

+2 +2

+3

Shadow Volume Count = +1+1 = 2

+ +

Shadowed

object

Page 42: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zfail, before Shadow

Vienna University of Technology 42

Shadowing objectLight

source

Eye

position

zero

zero

+1

+1

+2 +2

+3

Unshadowed

object

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

- +- - + +

Page 43: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volumes

Shadow volume = closed polyhedron

Actually 3 sets of polygons!

1. Object polygons facing the light (“light cap”)

2. Object polygons facing away from the light and projected to infinity (with w = 0) (“dark cap”)

3. Actual shadow volume polygons (extruded object edges) (“sides”) but which edges?

Vienna University of Technology 43

Page 44: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zpass vs. Zfail

Equivalent, but reversed

Zpass

Faster (light cap and dark cap not needed)

Light cap inside object always fails z-test

Dark cap infinitely far away either fails or falls on background

Problem at near clip plane (no robust solution)

Zfail

Slower (need to render dark and light caps!)

Problem at far clip plane when light extends farther than far clip plane

Robust solution with infinite shadow volumes!

Vienna University of Technology 44

Page 45: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Zpass vs. Zfail

Idea: Combine techniques!

Test whether viewport in shadow Zfail

Otherwise Zpass

Idea: avoid far plane clipping in Zfail!

Send far plane to infinity in projection matrix

Easy, but loses some depth buffer precision

Draw infinite vertices using homogeneous coordinates: project to infinity w = 0

robust solution!

Vienna University of Technology 45

Page 46: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

W=0 Rasterization

At infinity, vertices become vectors

Vienna University of Technology 46

(-3,-1,z1,1)

(2,-2,z2,1)

(2,-2,z2,0)(-1,-3,z1,0) (2,-2,z2,0)

Page 47: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Computing Actual SV Polygons

Trivial but bad: one volume per triangle

3 shadow volume polygons per triangle

Better: find exact silhouette

Expensive on CPU

Even better: possible silhouette edges

Edge shared by a back-facing and front-facing polygon (with respect to light source!), extended to infinity

Actual extrusion can be done by vertex shader

Vienna University of Technology 47

Page 48: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Possible Silhouette Edges

Vienna University of Technology 48

Page 49: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volumes Summary

AdvantagesArbitrary receivers

Fully dynamic

Omnidirectional lights (unlike shadow maps!)

Exact shadow boundaries (pixel-accurate)

Automatic self shadowing

Broad hardware support (stencil)

DisadvantagesFill-rate intensive

Difficult to get right (Zfail vs. Zpass)

Silhouette computation required

Doesn’t work for arbitrary casters (smoke, fog…)

Vienna University of Technology 49

Page 50: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Volume Issues

Stencil buffering fast and present in all cards

With 8 bits of stencil, maximum shadow depth is 255

EXT_stencil_wrap overcomes this

Two-sided stencil tests can test front- and back triangles simultaneously

Saves one pass – available on NV30+

NV_depth_clamp (hardware capping)

Regain depth precision with normal projection

Requires watertight models with connectivity, and watertight rasterization

Vienna University of Technology 50

Page 51: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Maps

Casting curved shadows on curved surfaces

Image-space algorithm, 2 passes

Vienna University of Technology 51

Shadow map

Final scene

Page 52: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Map Algorithm

Vienna University of Technology 52

EyeLight

Render from light; save depth values

Render from eye

Transform all fragments to light space

Compare zeye and zlight (both in light space!!!)

zeye > zLicht fragment in shadow

Shadow mapEye view

Page 53: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Maps in Hardware

Render scene to z-buffer (from light source)

Copy depth buffer to texture

Render to depth texture + pbuffer

Project shadow map into scene (remember projective texturing!)

Hardware shadow test (ARB_shadow)

Use homogeneous texture coordinates

Compare r/q with texel at (s/q, t/q)

Output 1 for lit and 0 for shadow

Blend fragment color with shadow test resultVienna University of Technology 53

Page 54: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Maps in Hardware

Shadow extension available since GeForce3

Requires high precision texture format(ARB_depth_texture)

On modern hardware:

Render lightspace depth into texture

In vertex shader:

Calculate texture coordinates as in projective texturing

In fragment shader:

Depth compare

Vienna University of Technology 54

Page 55: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Problem: Perspective Aliasing

Sufficient resolution far from eye

Insufficient resolution near eye

Vienna University of Technology 55 aliasedokay

Page 56: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Problem: Projection Aliasing

Shadow receiver ~ orthogonal toShadow Map - viewplane

Vienna University of Technology 56

Page 57: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Problem: Incorrect Self-Shadowing

Vienna University of Technology 57

Polygon

Page 58: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Problem: Incorrect Self-Shadowing

Vienna University of Technology 58

Polygon

zeye > zlight Incorrect Self-shadowing

Page 59: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Perspective Aliasing

Insufficient resolution near eye

Vienna University of Technology 59

aliasedokay

Page 60: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Perspective Aliasing

Insufficient resolution near eye

Redistribute values in shadow map

Vienna University of Technology 60

Page 61: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Perspective Aliasing

Sufficient resolution near eye

Redistribute values in shadow map

Vienna University of Technology 61

okay okay

Page 62: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Perspective Aliasing

How to redistribute?

Use perspective transform

Additional perspective matrix, used in both:

Light pass

Eye pass

More details:

[WSP2004]

Vienna University of Technology 62

[WSP2004] M. Wimmer, D. Scherzer, and W. Purgathofer;

Light space perspective shadow maps; In Proceedings of

Eurographics Symposium on Rendering 2004

Page 63: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Projection Aliasing

Shadow receiver ~ orthogonal to Shadow Map plane

Redistribution does not work

But...

Vienna University of Technology 63

Page 64: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Projection Aliasing

Diffuse lighting: I = IL max( dot( L, N ), 0 )

Almost orthogonal receivers have small I

Dark artifacts not very visible!

Vienna University of Technology 64

LN

Page 65: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Projection Aliasing

Recommendations

Small ambient term

Diffuse term hides artifacts

Specular term not problematic

Light and view direction almost identical

Shadow Map resolution sufficient

Vienna University of Technology 65

Page 66: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Incorrect Self-Shadowing

Vienna University of Technology 67

Polygon

zAug > zLicht Incorrect Self-shadowing

Shifted Polygon

zAug < zLicht No Self-shadowing

Page 67: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Solution for Incorrect Self-Shadowing

How to choose bias?

Vienna University of Technology 68

Slope-Scale Bias

Constant Bias

No Bias

Page 68: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Depth Bias

glPolygonOffset(1.1,4.0) works well

Works in window coordinates

Vienna University of Technology 69

Too little bias,

everything begins to

shadow

Too much bias, shadow

starts too far back

Page 69: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Problem: Aliasing Artifacts

Resolution mismatch image/shadow map!

Use perspective shadow maps

Use “percentage closer” filtering

Normal color filtering cannot be used

Filter lookup result, not depth map values!

2x2 PCF in hardware for NVIDIA

Better: Poisson-disk distributed samples (e.g., 6 averaged samples)

Vienna University of Technology 71

Page 70: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Map Filtering

GL_NEAREST GL_LINEAR

Vienna University of Technology 72

Page 71: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Shadow Map Summary

Advantages

Fast – only one additional pass

Independent of scene complexity (no additional shadow polygons!)

Self shadowing (but beware bias)

Can sometimes reuse depth map

Disadvantages

Problematic for omnidirectional lights

Biasing tweak (light leaks, surface acne)

Jagged edges (aliasing)

Vienna University of Technology 73

Page 72: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Vienna University of Technology 74

OGRE shadow demo

Page 73: Real-Time Rendering (Echtzeitgraphik) - TU WienRadiosity, ray tracing lightmaps Approximate shadows Projected shadows (Blinn 88) Shadow volumes (Crow 77) Object-space algorithm Shadow

Conclusions

Shadows are very important but still difficult

Many variations based on shadow volumes/shadow maps to do shadowing:

Variance shadow mapping (VSM)

Perspective shadow mapping (PSM)

Hierarchical shadow volume

Subdivided shadow maps

Vienna University of Technology 75