Game Development Reference
In-Depth Information
(back buffer) and depth buffer. Argument six is used to specify the
value to clear the stencil buffer to; in this example we clear it to 0.
Note: Using the stencil buffer can be considered a “free” operation
in modern hardware if you are already using depth buffering, accord-
ing to the NVIDIA presentation Creating Reflections and Shadows Using
Stencil Buffers by Mark J. Kilgard.
8.1.1 Requesting a Stencil Buffer
A stencil buffer can be created at the time that we create the depth
buffer. When specifying the format of the depth buffer, we can specify
the format of the stencil buffer at the same time. In actuality, the stencil
buffer and depth buffer share the same off-screen surface buffer, but a
segment of memory in each pixel is designated to each particular buffer.
For instance, consider the following three depth/stencil formats:
D3DFMT_D24S8 —This format says to create a 32-bit depth/stencil
buffer and designate 24 bits per pixel to the depth buffer and 8 bits
per pixel to the stencil buffer.
D3DFMT_D24X4S4 —This format says to create a 32-bit
depth/stencil buffer and designate 24 bits per pixel to the depth
buffer and 4 bits per pixel to the stencil buffer. Four of the bits will
not be used.
D3DFMT_D15S1 —This format says to create a 16-bit depth/stencil
buffer and designate 15 bits per pixel to the depth buffer and 1 bit
per pixel to the stencil buffer.
Note that there are formats that do not allocate any bits to the stencil
buffer. For example, the D3DFMT_D32 format says to create a 32-bit
depth buffer only.
Also, the support for stenciling varies among the various graphics
cards. Some cards may not support an 8-bit stencil buffer, for example.
8.1.2 The Stencil Test
As previously stated, we can use the stencil buffer to block rendering to
certain areas of the back buffer. The decision to block a particular pixel
from being written is decided by the stencil test , which is given by the
following expression:
(ref & mask) ComparisonOperation (value & mask)
The stencil test is performed for every pixel, assuming stenciling is
enabled, and takes two operands:
Search WWH ::




Custom Search