Game Development Reference
In-Depth Information
8.2.3.1 Part I
We begin by enabling the stencil buffer and setting the related render
states:
void RenderMirror()
{
Device->SetRenderState(D3DRS_STENCILENABLE, true);
Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
Device->SetRenderState(D3DRS_STENCILREF, 0x1);
Device->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
Device->SetRenderState(D3DRS_STENCILWRITEMASK,0xffffffff);
Device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
This is fairly straightforward. We set the stencil comparison operation
to D3DCMP_ALWAYS , which specifies that the stencil test will always
pass.
If the depth test fails, we specify D3DSTENCILOP_KEEP , which
indicates to not update the stencil buffer entry. That is, we keep its cur-
rent value. We do this because if the depth test fails, it means the pixel
is obscured. Therefore, we do not want to render part of the reflection
to a pixel that is obscured.
We also specify D3DSTENCILOP_KEEP if the stencil test fails. But
this isn't really necessary here, since the test never fails because we
specified D3DCMP_ALWAYS . However, we change the comparison oper-
ation in just a bit, so setting the stencil fail render state is required
eventually; we just do it now.
If the depth and stencil tests pass, we specify D3DSTENCILOP_
REPLACE , which replaces the stencil buffer entry with the stencil refer-
ence value— 0x1 .
8.2.3.2 Part II
This next block of code renders the mirror, but only to the stencil
buffer. We can stop writes to the depth buffer by setting the D3DRS_
ZWRITEENABLE and specifying false. We can prevent updating the back
buffer with blending and setting the source blend factor to D3DBLEND_
ZERO and the destination blend factor to D3DBLEND_ONE . Plugging
these blend factors into the blending equation, we show that the back
buffer is left unchanged:
FinalPixel
sourcePixel
0, 0, 0, 0
DestPixel
1, 1, 1, 1
0, 0, 0, 0
DestPixel
DestPixel
Search WWH ::




Custom Search