OpenGL: Clear the Stencil buffer, except for certain bits?

2.3k views Asked by At

I'm using the stencil buffer for two jobs. The first is to allow masking to happen, and the second is to write masks for objects that can be 'seen' through. In this particular case, the reserved bit is 0x80, the very last bit in the byte, with the rest left for regular masking.

The first purpose requires that the buffer be cleared after around 127 uses, or else past stencils will become "valid" again when testing, since the value must wrap back to 1. The second purpose requires the reserved bits in the buffer to stay alive through the entire frame.

Is there any way to clear the entire stencil buffer, while keeping the reserved bits set?

1

There are 1 answers

0
Reto Koradi On BEST ANSWER

Your theory in the comment is correct. glStencilMask() is applied to the values written by glClear() and glClearBuffer().

From section "17.4.3 Clearing the Buffers" in the OpenGL 4.5 spec (emphasis added):

When Clear is called, the only per-fragment operations that are applied (if enabled) are the pixel ownership test, the scissor test, sRGB conversion (see section17.3.9), and dithering. The masking operations described in section 17.4.2 are also applied.

Where section 17.4.2 is titled "Fine Control of Buffer Updates", and includes the documentation of glStencilMask(). For glStencilMaskSeparate(), which is a more general version of glStencilMask(), it even says explicitly:

Fragments generated by front-facing primitives use the front mask and fragments generated by back-facing primitives use the back mask (see section 17.3.5). The clear operation always uses the front stencil write mask when clearing the stencil buffer.

So to clear the bottom 7 bits of the stencil buffer, you can use:

glStencilMask(0x7f);
glClear(GL_STENCIL_BUFFER_BIT);