Game Development Reference
In-Depth Information
Figure 8.8: Notice the “black” areas of the
shadow in (a). These correspond to areas
where parts of the flattened teapot over-
lap, causing a “double blend.” Image (b)
shows the shadow rendered correctly
without double blending.
We can solve this problem using the stencil buffer. We set the stencil
test to only accept pixels the first time they are rendered. That is, as
we render the shadow's pixels to the back buffer, we mark the corre-
sponding stencil buffer entries. Then, if we attempt to write a pixel to
an area that has already been rendered to (marked in the stencil buffer),
the stencil test will fail. In this way, we prevent writing overlapping pix-
els and therefore avoid double blending artifacts.
8.3.5 Code and Explanation
The following code explanation is taken from the Shadow sample in the
companion files. The code relevant to this sample lies in the
RenderShadow function. Note that we assume the stencil buffer has
already been cleared to zero.
We begin by setting the stencil render states. We set the stencil
comparison function to D3DCMP_EQUAL and the D3DRS_STENCILREF
render state to 0x0 , thereby specifying to render the shadow to the
back buffer if the corresponding entry in the stencil buffer equals 0x0 .
Since the stencil buffer is cleared to zero ( 0x0 ), this will always be
true the first time we write a particular pixel of the shadow; but
because we set D3DRS_STENCILPASS to D3DSTENCILOP_INCR , the
test will fail if we try to write to a pixel that we have already written to.
The pixel's stencil entry will have been incremented to 0x1 the first
time it was written to, and thus the stencil test will fail if we try to
Search WWH ::




Custom Search