Shadows What for? Shadows tell us about the relative locations and - - PowerPoint PPT Presentation

shadows what for shadows tell us about the relative
SMART_READER_LITE
LIVE PREVIEW

Shadows What for? Shadows tell us about the relative locations and - - PowerPoint PPT Presentation

Shadows What for? Shadows tell us about the relative locations and motions of objects Vienna University of Technology 2 What for? Shadows tell us about the relative locations and motions of objects And about light positions Vienna


slide-1
SLIDE 1

Shadows

slide-2
SLIDE 2

What for? Shadows tell us about the relative locations and motions of objects

Vienna University of Technology 2

slide-3
SLIDE 3

What for? Shadows tell us about the relative locations and motions of objects And about light positions

Vienna University of Technology 3

slide-4
SLIDE 4

What for? Objects look like they are “floating”  Shadows can fix that!

Vienna University of Technology 4

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Shadow Terms

Vienna University of Technology 7

receiver (occludee) light source creator (occluder, blocker, caster) creator and receiver

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Static Soft Shadow Example

Vienna University of Technology 10

no filtering filtering 1 sample n samples

slide-11
SLIDE 11

Approximate Shadows Handdrawn approximate geometry

Perceptual studies suggest: shape not so important Minimal cost

Vienna University of Technology 11

slide-12
SLIDE 12

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! viewer light

slide-13
SLIDE 13

Approximate Shadows

Vienna University of Technology 13

viewer light Elevate 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

slide-14
SLIDE 14

Projection Shadows (Blinn 88) Shadows for selected large planar receivers

Ground plane Walls

Projective geometry: flatten 3D model onto plane

and “darken” using framebuffer blend

Vienna University of Technology 14

slide-15
SLIDE 15

Projection for Ground Plane Use similar-triangle tricks

Vienna University of Technology 15

p l y y=0 v

=

  • =
  • =
  • =
  • y

y y y z z y z y y y x x y x y y y x x x x

p v l v l v l p v l v l v l p v l l l v l p

slide-16
SLIDE 16

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 transform

Vienna University of Technology 16

l l l l l M

y y z x y

             

  • =

1

l p v n

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Projection Shadow Artifacts

Vienna University of Technology 18

Bad Good

extends off ground region Z fighting double blending

slide-19
SLIDE 19

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 with

glStencilOp(GL_KEEP, GL_KEEP, GL_ONE);

Draw shadow caster with no depth test but

glStencilFunc(GL_EQUAL, 1, 0xFF); glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);

Every plane pixel is touched at most once

Vienna University of Technology 19

fail zfail pass

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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 volume Shadowed scene

slide-23
SLIDE 23

Shadow Volumes (Crow 1977) Heavily used in Doom3

Vienna University of Technology 23

slide-24
SLIDE 24

2D Cutaway of Shadow Volume Occluder polygons extruded to semi-infinite volumes

Vienna University of Technology 24

shadowing

  • bject

shadow volume (infinite extent) partially shadowed

  • bject

light source eye position surface inside shadow volume (shadowed) surface outside shadow volume (illuminated)

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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 object light source eye position zero zero +1 +1 +2 +2 +3

slide-27
SLIDE 27

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

slide-28
SLIDE 28

Shadow Volume Algorithm

Vienna University of Technology 28

Step 1: Render scene  Z-values

slide-29
SLIDE 29

Shadow Volume Algorithm

Vienna University of Technology 29

Front face: +1

Step 2: Render shadow volume faces

Back face: -1

slide-30
SLIDE 30

Shadow Volume Algorithm

Vienna University of Technology 30

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

slide-31
SLIDE 31

Shadow Volume Algorithm

Vienna University of Technology 31

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

slide-32
SLIDE 32

Shadow Volume Algorithm

Vienna University of Technology 32

Front face: +1 Back face:

  • 1

 = ±0 ±0 +1

slide-33
SLIDE 33

Shadow Volume Algorithm

Vienna University of Technology 33

±0 +1 ±0 Step 3: Apply shadow mask to scene

slide-34
SLIDE 34

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

slide-35
SLIDE 35

Zpass Technique (Before Shadow)

Vienna University of Technology 35

Shadowing object Light source Eye position zero zero +1 +1 +2 +2 +3 Unshadowed

  • bject

Shadow Volume Count = 0 (no depth tests passes)

slide-36
SLIDE 36

Zpass Technique (In Shadow)

Vienna University of Technology 36

