shadow algorithms

44
Shadow Algorithms Han-Wei Shen

Upload: others

Post on 03-Feb-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shadow Algorithms

Shadow Algorithms

Han-Wei Shen

Page 2: Shadow Algorithms

Wh Sh d ?Why Shadows?

Makes 3D Graphics more believable Makes 3D Graphics more believable Provides additional cues for the shapes and

l ti iti f bj t i 3Drelative positions of objects in 3D

Page 3: Shadow Algorithms

Wh t i h d ?What is shadow?

Shadow: comparative darkness given by shelter from direct light; patch of shade projected by a bodyprojected by a body intercepting light

Page 4: Shadow Algorithms

T i lTerminology

(area) light source

occluder

penumbra • umbra – fully shadowed region

umbra

p• penumbra – partially shadowed region

receiver shadow

Page 5: Shadow Algorithms

“H d” d “S ft” Sh d“Hard” and “Soft” Shadows

D d th t f li ht Depends on the type of light sources Point or Directional (“Hard Shadows”, umbra)

point directional area

Area (“Soft Shadows”, umbra, penumbra), more difficult problem

point directional

more difficult problem

Page 6: Shadow Algorithms

“H d” d “S ft” Sh d“Hard” and “Soft” Shadows

Hard shadow Soft shadow – point light source – area light sourcepoint light source area light source

Page 7: Shadow Algorithms

Simple Approach: Ray tracing

Cast ray to light (shadow rays)

Surface point in shadow if the shadow rays hits an occluder object.

Ray tracing is slow, can we do better? (perhaps at the cost of quality)

Page 8: Shadow Algorithms

Sh d Al ithShadow Algorithms We will first focus on hard shadows We will first focus on hard shadows Planar Shadows Shadow Mapsp Shadow Volume

Page 9: Shadow Algorithms

Pl Sh dPlanar Shadows

The simplest algorithm shadowing occurs The simplest algorithm – shadowing occurs when objects cast shadows on planar surfaces (projection shadows)surfaces (projection shadows)

lighty lighty

shadow

y=0

shadow

n.x + d =0

Page 10: Shadow Algorithms

Pl Sh dPlanar Shadows Special case: the shadow receiver is an axis Special case: the shadow receiver is an axis

plane Just project all the polygon vertices to that Just project all the polygon vertices to that

plane and form shadow polygons

light Gi enlighty Given: - Light position l- Plane position y = 0

v - Vertex position v

Calculate: pshadow y=0p

Calculate: p

Page 11: Shadow Algorithms

Pl Sh dPlanar Shadows

We can use similar triangles to solve p We can use similar triangles to solve p

lighty px-lx ly

vx-lx ly-vy=

l

vly vx – lx vy

shadow y=0px

px =ly - vy

x• Same principle applied to different axis planes

Page 12: Shadow Algorithms

Pl Sh dPlanar Shadows

How about arbitrary plane as the shadow How about arbitrary plane as the shadow receiver?

Plane equation: n x + d = 0 orPlane equation: n.x + d = 0 or

ax + by + cz + d = 0 lightyl

where n = (a,b,c) - plane normal

Again given light position l vh dv

Again, given light position l , v

Find p

shadow

n.x + d =0pp

Page 13: Shadow Algorithms

Pl Sh dPlanar Shadows

Finding p W k l ( l) Finding p We know: p = l + (v-l) x

Also we know: n.p + d = 0

lightyl

pBecause p is on the plane

We can solve easily:

h dv

d + n lshadow

n.x + d =0p

p = l - (v - l) d + n.l

n. (v – l) p

Page 14: Shadow Algorithms

I b t l h dIssues about planar shadows Shadow polygon generation (z fighting) Shadow polygon generation (z fighting) Add an offset to the shadow polygons (glPolygonOffset) Draw receivers first, turn z-test off, then draw the shadow , ,

polygons. After this, draw the rest of the scene. Shadow polygons fall outside the receiver

U i il b ff d i d d h il Using stencil buffer – draw receiver and update the stencil buffer

Shadows have to be rendered at each frame Shadows have to be rendered at each frame Render into texture

