Game Engine Architecture

(Ben Green) #1
475

A shadow volume is used to generate a shadow by making use of a special
full-screen buff er known as the stencil buff er. This buff er stores a single inte-
ger value corresponding to each pixel of the screen. Rendering can be masked
by the values in the stencil buff er—for example, we could confi gure the GPU
to only render fragments whose corresponding stencil values are non-zero. In
addition, the GPU can be confi gured so that rendered geometry updates the
values in the stencil buff er in various useful ways.
To render shadows, the scene is fi rst drawn to generate an unshadowed
image in the frame buff er, along with an accurate z-buff er. The stencil buff er
is cleared so that it contains zeros at every pixel. Each shadow volume is then
rendered from the point of view of the camera in such a way that front-facing
triangles increase the values in the stencil buff er by one, while back-facing
triangles decrease them by one. In areas of the screen where the shadow vol-
ume does not appear at all, of course the stencil buff er’s pixels will be left
containing zero. The stencil buff er will also contain zeros where both the front
and back faces of the shadow volume are visible, because the front face will
increase the stencil value but the back face will decrease it again. In areas
where the back face of the shadow volume has been occluded by “real” scene
geometry, the stencil value will be one. This tells us which pixels of the screen
are in shadow. So we can render shadows in a third pass, by simply darkening
those regions of the screen that contain a non-zero stencil buff er value.


Shadow Maps


The shadow mapping technique is eff ectively a per-fragment depth test per-
formed from the point of view of the light instead of from the point of view
of the camera. The scene is rendered in two steps: First, a shadow map texture


Figure 10.51. A shadow volume generated by extruding the silhouette edges of a shadow
casting object as seen from the point of view of the light source.


10.3. Advanced Lighting and Global Illumination

Free download pdf