Draw a object comparing two different values of stencil buffer

Viewed 61

First draw Geometry_A and write to stencil buffer with the following value.

glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);  // replace stencil buffer values to ref=1
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glStencilFunc(GL_NEVER, 1, 0xFF); // never pass stencil test
glStencilMask(0xFF);
Draw Geometry_A

Draw Geometry_B and write to the stencil buffer with following Value.

 glStencilFunc(GL_NEVER, 2, 0xFF); // never pass stencil test
 glStencilMask(0xFF);
 Draw Geometry_B

Draw Geomtery_C Comparing the stencil value of Geometry_A

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_EQUAL, 1, 0xFF);

Draw Geomtery_D Comparing the stencil value of Geometry_B

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_EQUAL, 2, 0xFF);

Can we draw a geometry comparing both the values of stencil buffer from Geometry_A and Geometry_B

something like.

glStencilFunc(GL_EQUAL, 1 & 2, 0xFF);
1 Answers

Can we draw a geometry comparing both the values of stencil buffer from Geometry_A and Geometry_B

Yes of course, just set a bit in the stencil buffer, instead of changing all bits.

Set the bit mask 0x01 for geometry Geometry_A

glStencilFunc(GL_NEVER, 1, 0xFF); // never pass stencil test
glStencilMask(0x01);

Set the bit mask 0x02 for geometry Geometry_B

glStencilFunc(GL_NEVER, 2, 0xFF); // never pass stencil test
glStencilMask(0x02);

If you want to find the sections where Geometry_A and Geometry_B have been drawn, then you have to find test if the fist 2 bits in the stencil buffer are set.

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_EQUAL, 3, 0x03);

Note, with this approach the bits 3 to 8 are not affected at all.

Related