D3D Stencil Buffer
This demo come from book <Direct3D Graphic Programming introduction>. It show mirror effect and shadow effect, both of them using stencil buffer. In the mirror effect, the stencil buffer used to figure out the mirror area on the screen, so that these objects that should be displayed on this mirror will not beyond it’s boundary. For the shadow effect, actually we could it blob shadow. This technology working like that projecting a 3D object into a 2D plane. The stencil buffer used here to avoid overlap some pixels that already marked as shadow. The first time shadow shadered with stencil marked, and the second time will do stencil checking. If it already, this time will be skipped.
Although the stencil buffer is just a single value, or single channel mark buffer, it is very useful. For example, stencil volume shadow that use stencil buffer. Pre-calculate the light’s volume into the stencil buffer, we could skip a lot of pixel lighting calculation.
D3DX library provide some useful function: 1) D3DXMatrixShadow() build a projection matrix that project a object along a direction to a 2D plane. 2) D3DXMatrixReflect() build a matrix that reflect against a plane.
The full source code could be download from here.