Game Development Reference
In-Depth Information
effectively is best done by studying existing applications. Once you
understand a few applications of the stencil buffer, you will have a
better idea of how it can be applied for your own specific needs. For this
reason, this chapter puts a special emphasis on the study of two specific
applications using stencils (in particular, implementing mirrors and pla-
nar shadows).
Objectives
To gain an understanding of how the stencil buffer works, how to
create a stencil buffer, and how we can control the stencil buffer
To learn how to implement mirrors and using the stencil buffer to
prevent reflections from being drawn to non-mirror surfaces
To discover how to render shadows and prevent “double blending”
by using the stencil buffer
8.1 Using the Stencil Buffer
To use the stencil buffer, we must first request one when we initialize
Direct3D and then we must enable it. We describe requesting a stencil
buffer in section 8.1.1. To enable the stencil buffer, we must set the
D3DRS_STENCILENABLE render state and specify true . To disable
the stencil buffer, we specify false for the D3DRS_STENCILENABLE
render state. The following code snippet enables the stencil buffer and
then disables it:
Device->SetRenderState(D3DRS_STENCILENABLE,
true);
... // do stencil work
Device->SetRenderState(D3DRS_STENCILENABLE,
false);
Aside: Although not used in this topic, DirectX 9.0 has added a
two-sided stencil feature that speeds up shadow volume rendering by
reducing the number of rendering passes required to draw the shadow
volume. See the SDK documentation for details.
We can clear the stencil buffer to a default value using the
IDirect3DDevice9::Clear method. Recall that this is the same
method used to clear the back buffer and depth buffer as well.
Device->Clear(0, 0,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
0xff000000, 1.0f, 0 );
Note that we have added D3DCLEAR_STENCIL to the third argument,
indicating that we want to clear the stencil buffer as well as the target
Search WWH ::




Custom Search