Shadowing object Light source Eye position zero zero +1 +1 +2 +2 +3 Shadowed

  • bject

+

  • +

+

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

slide-37
SLIDE 37

Zpass Technique (Behind Shadow)

Vienna University of Technology 37

Shadowing object Light source Eye position zero zero +1 +1 +2 +2 +3 Unshadowed

  • bject

+

  • +

+

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

slide-38
SLIDE 38

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

slide-39
SLIDE 39

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 when depth 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

slide-40
SLIDE 40

Zfail, Behind Shadow

Vienna University of Technology 40

Shadowing object Light source Eye position zero zero +1 +1 +2 +2 +3 Unshadowed

  • bject

Shadow Volume Count = 0 (zero depth tests fail)

slide-41
SLIDE 41

Zfail, in Shadow

Vienna University of Technology 41

Shadowing object Light source Eye position zero zero +1 +1 +2 +2 +3

Shadow Volume Count = +1+1 = 2

+ + Shadowed

  • bject
slide-42
SLIDE 42

Zfail, before Shadow

Vienna University of Technology 42

Shadowing object Light source Eye position zero zero +1 +1 +2 +2 +3 Unshadowed

  • bject

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

  • +
  • +

+

slide-43
SLIDE 43

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

slide-44
SLIDE 44

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

slide-45
SLIDE 45

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

slide-46
SLIDE 46

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)

slide-47
SLIDE 47

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

slide-48
SLIDE 48

Possible Silhouette Edges

Vienna University of Technology 48

slide-49
SLIDE 49

Shadow Volumes Summary

Advantages

Arbitrary receivers Fully dynamic Omnidirectional lights (unlike shadow maps!) Exact shadow boundaries (pixel-accurate) Automatic self shadowing Broad hardware support (stencil)

Disadvantages

Fill-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

slide-50
SLIDE 50

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

slide-51
SLIDE 51

Shadow Maps Casting curved shadows on curved surfaces

Image-space algorithm, 2 passes

Vienna University of Technology 51

Shadow map Final scene

slide-52
SLIDE 52

Shadow Map Algorithm

Vienna University of Technology 52

Eye Light

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 map Eye view

slide-53
SLIDE 53

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 result

Vienna University of Technology 53

slide-54
SLIDE 54

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

slide-55
SLIDE 55

Problem: Perspective Aliasing

Sufficient resolution far from eye Insufficient resolution near eye

Vienna University of Technology 55

aliased

  • kay
slide-56
SLIDE 56

Problem: Projection Aliasing

Shadow receiver ~ orthogonal to Shadow Map - viewplane

Vienna University of Technology 56

slide-57
SLIDE 57

Problem: Incorrect Self-Shadowing

Vienna University of Technology 57

Polygon

slide-58
SLIDE 58

Problem: Incorrect Self-Shadowing

Vienna University of Technology 58

Polygon

zeye > zlight Incorrect Self-shadowing

slide-59
SLIDE 59

Solution for Perspective Aliasing

Insufficient resolution near eye

Vienna University of Technology 59

aliased

  • kay
slide-60
SLIDE 60

Solution for Perspective Aliasing

Insufficient resolution near eye Redistribute values in shadow map

Vienna University of Technology 60

slide-61
SLIDE 61

Solution for Perspective Aliasing

Sufficient resolution near eye Redistribute values in shadow map

Vienna University of Technology 61

  • kay
  • kay
slide-62
SLIDE 62

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

slide-63
SLIDE 63

Solution for Projection Aliasing

Shadow receiver ~ orthogonal to Shadow Map plane Redistribution does not work But...

Vienna University of Technology 63

slide-64
SLIDE 64

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

L N

slide-65
SLIDE 65

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

slide-66
SLIDE 66

Solution for Incorrect Self-Shadowing

Vienna University of Technology 67

Polygon

zAug > zLicht Incorrect Self-shadowing

Shifted Polygon

zAug < zLicht No Self-shadowing

slide-67
SLIDE 67

Solution for Incorrect Self-Shadowing How to choose bias?

Vienna University of Technology 68

Slope-Scale Bias Constant Bias No Bias

slide-68
SLIDE 68

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

slide-69
SLIDE 69

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

slide-70
SLIDE 70

Shadow Map Filtering GL_NEAREST GL_LINEAR

Vienna University of Technology 72

slide-71
SLIDE 71

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

slide-72
SLIDE 72

Vienna University of Technology 74

OGRE shadow demo

slide-73
SLIDE 73

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