stencil buffer?

Started by
3 comments, last by repeat4 17 years, 6 months ago
stencil buffer is used in many specific techniques,but I can't understand it at all. can anybody explain stencil buffer for me?
Advertisement
The stencil buffer is made of a few bits from the z buffer (usually 8 bits). Altrough you can't access the stencil buffer directly or output a stencil value in the pixel shader, you can still use some render states to change its behavior (D3DRS_STENCIL*). There are mainly two things you can do with the stencil:

1. Write to it. Using the render states, you can enable Draw*Primitive() calls to write to the stencil buffer. This usually allows you to mark certain regions of the screen for later operations. It allows you to modify stencil in three different situations (when the pixel fails the stencil test [see D3DRS_STENCILZFAIL], when the pixel fails the z test [see D3DRS_STENCILFAIL] and when the pixel passes the z test [see D3DRS_STENCILPASS]). For all of these situations, you can specify that you want to keep the stencil value [D3DSTENCILOP_KEEP], set it to zero [D3DSTENCILOP_ZERO], replace with the current reference value [D3DSTENCILOP_REPLACE using D3DRS_STENCILREF to set the reference value], increment or decrement the value that is already there [look at the D3DSTENCILOP type for more details about the stencil operations]. You can select which bits to write to using D3DRS_STENCILWRITEMASK. For all of these, you need to enable D3DRS_STENCILENABLE.

2. Use it to discard/allow pixels to be written. There is a stencil test that can be enabled to reject pixels based on the value stored in the stencil buffer and the current reference value. Check out (The following render states D3DRS_STENCILENABLE, D3DRS_STENCILFUNC, D3DRS_STENCILREF & D3DRS_STENCILMASK).

I hope this will be a good starting point to understand the stencil buffer.

- Aldenar
Thank you,Aldenar!

what you said giving me a basic knowledge about the stencil buffer.
Thank you again!
Aldenar's given you a good reply, but for completeness - have you seen the official documentation? Seems fairly complete to me...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

jollyjeffers,Thank you for your link.

This topic is closed to new replies.

Advertisement