Restrictive to planar objects

Page 15: Shadow Algorithms

I b t l h dIssues about planar shadows

Anti shadows and false shadows Anti-shadows and false shadows

false shadowanti-shadowcorrect

Page 16: Shadow Algorithms

Sh d V lShadow Volumes

A more general approach for receivers that A more general approach for receivers that have arbitrary shapes

Page 17: Shadow Algorithms

Wh t i h d l ?What is shadow volume? A volume of space formed by an occluder A volume of space formed by an occluder Bounded by the edges of the occluder Any object inside the shadow volume is in shadow Any object inside the shadow volume is in shadow

light source

Page 18: Shadow Algorithms

2D Cutaway of a Shadow Volume

Surface outsideShadowingobject

Light

Surface outsideshadow volume(illuminated)

Lightsource Shadow

volume(infinite extent)

E iti

(infinite extent)

Partiallyshadowed

bj t

Eye position(note that shadows are i d d t f

Surface insideshadow volume

objectindependent of the eye position)

(shadowed)

Page 19: Shadow Algorithms

I Sh d t?In Shadow or not? Use shadow volume to perform such a test Use shadow volume to perform such a test How do we know an object is inside the shadow

volume? 1 Allocate a counter1. Allocate a counter2. Cast a ray into the scene 3. Increment the counter when the ray enter a front-facing

polygon of the shadow volume (enter the shadow volume)polygon of the shadow volume (enter the shadow volume)4. Decrement the counter when the ray crosses a back-

facing polygon of the shadow volume (leave the shadow volume) )

5. When we hit the object, check the counter. If counter >0 ; in shadow Otherwise - not in shadow

Page 20: Shadow Algorithms

C t f Sh d V lCounter for Shadow Volume

Shadowing objectLightsource

zero +1

zero

Eyeposition

+1+2 +2

+3

In shadow

position

Page 21: Shadow Algorithms

R l ti h d lReal time shadow volume How can we render the idea of shadow volume in How can we render the idea of shadow volume in

real time? Use OpenGL Stencil buffer as the counter

Stencil buffer? Stencil buffer? Similar to color or depth buffer, except it’s meaning is

controlled by application (and not visible) Part of OpenGL fragment operation after alpha test Part of OpenGL fragment operation – after alpha test

before depth test Control whether a fragment is discarded or not Stencil function (Stencil test) - used to decide whether Stencil function (Stencil test) used to decide whether

to discard a fragment Stencil operation – decide how the stencil buffer is

updated as the result of the testp

Page 22: Shadow Algorithms

St il F tiStencil Function Comparison test between reference and stencil value p

GL_NEVER always fails GL_ALWAYS always passes GL_LESS passes if reference value is less than stencil buffer GL LEQUAL passes if reference value is less than or equal to stencil GL_LEQUAL passes if reference value is less than or equal to stencil

buffer GL_EQUAL passes if reference value is equal to stencil buffer GL_GEQUAL passes if reference value is greater than or equal to

stencil bufferstencil buffer GL_GREATER passes if reference value is greater than stencil buffer GL_NOTEQUAL passes if reference value is not equal to stencil buffer

If the stencil test fails, the fragment is discarded and the stencil operations fassociated with the stencil test failing is applied to the stencil value

If the stencil test passes, the depth test is applied If the depth test passes, the fragment continue through the graphics

pipeline, and the stencil operation for stencil and depth test passing is gapplied

If the depth test fails, the stencil operation for stencil passing but depth failing is applied

Page 23: Shadow Algorithms

St il O tiStencil Operation Stencil Operation: Results of Operation on Stencil Stencil Operation: Results of Operation on Stencil

Values GL_KEEP stencil value unchanged GL ZERO stencil value set to zero GL_ZERO stencil value set to zero GL_REPLACE stencil value replaced by stencil reference

value GL INCR stencil value incremented GL_INCR stencil value incremented GL_DECR stencil value decremented GL_INVERT stencil

value bitwise inverted Remember you can set different operations for Remember you can set different operations for Stencil fails Stencil passes, depth fails Stencil passes depth passes Stencil passes, depth passes

