Stencil buffer

From DmWiki

A stencil buffer is one of the several buffers that compose a 3D application's frame buffer. The stencil buffer is never directly visible, and serves no single purpose: it is like a scratch pad that applications can use for a variety of different purposes.

The key feature of the stencil buffer is that one can enable the stencil test, which causes pixels to be drawn or not drawn to the other parts of the frame buffer depending on the contents of the stencil buffer. In particular, users can set up the stencil test to perform a mathematical comparison (e.g. equality, greater-than, less-than, etc) between the stencil buffer and a user-supplied value. Pixels are not drawn if the stencil test fails (returns false).

Additionally, one can specify an operation to be performed on the stencil buffer when pixels are drawn. Three different operations can be specified:

  • The operation to be performed when the stencil test fails;
  • The operation to be performed when the stencil test passes, but the depth test fails;
  • The operation to be performed when both the stencil test and the depth test pass.

The available operations include increment, decrement, no change, replace with zero, and replace with a user-supplied value.

The myriad of different ways the stencil buffer can be configured make it an uncommonly powerful tool for creating different rendering effects. Two of the most common uses of the stencil buffer are for creating certain kinds of reflections, and in the shadow volumes algorithm.


DevMaster navigation