Graphics & Visualization
Chapter 13
Shadows
Graphics & Visualization: Principles & Algorithms Chapter 13
Shadows Graphics & Visualization: Principles & Algorithms - - PowerPoint PPT Presentation
Graphics & Visualization Chapter 13 Shadows Graphics & Visualization: Principles & Algorithms Chapter 13 Introduction Wherever there is light, there is shadow Shadows are not just a photorealistic image
Graphics & Visualization: Principles & Algorithms Chapter 13
Graphics & Visualization: Principles & Algorithms Chapter 13
u Perspective cannot always give enough clues about the
u Shadows help resolve part of the depth ambiguities that
2
Graphics & Visualization: Principles & Algorithms Chapter 13
(a) A lit scene showing a ball in front of a staircase
(b) Size/distance ambiguity, when scene perceived from viewpoint
(c)–(e) Images rendered from the same viewpoint as (a). Shadows help us define the object relative to its surroundings
3
Graphics & Visualization: Principles & Algorithms Chapter 13
help our vision system justify the lack of detail-related contrast
4
Graphics & Visualization: Principles & Algorithms Chapter 13
Umbra: surface area where shadow is cast with full light-source occlusion
Penumbra: area where shadow is partially lit by the light-emitting source
As distance affects the apparent size of objects, the apparent projection of the light emitter on the surface needs to be non-negligible to create a penumbra
5
Graphics & Visualization: Principles & Algorithms Chapter 13
(a) Point light source (hard shadow) (b) Small non-infinitesimal light source (soft shadow) (c) Large area light (soft shadow) (d) Infinite (directional) light source (hard shadow)
6
Graphics & Visualization: Principles & Algorithms Chapter 13
It extends away from the light source to infinity, unless the light source has a
local effect & a finite range:
Shadow volume extends up to the range of influence of light source
7
Graphics & Visualization: Principles & Algorithms Chapter 13
Shadow shafts produced by directional lights are prismatic volumes with parallel sides and the resulting shadows neither converge nor diverge
8
Graphics & Visualization: Principles & Algorithms Chapter 13
Shadow volumes: works in object space and is ideal for casting hard, precise shadows on polygonal objects
Shadow mapping: works in image/texture space, it is applicable in a wide range of geometric entity representations, can be adapted to handle semi- transparent and partially occluding media, NOT effective in producing sharp- edged shadows.
9
Graphics & Visualization: Principles & Algorithms Chapter 13
Requires that the occluders are polygonal
Assumes that connectivity information is available for these meshes
Or can be determined as a pre-processing step
10
Graphics & Visualization: Principles & Algorithms Chapter 13
Is a buffer, allocated in the graphics hardware or the system memory
Implements a counter and comparator unit per image pixel
Is equal in dimension to the frame buffer and usually has a resolution of 8 bpp
11
Graphics & Visualization: Principles & Algorithms Chapter 13
(a) Stencil buffer update pass: writing to the frame buffer is disabled and the operation on the stencil values is set to always increase (b) Conditional rendering pass in the frame buffer: fragments are rendered only if the corresponding stencil values are equal to zero.
12
Graphics & Visualization: Principles & Algorithms Chapter 13
replacing,
setting,
maintaining,
incrementing,
decrementing,
inverting the current values
13
Graphics & Visualization: Principles & Algorithms Chapter 13
A counter is incremented each time the eye-to-fragment line enters a shadow volume and decremented when it exits
The surface is in shadow when the counter is other than 0
If the rendered point lies within the shadow volume, surface will be hit before ray exits one or more of the overlapping shadow volumes counter > 0
14
Graphics & Visualization: Principles & Algorithms Chapter 13
1. Initialization: Clear depth and frame buffers, ignore the stencil operations 2. Shadow lighting: Render the geometry using only the indirect illumination components and emissive effects (lights off) 3. For each occluder, prepare and render the shadow volume: (a) Shadow volume construction. Construct a closed shadow volume by extruding the faces of the caster away from light source. (b) Volume Z-pass test. Render the front-facing polygons of the shadow volumes without updating frame or depth buffer. Each time a fragment successfully passes the depth test, the stencil buffer is incremented. (c) Render the back-facing polygons of the shadow volumes, without depth and frame buffer updates. This time, decrement the corresponding stencil values when fragments pass the depth test. 4. Render the lit geometry. Enable the light source and shade all pixels whose corresponding stencil value is zero.
15
Graphics & Visualization: Principles & Algorithms Chapter 13
16
Graphics & Visualization: Principles & Algorithms Chapter 13
17
Graphics & Visualization: Principles & Algorithms Chapter 13
18
Graphics & Visualization: Principles & Algorithms Chapter 13
19
Graphics & Visualization: Principles & Algorithms Chapter 13
This will create a shadow shaft for every polygon
The shadow volume of the entire object is the union of these polygonal frusta
All of the common interior shaft faces eventually cancel out in the stencil buffer
20
i i L i L i L i L
Graphics & Visualization: Principles & Algorithms Chapter 13
Therefore the shadow-volume sides are very long and need not be sized according to the caster position relative to the light source
considering that rL >> |pi −pL |:
21
i i L i L i L
i i L
Graphics & Visualization: Principles & Algorithms Chapter 13
(a) Casters do not need to be convex and may be self-shadowed (b) Silhouette edges as seen from the point of view of the light (c) Silhouette edge determination (d) 2-triangle shaft-sides are formed by extruding the triangle or silhouette edges (e) The “bright” and “dark” caps are the polygons facing toward and away from the light source, respectively (f) The final closed shadow volume
22
Graphics & Visualization: Principles & Algorithms Chapter 13
Triangles on the occluder surface share edges, which, when extruded, will create triangles that share all vertices and eventually cancel each other
render all front-facing shadow volume polygons in one pass
render all back-facing polygons in another pass
23
Graphics & Visualization: Principles & Algorithms Chapter 13
More complex frusta generation stage
Far fewer polygons
For low polygon models
For static light-object relationships
24
Graphics & Visualization: Principles & Algorithms Chapter 13
Union of triangles also forms the “near” cap, relative to the light source
A silhouette-determination stage
An edge-extrusion stage
25
Graphics & Visualization: Principles & Algorithms Chapter 13
Set of all visible polygon edges that are shared by at least one back-facing and
For open 3D shapes, this set is extended to include all open edges
Determine all neighboring polygons
Mark the common edges
26
Graphics & Visualization: Principles & Algorithms Chapter 13
27
typedef struct { Point3f v[3]; Vector3f n[3]; Vector3f facenormal; long neighbor[3]; // <-- initialize to -1; } Triangle; int CommonEdge( Point3f a1, Point3f a2, Point3f b1, Point3f b2) { extern float weld_thres_squared; // avoid sqr. root Vector3f d1 = PointSubtract(a1-b1); Vector3f d2 = PointSubtract(a2-b2); return ( DotProduct(d1,d1) < weld_thres_squared && DotProduct(d2,d2) < weld_thres_squared ); }
Graphics & Visualization: Principles & Algorithms Chapter 13
void FindConnectivity (Triangle *tri, long numOfTris) { long i,j; int k,n; for (i=0;i<numOfTris-1;i++) for (j=i+1;j<numOfTris;j++) for (k=0;k<3;k++) for (n=0;n<3;n++) if ( CommonEdge(tri[i].v[k], tri[i].v[(k+1)%3] tri[j].v[n], tri[j].v[(n+1)%3] ) { tri[i].neighbor[k] = j; // edge v[k]-v[k+1] is shared with tr. j tri[j].neighbor[n] = -1; // mark adjacency only on one triangle to // avoid double edges during sil. // detection Break; } }
28
Graphics & Visualization: Principles & Algorithms Chapter 13
29
ˆ ˆ · · ·
i L i j L j
n p p n p p
typedef struct { Point3f * edgeVertex; int numOfEdges; } edgeList;
Graphics & Visualization: Principles & Algorithms Chapter 13
edgeList* FindSilhouette( Triangle * tri, long numOfTris, Vector3f light, boolean infinite) { long i,j, edges=0; int k; Vector3f L, ni, nj; float visi, visj; // Allocate a large edge buffer: Point3f * endpts = (Point3f*)malloc(sizeof(Point3f)*numOfTris*3*2); (continued)
30
Graphics & Visualization: Principles & Algorithms Chapter 13
for (i=0; i<numOfTris; i++) for (k=0; k<3; k++){ j = tri[i].neighbor[k]; if (j!=-1) // if neighbor is marked: { ni=tri[i].facenormal; L = infinite? VectorInvert(light): VectorNorm(PointSubtract(light,tri[i].v[0]); visi = DotProduct(ni,L); nj = tri[j].facenormal; L = infinite? VectorInvert(light): VectorNorm(PointSubtract(light,tri[j].v[0]); visj = DotProduct(nj,L); if (visi*visj<0) { PointCopy(endpts[2*edges+0],tri[i].v[k]; PointCopy(endpts[2*edges+1],tri[i].v[(k+1)%3]; edges++; } } } (continued)
31
Graphics & Visualization: Principles & Algorithms Chapter 13
edgeList * list = (edgeList*)malloc(sizeof(edgeList)); list->numOfEdges = edges; list->edgeVertex =(Point3f*)malloc(sizeof(Point3f)*2*edges); for (i=0;i<2*edges;i++) PointCopy(list->edgeVertex[i],endpts[i]); free(endpts); return(list); }
32
Graphics & Visualization: Principles & Algorithms Chapter 13
Near cap of the shadow volume consists of all the triangles that face towards the light source
Far cap is made of the rest of the mesh’s faces, after extruding their vertices
The shadow-volume sides are the extruded silhouette edges.
33
Graphics & Visualization: Principles & Algorithms Chapter 13
34
Graphics & Visualization: Principles & Algorithms Chapter 13
This also includes the case when the viewpoint lies inside the shadow volume
Cases may occur where view plane is entirely in shadow while the viewer is
35
Graphics & Visualization: Principles & Algorithms Chapter 13
Removes the near clipping problem but raises the same problem at the far clipping plane
Fortunately, it is easier to ensure that the far clipping plane does not intersect the shadow volumes:
Either by consistently preparing the far caps of the shadow volume
Or by pushing the clipping plane to infinity
36
Graphics & Visualization: Principles & Algorithms Chapter 13
1. Initialization: Clear depth and frame buffers, ignore the stencil operations 2. Shadow lighting: Render the geometry using only the indirect illumination components and emissive effects (lights off) 3. For each occluder, prepare and render the shadow volume: (a) Shadow volume construction. Construct a closed shadow volume by extruding the faces of the caster away from light source. (b) Volume Z-fail test. Render the back-facing polygons of the shadow volumes without updating frame or depth buffer. Each time a fragment fails the depth test, the stencil buffer is incremented. (c) Render the front-facing polygons of the shadow volumes, without depth and frame buffer updates. This time, decrement the corresponding stencil values when depth test fails. 4. Render the lit geometry. Enable the light source and shade all pixels whose corresponding stencil value is zero.
37
Graphics & Visualization: Principles & Algorithms Chapter 13 38
Graphics & Visualization: Principles & Algorithms Chapter 13 39
Graphics & Visualization: Principles & Algorithms Chapter 13
Instead counting shadow volume enters/exits along the eye-pixel ray starting from the eye & ending at the nearest visible fragment:
Z-fail algorithm counts ray intersections starting from infinity & ending at the nearest visible fragment
All backfacing shadow-volume fragments should be closer than or at “infinity”
40
Graphics & Visualization: Principles & Algorithms Chapter 13
41
Graphics & Visualization: Principles & Algorithms Chapter 13
Solid angle subtended by the silhouette increases
Radial distance remains fixed
By estimating the limit of the matrix as the far clipping plane distance tends to infinity
42
Graphics & Visualization: Principles & Algorithms Chapter 13
Only the 3rd row of the matrix is affected by the limit operation
It holds:
43
2 2 2 1 n r l r l r l n t b t b t b f n nf f n f n P
lim / 1
x
x a x b
Graphics & Visualization: Principles & Algorithms Chapter 13
according to whether the viewport intersects the shadow volume or not
44
inf
2 2 lim 1 2 1
f
n r l r l r l n t b t b t b n
P P
Graphics & Visualization: Principles & Algorithms Chapter 13
The stencil-buffer values can only be incremented that many times
45
Graphics & Visualization: Principles & Algorithms Chapter 13
Use depth buffer to sort surfaces with respect to the light source line of sight
Rendering software
Real-time applications
47
Graphics & Visualization: Principles & Algorithms Chapter 13
48
Graphics & Visualization: Principles & Algorithms Chapter 13
the far clipping plane can be adjusted to rL
49
L L L
a a
Graphics & Visualization: Principles & Algorithms Chapter 13
Transform every object according to ML
Project it using PL
Store corresponding depth map ZL (shadow map)
Point p = (x, y, z) on a surface is shadowed if located at greater distance than the value stored in ZL when p expressed in the light source's viewport coordinates:
p΄ = (x’, y’, z’) = PL ML
> ZL (x’, y’)
50
ˆ ˆ ˆ ( , , , )
L L L L
p u v n
Graphics & Visualization: Principles & Algorithms Chapter 13
(a) Spotlight set-up (b) Depth-buffer capture of the scene from the point of view of the light source (c) Final pass: Each fragment is tested for visibility against the light-space depth map
51
Graphics & Visualization: Principles & Algorithms Chapter 13
Multiple shadow maps to cover the sphere around the point light source so that any direction on the unit sphere centered at pL is addressable
If multiple light sources participate in the scene, above procedure is duplicated for each one of them
Only whenever the lit environment changes or the light source moves
Change in the camera viewpoint has no impact on the shadow map
Take advantage of projective texture-coordinate transformations
Take advantage of complex texture-component manipulations
52
Graphics & Visualization: Principles & Algorithms Chapter 13
Have to transform these eye-clip-space coordinates back to world space coordinates and then to light-clip space:
53
1 · 1 frag frag frag frag frag
L L C C
Graphics & Visualization: Principles & Algorithms Chapter 13
frustum set-up
The WCS-to-VCS transformation of this pass needs to be inverted and stored (see equation (13.5))
Requires all eye-clip-space fragments to be transformed to the normalized texture space of the shadow map
Transformed fragments are scaled and translated so that (x’frag , y’frag ) pair may correspond to the (u, v) shadow-map texture coordinates & z’frag can be compared to the stored, normalized depth map
54
Graphics & Visualization: Principles & Algorithms Chapter 13
55
Graphics & Visualization: Principles & Algorithms Chapter 13
56
Graphics & Visualization: Principles & Algorithms Chapter 13
Affected by the image-level depth comparisons & depth-map rendering time
Shadow maps can be used with any type of rendering primitive, provided that a depth buffer can be prepared from the rendered fragments of the geometry
If a fragment is A-culled in the light-space rendering pass, it will not produce a shadow in the eye-space pass
This fact can be exploited to produce elaborate shadows from simple geometry
57
Graphics & Visualization: Principles & Algorithms Chapter 13
Footprint size sshadow
varies according to the distance from & the angle at which a surface is encountered
58
Graphics & Visualization: Principles & Algorithms Chapter 13
59
Graphics & Visualization: Principles & Algorithms Chapter 13
This can only be partially alleviated by increasing resolution of the shadow buffer at the expense, of course, of rendering time and texture memory
Depth map (shadow map) granularity and numerical accuracy affect the shadow tests poor and misplaced shadow boundaries
Problem is most noticeable in self-shadowing cases
60
Graphics & Visualization: Principles & Algorithms Chapter 13
Introduces a safety margin in the self-shadowing depth comparisons
Moves the shadows a little further away from the light source appear detached from the shadow casters in some cases
Light source at the center of the cube (allows a straightforward implementation in hardware-accelerated APIs)
Reasonable depth distortion due to the 90-degree aperture of resulting frusta
61
Graphics & Visualization: Principles & Algorithms Chapter 13
Near & far values on surface boundaries would be erroneously averaged filtered depth values along the edges of objects would not correspond to the depicted geometry
distributed in the neighborhood of the final eye-space fragment to be rendered
62
Graphics & Visualization: Principles & Algorithms Chapter 13
63
Graphics & Visualization: Principles & Algorithms Chapter 13
64
Graphics & Visualization: Principles & Algorithms Chapter 13
Although desirable in some cases, the jagged pattern samples can still be noticeable, especially in a real-time implementation PCF is sub-optimal
Try to sample the object space over a grid that more closely matches the eye- space sample distribution of the world
65
Graphics & Visualization: Principles & Algorithms Chapter 13
Create multiple shadow maps of same resolution but increasing area coverage and choose the appropriate shadow map according to the distance from the viewpoint
This is commonly used for outdoor lighting (distant light). Difficult to model arbitrary viewpoint-light position configurations.
66
Graphics & Visualization: Principles & Algorithms Chapter 13
A modification of the shadow-map algorithm adapts the map resolution to the current view angle:
Instead of transforming & projecting the geometry expressed in the world coordinate system, the method creates the shadow maps after transforming the world and the light source into the camera-view clip-space (CSS)
Programmer must handle many special cases of relative camera/light position
Captured shadow map “sees” the scene after the camera perspective projection perspective aliasing is significantly reduced
Need to update the shadow volume when the view changes, regardless of whether the world has changed or not
67
Graphics & Visualization: Principles & Algorithms Chapter 13 68
Regular SM PSM
Graphics & Visualization: Principles & Algorithms Chapter 13
69