Stencil shadow volume
From Free net encyclopedia
A stencil shadow volume is a method of rendering in 3D computer graphics. Frank Crow introduced shadow volumes in 1977 as the geometry describing the 3D shape of the region occluded from a light source. Tim Heidmann later showed how to use the stencil buffer to render shadows with shadow volumes in real time. Heidmann's approach only worked when the virtual camera was itself not in shadow. Around 2000, several people discovered that Heidmann's method can be made to work for all camera positions by reversing a test based on the z-coordinate. His original method is now known as z-pass and the new method is called z-fail. Sim Dietrich's PowerPoint talk at a Creative Labs talk appears to be the first to mention this; however, Cass Everitt and Mark Kilgard's 2002 NVIDIA technical report is the first detailed analysis of the technique. John Carmack of id Software popularized the technique by using it in the Doom 3 video game, so it is often referred to as Carmack's Reverse.
Creative Labs has recently attempted to enforce a patent on z-fail testing.
Contents |
Variants
There are several ways of implementing stencil shadow volumes, all of which use the stencil buffer available in both OpenGL and DirectX. The variants differ in speed, robustness and the number of stencil-bits required.
All variants requires the construction of a shadow volume first, using the properties of silhouette edges.
Exclusive-Or variant
This is the fastest and simplest variant, requiring only a single additional pass. This method also only requires a single bit in the stencil buffer.
- Render scene with only ambient lighting
- Render entire shadow volume to the stencil buffer. Invert stencil value on z-pass
- Render scene with diffuse and specular lighting in areas of zero stencil value
This variant has the drawback of intersecting shadow volumes canceling out each other. It also fails if the camera is inside a shadow volume.
Depth-pass variant
This is a more advanced stencil counting approach capable of handling intersecting shadow volumes.
- Render scene with only ambient lighting
- Render front-facing faces to the stencil buffer. Increment stencil value on z-pass
- Render back-facing faces to the stencil buffer. Decrement stencil value on z-pass
- Render scene with diffuse and specular lighting in areas of zero stencil value
The near-plane is a plane in front of the camera; it is used to clip geometry that is in front of that plane. The Depth-pass variant of the stencil shadow volume algorithm fails if the near-rectangle (the portion of the near-plane visible by the camera) intersects a shadow volume. It is possible, however, to 'repair' such failure, with an additional rendering pass, see ZP+ in the External Links section, below.
Depth-fail variant (Carmack's Reverse)
This is an even more advanced stencil counting approach capable of handling both intersecting shadow volumes and cameras inside the shadow volumes.
- Render scene with only ambient lighting
- Render back-facing faces to the stencil buffer. Increment stencil value on z-fail
- Render front-facing faces to the stencil buffer. Decrement stencil value on z-fail
- Render scene with diffuse and specular lighting in areas of zero stencil value
This variant does not fail if the camera is inside a shadow volume, but requires closed shadow volumes which can be expensive to compute.
Common problems
- Non-convex objects may have self-intersecting silhouettes, leading to artifacts in the shadows if this is not accounted for (by removing all self-intersections before rendering).
- Stencil buffer uses saturation arithmetic, which may lead to problems if the initial stencil value is zero.
See also
- Shadow volume, which describes the general algorithm
- Silhouette edge, used to determine the shadow volume
- Stencil buffer, the buffer used
External links
- The Theory of Stencil Shadow Volumes by Hun Yen Kwoon on GameDev.net
- Tutorial - Stenciled Shadow Volumes in OpenGL by Josh Beam on 3ddrome
- ZP+: correct Z-pass stencil shadows