Shadow Rendering EDA101 Advanced Shading and Rendering 2006 Tomas Akenine-Möller 1
Why, oh why? (1) Shadows provide cues about spatial relationships among objects 2006 Tomas Akenine-Möller 2
Why, oh why? (2) Shadows increase the level of realism [Oh, well, flying bar stools may not be realistic] 2006 Tomas Akenine-Möller 3
Atmosphere... Why, oh why? (3) Neverwinter Nights Image courtesy of BioWare 2006 Tomas Akenine-Möller 4
Who s responsible? In a ray tracer, you kind of get them for free Very little coding, good results Though, soft shadows can be very expensive For real-time rendering, using, e.,g OpenGL You re responsible Often need quite a bit of coding Often need to be aware of artifacts Can be costly 2006 Tomas Akenine-Möller 5
Definitions Light sources Shadows occluder and receivers Light source Occluder Occluder and receiver Receiver N.B. Occluder could be called Creator 2006 Tomas Akenine-Möller 6
Hard vs soft shadows Two different light source types point source area source umbra penumbra umbra 2006 Tomas Akenine-Möller 7
Umbra and penumbra Point light source Area light source umbra umbra penumbra 2006 Tomas Akenine-Möller 8
Topics of today s lecture The shadow mapping algorithm The shadow volume algorithm Soft shadows A real-time soft shadow volume algorithm 2006 Tomas Akenine-Möller 9
Ways of thinking about shadows As separate objects (like Peter Pan's shadow) As volumes of space that are dark As places not seen from a light source looking at the scene 2006 Tomas Akenine-Möller 10
Shadow mapping Introduced by Lance Williams in 1978 Used by many (e.g., Pixar s Renderman) Can also be used with current graphics cards in real time Conceptually very simple: Render to Z-buffer as seen from the light source this buffer is the shadow map Render from the eye, and transform each pixel into the coordinate system of the light Compare depths shadow or lit 2006 Tomas Akenine-Möller 11
The shadow map Turn off texturing, writes to the color buffer, etc. Only write to Z-buffer Aim the frustum of the light towards the region that should contain shadows White is far, black is near 2006 Tomas Akenine-Möller 12
Using the shadow map When scene is viewed, check viewed location in light's shadow buffer If point's depth is greater than shadow depth, object is in shadow. shadow depth map For each pixel, compare distance to light with the depth stored in the shadow map 2006 Tomas Akenine-Möller 13
Shadow map The result Need to regenerate the shadow map for dynamic scenes, but it can be reused for static scenes. 2006 Tomas Akenine-Möller 14
Shadow mapping problems (1) Low resolution of shadow map Gives jagged shadow edges Lots of research, and improvements exist Image courtesy Marc Stamminger 2006 Tomas Akenine-Möller 15
Shadow mapping problems (2) Self-shadowing What if an object shadows itself? floating point imprecision (since you use a floating point matrix transform) But more importantly, a screen space pixel seldom maps exactly to the center of a shadow map pixel! 2006 Tomas Akenine-Möller 16
Shadow mapping problems (2) Choosing bias (epsilon) is not trivial! Light source One pixel in the shadowmap Depth stored in SM, d sm Depth of pixel when rendered from the eye, d eye Assume that the ellipse and rectangle is the same surface We do not want incorrect self-shadowing Solution: add bias d sm + bias < d eye shadow 2006 Tomas Akenine-Möller 17
Too low bias You need to make sure the surface seen by the light does not shadow itself Surface acne 2006 Tomas Akenine-Möller 18
Too high bias Too much bias and the shadow floats. Image also shows jaggedness of shadow boundary 2006 Tomas Akenine-Möller 19
The shadow volume algorithm Crow's (1977) idea Shadow volumes define volumes of space in shadow. From the School of Leonardo Da Vinci 2006 Tomas Akenine-Möller 20
Shadow volume concept Shadow volume concept Create volumes of space in shadow from each polygon in light. Each triangle creates 3 projecting quads 2006 Tomas Akenine-Möller 21
Using the volume To test a point, count the number of polygons between it and the eye. If we look through more frontfacing than backfacing polygons, then in shadow. backfacing frontfacing 2006 Tomas Akenine-Möller 22
The stencil buffer Used by the shadow volume algorithm Is just another buffer (often 8 bits per pixel) When rendering to it, we can add, subtract, etc Then, the resulting image can be used to mask off subsequent rendering Stencil Buffer Mask Rendered image result 2006 Tomas Akenine-Möller 23
Z-pass by example: how the stencil buffer is used What we have... What we wnat... 2006 Tomas Akenine-Möller 24
How To Do It (Z-pass) A four pass process [Heidmann91]: 1st Pass: render the scene with just ambient and emission lighting. Turn off updating Z-buffer and writing to color buffer (i.e. Z-compare, draw to stencil only). 2nd pass: render front facing shadow volume polygons to stencil buffer, incrementing count. 3rd pass: render backfacing shadow volume polygons to stencil, decrementing. 4th pass: render diffuse,ambient and specular where stencil buffer is 0. 2006 Tomas Akenine-Möller 25
Eye Location Problem If the eye location is inside one or more shadow volumes, count is wrong. Eye problem fixed by clearing stencil buffer to # of volumes eye is inside. 2006 Tomas Akenine-Möller 26
Frustum Location Problem Frustum s near (hither) plane overlaps a shadow volume, frontface counts missed Some form of capping is needed to make the shadow volume whole again (not trivial, nor robust) 2006 Tomas Akenine-Möller 27
Solution: Count Beyond Surface Z-fail-algorithm Render to stencil only when shadow volume Z >= stored Z! must cap ends of shadow volumes (or project to infinity, w=0 for vertices) 2006 Tomas Akenine-Möller 28
Why This Works (f - b) in front of plane = (b - f) in back 2-2 = 0 violet - red 2 = 2 1 = 2-1 2006 Tomas Akenine-Möller 29
Z-fail by example 2006 Tomas Akenine-Möller 30
Merging shadow volumes Edge shared by two polygons facing the light creates front and backfacing quad. This interior edge makes two quads, which cancel out Instead, use only potential silhouette edges as seen from the light: 2006 Tomas Akenine-Möller 31
Shadow maps vs shadow volumes Shadow Volumes Good: Anything can shadow anything, including self- shadowing, and the shadows are crisp. Bad: 3 or 4 passes, shadow polygons must be generated and rendered (lots of polygons & fill), CPU intensive, complicates skinning. Ugly: counting problems if view frustum overlaps. Shadow Maps Good: Anything to anything, constant cost regardless of complexity, map can sometimes be reused. Bad: Frustum limited. Ugly: Jagged shadows if res too low, biasing headaches. 2006 Tomas Akenine-Möller 32
A soft shadow volume algorithm In real time... 2006 Tomas Akenine-Möller 33
Soft shadows 2006 Tomas Akenine-Möller 34
Geometrically-correct soft shadows Light samples (L) L*R rays to test!! Receiver samples (R) T: num triangles in scene L*R rays is a lot of work For example, 200 * (1920x1080) = 400M rays Are the rays blocked? Ray tracing O(log T) soft shadows O(L*R*log T) 2006 Tomas Akenine-Möller 35
Soft shadow algorithms in general (Real-time...) Fundamental and inherently difficult problem in computer graphics 2006 Tomas Akenine-Möller 36
The general idea: Penumbra Wedges A primitive for bounding the penumbra region imposed by a silhouette edge In two dimensions A single penumbra wedge 2006 Tomas Akenine-Möller 37
Important simplification Compute potential silhoutte edges as seen from the center of the light source Use one penumbra wedge per such edge 2006 Tomas Akenine-Möller 38
A wedge for each silhouette edge 2006 Tomas Akenine-Möller 39
A wedge for each silhouette edge 2006 Tomas Akenine-Möller 40
A wedge for each silhouette edge 2006 Tomas Akenine-Möller 41
A wedge for each silhouette edge 2006 Tomas Akenine-Möller 42
A wedge for each silhouette edge 2006 Tomas Akenine-Möller 43
Rasterizing the wedges 2006 Tomas Akenine-Möller 44
Rasterizing the wedges 2006 Tomas Akenine-Möller 45
Rasterizing the wedges 2006 Tomas Akenine-Möller 46
Rasterizing the wedges 2006 Tomas Akenine-Möller 47
Rasterizing the wedges Skipping wedge construction see reading list 2006 Tomas Akenine-Möller 48
Visibility computations Really want to compute how much of the light source that we can see 2006 Tomas Akenine-Möller 49
Each silhouette edge s contribution to visibility From the point-to-be-shaded, project the edge onto the light source Compute the area, called coverage, of the dark gray region Add/subtract to a visibility buffer 2006 Tomas Akenine-Möller 50
Precomputed 4D textures 2006 Tomas Akenine-Möller 51
Used as a lookup table Due to intelligent caches fast! Can have textured lights (and animated) 32x32 light texture 3 MB Why 4D textures? 2006 Tomas Akenine-Möller 52
Some examples using textured light sources 2006 Tomas Akenine-Möller 53
Example of how it works 2006 Tomas Akenine-Möller 54
The algorithm 1st pass: Render hard shadow quads (as usual) To be sure that we register when we enter/exit umbra 2nd pass: compensate for overstated umbra 2006 Tomas Akenine-Möller 55
Combine shadow mask with lighting and texturing = = 2006 Tomas Akenine-Möller Images courtesy of Jonas Svensson and Ulf 56 Borgenstam
Disadvantages of Soft Shadow Volumes Cases handled 100% correctly: An arbitrary non selfintersecting, planar polygon If the silhouette of the object is the same from all points on the area light source There are still a few approximations Shadow volumes do in general not scale well with scene complexity 2006 Tomas Akenine-Möller 57
Artifacts: Single silhouette error Overlapping geometry is handled incorrectly 2006 Tomas Akenine-Möller 58
Comparison Hard vs Soft Hard to beat the soft! Soft is more realistic Soft contains less high frequency content Soft provides better spatial relationship cues Soft seldom gives aliasing effects But... Soft costs more More research to be done before the final answer is here!! 2006 Tomas Akenine-Möller 59
Required reading list (i.e., if you want to pass the exam, read those papers) Section 6.12 (intro), 6.12.2-6.12.4 in Real-Time Rendering, Tomas Akenine-Möller and Eric Haines, 2nd edition, 2002. Paper copies will be available. Ulf Assarsson, and Tomas Akenine-Möller, A Geometrybased Soft Shadow Volume Algorithm, ACM Transactions on Graphics (Proceedings of ACM SIGGRAPH), vol. 22, no. 3, pp. 511-520, July 2003 2006 Tomas Akenine-Möller 60