Page 24: Shadow Algorithms

O GL f h d lOpenGL for shadow volumes Z pass approach Z-pass approach Z-fail approach Ideas used by both of the algorithms are similar Ideas used by both of the algorithms are similar Z-pass: see whether the number of visible front-facing

shadow volume polygons and the number of visible back-facing polygons are equal. If yes – objects are not in shadow

Z-fail: see whether the number of invisible back-facing shadow volume polygons and the number of invisible front-p ygfacing polygons are equal. If yes – objects are not in shadow

Page 25: Shadow Algorithms

Z hZ-pass approach Render visible scene with only ambient and emssion and Render visible scene with only ambient and emssion and

update depth buffer Turn off depth and color write, turn on stencil (but still keep

the depth test on)the depth test on) Init. stencil buffer Draw shadow volume twice using face culling

1 t d f t f d i t t il b ff 1st pass: render front faces and increment stencil buffer when depth test passes

2nd pass: render back faces and decrement when depth t ttest passes

stencil pixels != 0 in shadow, = 0 are lit Render the scene again with diffuse and specular when

stencil pixels =0

Page 26: Shadow Algorithms

P bl f Z l ithProblems of Z-pass algorithm

1 When the eye is in the shadow volume1. When the eye is in the shadow volume Counter= 0 does not imply out of shadow

anymoreanymore In this case, the stencil buffer should be init. with

the number of shadow volumes in which the eye yis in (instead 0)

2. When the near plane intersects with the pshadow volume faces (and thus will clip the faces)

Page 27: Shadow Algorithms

Z l ith blZ-pass algorithm problem

illustration illustrationlight

eye

near

y

farfar

Mistakenly determined as not in shadow

Page 28: Shadow Algorithms

Z f il hZ-fail approach

R d i ibl t d th b ff Render visible scene to depth buffer Turn off depth and color, turn on stencil Init. stencil buffer given viewpoint 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

t il i l ! 0 i h d 0 lit stencil pixels != 0 in shadow, = 0 are lit

Page 29: Shadow Algorithms

P bl f f il l ithProblem of z-fail algorithm

Shadow volume can penetrate the far plane Shadow volume can penetrate the far plane

Depth clamping

• Solution: depth clamping - close up the shadowSolution: depth clamping close up the shadowvolume

Page 30: Shadow Algorithms

Sh d MShadow Map

Basic idea: objects that are not visible to the Basic idea: objects that are not visible to the light are in shadow H t d t i h th bj t How to determine whether an object are visible to the eye?

Use z buffer algorithm but now the “eye” is light Use z-buffer algorithm, but now the “eye” is light, i.e., the scene is rendered from light’s point of view

This particular z-buffer for the eye is called shadow map

Page 31: Shadow Algorithms

Sh d M Al ithShadow Map Algorithm illustration illustration

Page 32: Shadow Algorithms

Sh d M Al ithShadow Map Algorithm

1 Render the scene using the light as the1. Render the scene using the light as the camera and perform z-bufferingG t li ht b ff ( ll d h d2. Generate a light z buffer (called shadow map) R d th i th l3. Render the scene using the regular camera, perform z-buffering, and run the following steps: (next slide)steps: (next slide)

Page 33: Shadow Algorithms

Sh d M Al ithShadow Map Algorithm (cont’d)

3 1 For each visible fragment with [x y z] in3.1 For each visible fragment with [x,y,z] in loccal space, perform a transformation to the light’s clip space (light as the eye)the light s clip space (light as the eye) [x1,y1,z1]

3 2 Compare z1 with z = shadow map[x1 y1]3.2 Compare z1 with z = shadow_map[x1,y1]- If z1 <=z (closer to light), then the pixel in

question is not in shadow; otherwise thequestion is not in shadow; otherwise the fragment is shadowed

Page 34: Shadow Algorithms

1st Pass

View from lightView from light Depth Buffer (shadow map)

Page 35: Shadow Algorithms

2nd Pass

Visible surface depth

Page 36: Shadow Algorithms

2nd Pass

Final ImageNon-green in shadow

Final Image

Page 37: Shadow Algorithms

Shadow Maps WithShadow Maps With OpenGL/GLSL

Render scene using the light as a camera Render the depth buffer into a 2D texture.

We now have a depth texture. Render the scene using the real camera

Pass the transformation matrices as before to your Pass the transformation matrices as before to your shader (modelview, modelview projection, etc)

Pass one more matrix: ShadowMatrix ShadowMatrix is to transform the vertices from

local space to light’s clip space Multiply your vertex positions by the ShadowMatrix p y y p y

to get the texture coordinates into the shadow map

Page 38: Shadow Algorithms

R d d th i t t tRender depth into a texturevoid Init_FBO() {

lT P t f (GL TEXTURE 2DglGenFramebuffers(1, &shadowFBO); glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO);

GLfloat border[] = {1.0f, 0.0f, 0.0f, 0.0f}; GLuint depthTex;

glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);

GLuint depthTex;

glGenTextures(1, &depthTex); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, depthTex);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);

