Game Development Reference
In-Depth Information
// disable writes to the depth and back buffers
Device->SetRenderState(D3DRS_ZWRITEENABLE, false);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
// draw the mirror to the stencil buffer
Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
Device->SetFVF(Vertex::FVF);
Device->SetMaterial(&MirrorMtrl);
Device->SetTexture(0, MirrorTex);
D3DXMATRIX I;
D3DXMatrixIdentity(&I);
Device->SetTransform(D3DTS_WORLD, &I);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 18, 2);
// re-enable depth writes
Device->SetRenderState(D3DRS_ZWRITEENABLE, true);
8.2.3.3 Part III
At this point, the pixels in the stencil buffer that correspond to the visi-
ble pixels of the mirror have an entry on 0x1 , thus marking the area
where the mirror has been rendered. We now prepare to render the
reflected teapot. Recall that we only want to render the reflection into
pixels that correspond to the mirror. We can do this easily now that we
have marked those pixels in the stencil buffer.
We set the following render states:
Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
With a new comparison operation set, we get the following stencil test:
(ref & mask == (value & mask)
(0x1 & 0xffffffff) == (value & 0xffffffff)
(0x1)== (value & 0xffffffff)
This shows that the stencil test only succeeds if value = 0x1 . Since
value is only 0x1 in areas of the stencil buffer that correspond to the
mirror, the test only succeeds if we are rendering to those areas. Thus,
the reflected teapot is only drawn into the mirror and is not drawn into
other surfaces.
Note that we have changed the D3DRS_STENCILPASS render
state to D3DSTENCILOP_KEEP , which simply says to keep the value in
the stencil buffer if the test passed. Therefore, in this next rendering
pass, we do not change the values in the stencil buffer (all controls are
D3DSTENCILOP_KEEP ). We are only using the stencil buffer to mark
the pixels that correspond to the mirror.
Search WWH ::




Custom Search