226 CHAPTER 7: Well-Rendered Miscellany^
Figure 7-8. Using stencils to create reflections
Coming of the Shadows
Shadow casting has always been a bit of a black art in OpenGL, and it still is to a certain
extent. However, with faster CPUs and GPUs, many tricks of the trade that were
previously more the subject of a grad student’s paper can finally step out of theory into
the warm glow of real-world deployment. Rigorous solutions to shadow casting are still
the domain of the non-real-time rendering that Hollywood employs, but basic shadows,
under limited conditions, are available to full-motion rendering. With thanks to the
various hardware manufacturers that have added both shadow and lighting support to
their GPUs, our 3D universes look richer than ever before because few elements in
computer graphics can add more realism than carefully managed shadows. (Ask any
lighting director on a Hollywood movie.) And don’t forget the per-pixel support via the
use of shaders in OpenGL ES 2, which can let a programmer delicately shade every
corner of every spooky castle in Blow Up Everything 3.
There are many ways to cast shadows, or at least shadow-looking things. Perhaps the
simplest is to have a prerendered shadow texture: a bitmap that looks like a shadow on
the ground, cast by your object, which is then moved around as it moves. It’s cheap,
fast, but extremely limited. On the other extreme is the full-blown render-everything-you-
can-ever-see software that eats GPUs by the handful for lunch. In between the two,
you’ll find shadow mapping, shadow volumes, and projection shadows.