280 likes | 527 Views
Shadows CS 446: Real-Time Rendering & Game Technology. David Luebke University of Virginia. Demo. Today: Jiajun Zhu Tuesday: Ted Yokoyama . Assignments. Reminder: integrated version of assn 4 due today Group turns in whole project today
E N D
Shadows CS 446: Real-Time Rendering & Game Technology David Luebke University of Virginia
Demo • Today: Jiajun Zhu • Tuesday: Ted Yokoyama Real-Time Rendering
Assignments • Reminder: integrated version of assn 4 due today • Group turns in whole project today • Individuals taking late days turn in a new version of whole project when their changes are integrated • Remember, show up to class or assignment due in the morning… Real-Time Rendering
Recap: Shadow Notation • Light source • Point vs area • Occluders & receivers • Identify ahead of time? • Self-shadowing? • Shadow • Umbra • Penumbra • Soft vs hard shadows Real-Time Rendering
Recap: Planar Shadows • Old trick: project the occluder geometry to a plane and render over ground plane • Can do with a matrix • Z-bias issues • Semiopaque shadows harder • Stencil and Z-buffer tricks • Another option: generate textured rectangle • Problems • Light source inside object (Antishadows) • Only planar receivers no self-shadowing Real-Time Rendering
Planar Soft Shadows • Basic idea: • Sample light source multiple times, average results (accumulation buffer) into a texture • Gooch et al: move plane up and down • Nested shadows fewer passes Real-Time Rendering
Projective Shadows • Render scene from light’s point of view • Render all occluders as black • Can turn off depth buffer etc • Project onto receiver polygons using projective texture mapping • Works for curved surfaces • Designer separates occluders and receivers no self-shadowing Real-Time Rendering
Shadow Volumes • Basic idea: • Create polygonal objects to represent shadowed volumes • Make clever use of stencil buffer so that these objects affect what lighting is done Real-Time Rendering
Stencil Buffer • The stencil buffer has been around since OpenGL 1.0 • Basic idea: provide a per-pixel flag to indicate whether pixels are drawn or not • But… • Let that flag be an integer (usually 8 bits) • Usually shared with depth buffer • And let drawing operations increment or decrement the stencil buffer based on different events • Always, depth-pass, depth-fail, etc. Real-Time Rendering
Shadow Volumes • Details of the basic algorithm: • Compute shadow volumes • View-independent! • Clear stencil buffer • Render the scene without diffuse/spec lighting • “Render” front faces of shadow volumes • Turn off color, depth updates (but leave depth test on) • Visible polygons increment pixel stencil buffer count • “Render” back faces of shadow volumes • Turn off color, depth updates (but leave depth test on) • Visible polygons decrement pixel stencil buffer count • Render scene with only diffuse/spec lighting • Only update pixels where stencil = 0
Shadow Volumes • Refinements (see book, next slides) • NV30, XBox supports signed stencil addition • Two-sided lighting determines whether polygon adds or subtracts 1 from stencil buffer • One-pass algorithm! But a little slower in practice? • What if you’re inside a shadow volume? • Invert meaning of stencil test • What if near clip intersects shadow plane? • Carmack, others: use z-fail test • Clever extensions in NV2X help this idea out • Creating shadow volumes: vertex program! • ATI: clever degenerate-edge trick again
Shadow Volumes • Advantages: • Robust • Self-shadowing • GPU • Disadvantages: • Can be geometry limited • Stencil polys • Multi-pass scene geometry • Can be fill limited • Stencil test – per pixel expense • Hard shadows
Shadow Volumes • Will return to the gruesome details shortly Real-Time Rendering
Shadow Maps • Idea: • Render scene from light source, read Z-buffer • Result: discretized image (shadow map) telling distance of objects to light source • Render scene normally • At each pixel, calculate distance D to light • Compare to distance S stored in shadow map • If D=S, surface lit by light, else in shadow Real-Time Rendering
Shadow Maps • The basic algorithm (w/o hardware) • Render scene with ambient lighting only • Read Z-buffer, transform values into coordinate system of light • Use comparison to set alpha buffer • Render w/ diffuse and specular components, multiplying by alpha Real-Time Rendering
Shadow Maps • Advantages: • Hardware-accelerated general shadow algorithm • Supports self-shadowing • Cost is linear in # lights and # polygons • Disadvantages: • Self-shadow aliasing • Biasing and other techniques can help, but not fix • Shadow map resolution critical! • Solution: perspective shadow maps Real-Time Rendering
Taken from: Practical & RobustStenciled Shadow Volumes for Hardware-Accelerated Rendering Cass Everitt & Mark J. Kilgard, NVIDIA GDC 2003 See the accompanying paper at: http://developer.nvidia.com/object/robust_shadow_volumes.html Also see: http://developer.nvidia.com/object/fast_shadow_volumes.html
2D Cutaway of a Shadow Volume Surface outsideshadow volume (illuminated) Shadowingobject Lightsource Shadowvolume (infinite extent) Eye position (note that shadows are independent of the eye position) Surface insideshadow volume (shadowed) Partially shadowed object
Shadow Volume Advantages • Omni-directional approach • Not just spotlight frustums as with shadow maps • Automatic self-shadowing • Everything can shadow everything, including self • Without shadow acne artifacts as with shadow maps • Window-space shadow determination • Shadows accurate to a pixel • Or sub-pixel if multisampling is available • Required stencil buffer broadly supported today • OpenGL support since version 1.0 (1991) • Direct3D support since DX6 (1998)
Shadow Volume Disadvantages • Ideal light sources only • Limited to local point and directional lights • No area light sources for soft shadows • Requires polygonal models with connectivity • Models must be closed (2-manifold) • Models must be free of non-planar polygons • Silhouette computations are required • Can burden CPU • Particularly for dynamic scenes • Inherently multi-pass algorithm • Consumes lots of GPU fill rate
Visualizing Shadow Volumes in 3D • Occluders and light source cast out a shadow volume • Objects within the volume should be shadowed Lightsource Scene with shadows from an NVIDIA logo casting a shadow volume Visualization of the shadow volume
Visualizing the Stencil Buffer Counts Shadowed scene Stencil buffer contents Stencil counts beyond 1 are possible for multiple or complex occluders. red = stencil value of 1green = stencil value of 0 GLUT shadowvol example credit: Tom McReynolds
Algorithm • Render scene to initialize depth buffer • Depth values indicate the closest visible fragments • Use 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 in shadow, and zero if illuminated
Why Eye-to-Object Stencil Enter/ Leave Counting Approach Works Lightsource Shadowing object zero +1 zero +2 +2 Eyeposition +1 +3
+ + + - - - Illuminated,Behind Shadow Volumes Lightsource Shadowing object zero +1 zero Unshadowedobject +2 +2 Eyeposition +1 +3 Shadow Volume Count = +1+1+1-1-1-1 = 0
+ + + - Shadowed,Nested in Shadow Volumes Lightsource Shadowing object zero +1 zero Shadowedobject +2 +2 Eyeposition +1 +3 Shadow Volume Count = +1+1+1-1 = 2
Illuminated,In Front of Shadow Volumes Lightsource Shadowing object zero +1 zero Shadowedobject +2 +2 Eyeposition +1 +3 Shadow Volume Count = 0
Problems Created by Near Plane Clipping (Zpass approach) Missed shadow volume intersection due to near clip plane clipping; leads to mistaken count Far clipplane zero +1 +1 +2 zero +3 +2 Near clipplane