Pro OpenGL ES for iOS

(singke) #1

CHAPTER 7: Well-Rendered Miscellany (^227)
Shadow Mapping
At one time, one of the most popular forms of shadow casting was through the use of
shadow mapping frequently employed in games. Although it is a bit of a bother to set
up, not to mention describe, the theory is pretty simple...considering.
Shadow mapping requires two snapshots of the scene. One is from the light’s point of
view, and the other is from that of the camera’s. When rendered from the light, the
image will, by definition, see everything illuminated by itself. The color information is
ignored, but the depth information is preserved, so we end up with a map of the visible
fragments and their relative distances. Now take a shot from the camera’s viewpoint. By
comparing the two images, we can find out what bits the camera sees that the light
cannot. Those bits are in shadow.
In practice, of course, it is a little more complicated than that.
Shadow Volumes
Shadow volumes are used for determining what part of your scene is illuminated and
what is not by making very clever use of certain properties of the stencil buffer. What
makes this technique so powerful is that it permits shadows to be cast across arbitrary
geometric shapes as opposed to projection shadows (discussed later), which really
works only for the simplified case of the shadow being thrown against a flat surface.
When a scene is rendered using the shadow volume technique, the stencil buffer will be
left in a state in which any part of the resulting image that is shaded will have a
corresponding stencil pixel that is greater than zero, while any part that is illuminated will
have a zero. See Figure 7-9.
Figure 7-9. Shadow volumes showing the corresponding values in the stencil buffer: 0 for any parts that are
illuminated, >0 for regions in shadow

Free download pdf