Game Development Reference
In-Depth Information
3.
Render the primitives that make up the mirror into the stencil
buffer only . Set the stencil test to always succeed, and specify that
the stencil buffer entry should be replaced with 1 if the test passes.
Since we are only rendering the mirror, all the pixels in the stencil
buffer will be 0 except for the pixels that correspond to the mir-
ror—they will have a 1. Figure 8.4 shows the updated stencil buffer.
Essentially, we are marking the pixels of the mirror in the stencil
buffer.
Figure 8.4: Rendering
the mirror to the sten-
cil buffer, essentially
marking the pixels in
the stencil buffer that
correspond to the mir-
ror. The black on the
stencil buffer denotes
pixels set to 1.
4.
Now we render the reflected teapot to the back buffer and stencil
buffer. But recall that we only render to the back buffer if the sten-
cil test passes. This time we set the stencil test to only succeed if
the value in the stencil buffer is a 1. In this way, the teapot is only
rendered to areas that havea1intheir corresponding stencil buffer
entry. Since the areas in the stencil buffer that correspond to the
mirror are the only entries that have a 1, the reflected teapot is
only rendered into the mirror.
8.2.3 Code and Explanation
The code relevant to this sample lies in the RenderMirror function,
which first renders the mirror primitives to the stencil buffer and then
renders the reflected teapot only if it is being rendered into the mirror.
We walk through the RenderMirror function almost line by line and
explain what is occurring and, more importantly, why.
If you are using the steps outlined in section 8.2.2 to serve as an
overall guide to the code, note that we are starting at step 3 since steps
1 and 2 have nothing to do with the stencil buffer. Also be aware that
we are discussing the rendering of the mirror through this explanation.
Note that we divide this explanation up into parts for no other rea-
son than to offer a more modular discussion.
Search WWH ::




Custom Search