Shadow Mapping

A point is lit if it is “visible” EECS 487: Interactive from the light source • similar to visible surface determination Shadow computation by Lecture 32: Interactive Visual Effects simulating eye at light • Shadow Map position •

Durand

Shadow Mapping Shadow Mapping Requires 2 passes through the pipeline First pass: compute shadow map Second pass: shadow determination (depth of closest pixels to the light) using the shadow map • render scene from light For each pixel • populate z-buffer that we’ll use as our shadow map • do normal z-buffer computation • store the distance from light to nearest object to check visibility from eye • white is far, black is near shadow map • if visible, look up distance to light scene from light’s point of view • perspective project visible pixels from eye scene from eye’s point of view depth value space back to light space • lookup depth stored in shadow map • if distance to light is (epsilon) greater than stored depth, pixel is in shadow

Foley et al., Akenine-Möller02,Durand Foley et al., Akenine-Möller02,Durand Shadow Mapping with OpenGL Shadow Mapping with OpenGL

Create the shadow map: Determine eye-space, light-space correspondences: • render the scene with eye at light position • unproject every pixel in the z-buffer to obtain object • save the resulting z-buffer and projection matrix, coordinates, using gluUnproject(…) and the these are the shadow map and shadow projection matrix viewpoint projection matrix • glReadPixels(…) to read back z-buffer • glGetDoublev(GL_MODELVIEW_MATRIX, …), • project the object coordinates into the shadow map using glGetDoublev(GL_PROJECTION_MATRIX, …), gluProject(…) and the shadow projection matrix glGetIntergerv(GL_VIEWPORT, …)

Render scene: Shadow rendering: • compare the resulting z values with the corresponding • render the scene from the actual viewpoint content of the shadow map and update the color buffer to • save both color and z-buffers and viewpoint projection matrix draw the projected shadows • write back the modified color buffer using glDrawPixels(…)

Yu Yu

Limited Field of View Limitations of Shadow Maps What if a point/ is outside field of 1. Limited field of view, no omni-directional light view of the shadow map?

2. Limited depth resolution Use six shadow maps, to form a cube 3. Shadow map aliasing enclosing the light • requires a separate rendering 4. Hard shadows only (with jaggies) pass for each shadow map! (or soft shadows only if PCF is used)

Durand Durand Limited Depth Resolution Shadow Map Aliasing Due to z-fighting, distance to light and stored depth may not compare correctly Results from sampling rates • add an ε to depth in shadow map mismatch (re-sampling to prevent unintended (self-)shadowing problem): sampling rate (pixel • in computing shadow map move geometry away from light by a small amount size) of light is mismatched to • choosing correct ε value is tricky that of the eye

ε precision Precision error also error possible with projection shadows

Merrell Kilgard

Shadow Map Aliasing Shadow Map Aliasing Shadow “fragment” is also Least aliasing when light frustum is reasonably stretched when eye is close to well aligned with the eye’s view frustum: the the surface but light is far away ratio of sample sizes is close to 1 • surfaces that are nearly edge-on to the eye face the light directly reference image • best case if eye and light frusta are nearly identical • results in under-sampling of near (“miner’s lamp” case) field in shadow map • but only limited scene setups satisfy this • worst case is when light is shining at the viewer (“deer-in-the-headlights” case) • also known as the “dueling frusta” problem

projected fragment

Kilgard Durand,Merrell,RTR3 standard shadow map Shadow Map Anti-Aliasing Shadow Map Anti-Aliasing

Possible solutions: Asymmetric frustrum for shadow map rendering 1. increase shadow map resolution 2. split shadow map into several slices

different projections, same resolution 3. use asymmetric frustrum for shadow map rendering 4. average nearby pixels

Durand,RTR3 RTR3,Martin&Tan

Percentage-Closer Filtering Soft-Shadows with Shadow Map depth values Look up several nearby shadow map fragments, not just one Compute average shadow value for the neighborhood

If so, (5/9) • use immediate neighbors for anti-aliasing occludes • surface use neighbors further afield for soft-shadows Sampling strategy: grid, jittered, or adaptive for Results in aliased shadow improved performance Anti-aliasing by averaging several nearby shadow map fragments What to average? Depth values? • No, 1.2 < 49.8, but so is 22.9: still a binary result, no anti-aliased blurring Instead, perform depth test for a neighborhood of pixels • then compute percentage of lit pixels • (this makes ε computation even trickier!)

Durand,Schulze Percentage-Closer Filtering Ambient Occlusion Supported in hardware for small (2×2) filters All the real-time shadow algorithms assume directional lighting only • shadow map coordinates generated using projection matrix Ambient occlusion is shadows due to • shadow map stored as texture: modern hardware permits tests on texture values global indirect/ambient lighting • can use larger filters with additional rendering passes Can be pre-computed using and baked into a texture limited to static scenes

Yang et al., Durand,Schulze TP3

Ambient Occlusion SSAO Two approaches: • Screen-space Ambient Occlusion (SSAO) At each point find fraction of • Screen-space Directional Occlusion (SSDO) hemisphere that is occluded • visible fraction: 1-occlusion Basic idea: • modulate diffuse by • approximate indirect lighting using a uniform, (1−occlusion) distant environment irradiance • cd = md sd max((n•l), 0) (1−occlusion) • simulate the darkening effect where ambient light is blocked by other geometry in the scene Alternately, to avoid , • compute the portion of the hemisphere around a point that is blocked sample a circle around point • purely geometric, independent of lighting • compare depth of samples conditions or viewing direction against z-buffer content • sample is not occluded if

depth < z-buffer content −Z

TP3 Bala, Yang, Yip, Xu n SSDO b Shadow and Environment Maps

SSAO plus EM and indirect lighting: p • instead of modulating diffuse shading by (1−occlusion), Basic method to add realism to interactive rendering: add up light contribution from unoccluded directions only instead of ray tracing, use image-based methods • compute average un-occluded direction (“bent normal”, b) • Shadow maps: image-based hard shadows [Williams78] • lookup light/environment map using the “bent normal” • many recent extensions result in light-colored shadow instead of just grey • widely used even in software rendering (RenderMan) • (can add a single bounce off facing occluding surfaces) • Environment maps: image-based complex lighting Alternately, to avoid ray tracing, [Blinn&Newell76] • sample uniformly across the hemisphere • huge amount of recent work above point, this gives N light vectors • sample each light vector at random offsets from the point Together, give many “realistic” effects • compare depth of samples against z-buffer content • sample is not occluded if depth < z-buffer content • but cannot be easily combined!

Ritschel,Grosch,Seidel09 Ramamoorthi