#13: Shadows & Ray Tracing CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.
Lecture 13: Shadows
Transcript of Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Outline
1 The need for shadows
2 Hard ShadowsShadow mapsShadow volumesHardware support: stencil bufferFake shadows
3 Soft Shadows
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
The need for shadows
Shadows come “for free” in raytracing and radiosity methods,but in projective methods theyrequire some extra effort.
However, these efforts arewell-spent: shadows addrealism and depth, as well asgive a better understanding ofthe images.
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Hard and soft shadows
Point light sources give hardshadows, whereas area lightsgive soft shadows.
For soft shadows, the regionthat receives no light at all iscalled the umbra, and theregion that receives partiallight is called the penumbra.
The penumbra gives additionalcues with respect to the size ofthe light source and thedistance from the occluder tothe receiver.
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow maps
A popular hardware supportedshadow rendering algorithmuses shadow maps.
The scene is first renderedfrom the light sourcepoint-of-view to fill a depthbuffer, stored with the light.
light source
view point
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow maps
Next, the scene is renderedfrom the camera point-of-view.The z-coordinate renderedpolyon for a pixel is convertedwith a transformation matrixto a z-coordinate in the lightsource coordinate system.
If the light source z-buffer hasa smaller z-value stored, thenthe part of the polygonprojected to the current pixellies in shadow.
light source
view point
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow maps
Advantages:
hardware-supported(z-buffer and matrix ops),so very fast.
Disadvantages:
Suited for spot lights, notreally for omni-directionallights.
Precision problems due touse of floating points anddiscrete sampling.
light source
view point
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow volumes
An alternative method forshadow computation usesshadow volumes.
For a light source and anoccluder, the 3D shadowvolume is determined by thesilhouette edges of theoccluder w.r.t the light source.
12
1
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow volumes
The faces of the shadowvolumes are either front facingor back facing w.r.t thecamera. This is easilydetermined with the normalsof the faces.
12
1
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow volumes
The idea: when a pixel isrendered, we follow the rayfrom the viewpoint to theobject for the pixel.
Whenever the ray crosses ashadow volume face, weincrement or decrement acounter, depending on whetherthe face is front-facing orback-facing.
12
1
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow volumes
Objects for which the counteris zero are lit; objects forwhich the counter is positiveare in shadow. 1
21
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Hardware support: stencil buffer
Obviously, we don’t want to doray tracing to determineshadows in real-time renderingmethods.This is where the stencil buffercomes in.
12
1
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Hardware support: stencil buffer
The stencil buffer is comparable to the z-buffer (i.e., there is aone-to-one correspondence of pixels in the frame buffer and entriesin the stencil buffer), but here every entry is a counter.
It supports
resetting, incrementing and decrementing the counters.
idem, but conditionally, depending on a test against thez-buffer.
conditional drawing in the frame buffer, i.e., only draw a pixelif the corresponding counter is zero/non-zero.
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Hardware support: stencil buffer
Computing shadows using thestencil buffer:
Draw the scene withambient lighting only(i.e., everything is drawnas if it is in shadow).
Compute the shadowvolumes
“draw” the shadowvolume faces in the stencilbuffer.
12
1
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Hardware support: stencil buffer
Computing shadows using thestencil buffer (cont’d):
Drawing a pixel in the stencilbuffer means:
Test the shadow face“pixel” agains thez-buffer. If its z value issmaller than that of thedrawn (real) object, then:
If it is a front-facingface, increment thecounter.If it is a back-facingface, decrement thecounter.
12
1
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Hardware support: stencil buffer
Computing shadows using thestencil buffer (cont’d):
Once all shadow volume facehave been drawn, the stencilbuffer contains zeros for pixelsthat are not in shadow.
Next, draw the entire sceneagain (only the real objects),this time including lightingcalculations, but tell thegraphics card to ignore pixelsthat have a non-zero stencilbuffer entry.
12
1
00
light source
+1 +1−1
−1
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow volumes: problems
Problems with the shadowvolume approach:
The view point may lie inone or more shadowvolumes.
The near clipping planemay slice open shadowvolumes.
light source
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow volumes: problems
The described problems can be solved by capping the shadowvolumes: the near clipping plane (partially) becomes one of thefaces of each volume. Drawback: precision problems close to nearclipping plane.
The described algorithm uses a depth-pass approach: only shadowvolume faces in front of objects are drawn in the stencil buffer.
Alternative: depth-fail approach: only draw/count shadow volumefaces behind object. Problems with near clipping plane are tradedfor problems with far clipping plane.
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Shadow volumes: depth-pass vs. depth-fail
Depth-pass approach:
Depth-fail approach:
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Fake shadows
A fast and easy (but not extremely realistic) way of creatingshadows: fake shadows. Only few (important) occluders areselected, and shadows are projected onto supporting planes of onlyfew (important) surfaces (e.g.: table has shadows, but chairsaround it haven’t).
Draw the scene, including lighting.
Project occluders onto designated planes. Improvement: don’trender projections black, but blend with original colors.
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Fake shadows
Problems with fake shadows:
projected shadows have same depth as shadow receivers.
Shadows may stick out beyond shadow receivers (since weproject on planes instead of objects).
Multiple occluders may give rise to double blending.
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Shadow mapsShadow volumesHardware support: stencil bufferFake shadows
Fake shadows
Solution to the problems with fake shadows: use stencil buffer.
Reset stencil buffer counters.
Draw scene. When shadow receivers are drawn, incrementcorresponding stencil buffer counters.
Project shadow polygons, but only draw/blend for pixel thathave a non-zero stencil value. Reset stencil entry afterdrawing, to avoid double blending.
Graphics, 2nd period 2007/08 Lecture 13: Shadows
The need for shadowsHard ShadowsSoft Shadows
Soft Shadows
Soft shadows are expensive. Usual approach: approximate bysampling area lights, treating every sample as a point light source.Compute individual shadows, and average.
Drawback: many samples are needed to get smooth penumbras,and this hurts performance.
Graphics, 2nd period 2007/08 Lecture 13: Shadows