how to increment value in stencil buffer?

I wrote some thing like below:

glClearStencil(0x0);
glEnable(GL_STENCIL_TEST);
glClear (GL_STENCIL_BUFFER_BIT);

glStencilFunc(GL_ALWAYS, 0x4, 0x4);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

draw something…

glStencilFunc(GL_EQUAL, 0x4, 0x4);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);

draw something …

glStencilFunc(GL_EQUAL, 0x5, 0x5);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

draw something …

during the second stencil operation, if the z buffer test pass, the value is supposed to be incremented by 1 that is 4+1=5, so that the third operation can function, but I found the value is 0x6 using debug, it seems the increment operation just shift the value one bit right so it is 6. can someone tell me how to increment it to be 5? thanks.

Hi,

I think the problem is that you are for some reason specifying masks for your stencil operations. Set all masks to 0, and it should work.

-Ilkka

Hi, thanks for your information.

I just found out that the reason why the stencil buffer is incremented by 2, it is because the object being rendered has two faces, front and back, so there are 2 Zbuffer passes.