glDrawBuffer(GL_NONE); glReadBuffer(GL_NONE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 512,512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTex, 0);

// check FBO statusGLenum FBOstatus =

glCheckFramebufferStatusEXT(GL FRAMEBUFFER);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,

GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,

GL_CLAMP_TO_BORDER);

glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);if(FBOstatus != GL_FRAMEBUFFER_COMPLETE)

printf("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO\n");

glBindFramebuffer(GL_FRAMEBUFFER, 0); // go back to the default framebuffer

}

Page 39: Shadow Algorithms

Sh d M t iShadow Matrix

Shadow Matrix = B * P * V * M Shadow Matrix = B * PL * VL * M PL : Light’s projection matrix VL: Light’s view matrix M: Object’s model matrix B: change the range from [-1,1] to [0,1]

Page 40: Shadow Algorithms

Sh d M L kShadow Map Look up

Multiply the shadow matrix to the vertex’s Multiply the shadow matrix to the vertex s local coordinate position -> [s,t,r,w]P th lt t th f t h d Pass the result to the fragment shader

In the fragment shader Perform perspective division [s,t,r,w]/w Use (s/w, t/w) to look up the shadow map

C / i h h l i h h d Compare r/w with the value stores in the shadow map

If r/w is closer (smaller) fragment is not in shadow If r/w is closer (smaller), fragment is not in shadow Otherwise it is in shadow

Page 41: Shadow Algorithms

Generate texture coordinates t l k h dto look up shadow map Goal: compare the fragment’s distance to the light to the Goal: compare the fragment s distance to the light to the

value stored in the corresponding position in the shadow map

Major steps: Map the fragment’s position to the light’s projection (clip) space Generate texture coordinates (s t r) to look up the shadow map Generate texture coordinates (s,t,r) to look up the shadow map Compare the fragment’s depth value in the light’s clip space to

the value stored in the shadow map The fragment is not in shadow if the depth value is less than or The fragment is not in shadow if the depth value is less than or

equal to the value stored in the shadow map

uniform sampler2DShadow ShadowMap;vec4 texC = ShadowCoord; float shadow = shadow2DProj(ShadowMap, texC).x; gl_FragColor = v_color*shadow;

Page 42: Shadow Algorithms

Sh d iShadow map issues

Shadow quality depends on Shadow quality depends on Shadow map resolution – aliasing problem

Z resolution the shadow map is often stored in Z resolution – the shadow map is often stored in one channel of texture, which used to be only 8 bits, but now most of hardware supports 24 bits, pp

Self-shadow aliasing – caused by different sample positions in the shadow map and the screen

Page 43: Shadow Algorithms

Sh d li i blShadow map aliasing problem

The shadow looks blocky when one single The shadow looks blocky – when one single shadow map pixel covers several screen pixelspixels

This is a problem similar to texture magnificationmagnification

Page 44: Shadow Algorithms

Percentage Closer FilteringPercentage Closer Filtering

Could average binary results of all depth map pixels coveredpixels covered

Soft anti-aliased shadows