Work with stencil buffer


#1

While testing the recording to the stencil buffer, I discovered that in the places where the masks overlap, their pixels are added together. In fact, I don’t know whether this is a mistake or not. But this is contrary to logic. Since I use the replace rule. Using this code to populate the stencil buffer.

cube.node().setAttrib(StencilAttrib.make(1, StencilAttrib.SCFAlways, StencilAttrib.SOReplace, StencilAttrib.SOReplace, StencilAttrib.SOReplace, 10, 0, 10))

sphere.node().setAttrib(StencilAttrib.make(1, StencilAttrib.SCFAlways, StencilAttrib.SOKeep, StencilAttrib.SOKeep, StencilAttrib.SOReplace, 20, 0, 20))

As a result, I get a pixel equal to 30 in the place where the sphere of the cube overlaps. I would like to clarify, is this the correct behavior?

python.zip(python.trace) (95.4 KB)


#2

You are passing in a write mask that is AND’ed with the value you are writing. This is the last argument, which corresponds to glStencilMask.

When you write the second one you are only replacing the bits you are masking, so you are writing (10 & ~20) | 20 which happens to be 30.

The last argument should usually be 0xff or ~0 if you prefer.