Stencil Buffer : glStencilOp problem ?

I am currently working with the Stencil buffer and I encountered a problem. Maybe someone could give me a hint. Here’s the problem :

I have a binary image that I use as a mask for the stencil buffer ( written there with glDrawPixels ). Then I draw some object with these settings :

glStencilFunc (GL_EQUAL, 1, 1);
glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);

Works fine. The object gets drawn only where my stencil buffer values are 1.

However, now I would like that after my object is drawn, the pixels where it got drawn to be switched to 0 in the stencil buffer. From what I read about glStencilOp, I thought the following would work :

glStencilFunc (GL_EQUAL, 1, 1);
glStencilOp (GL_KEEP, GL_KEEP, GL_ZERO);

I thought that my stencil buffer values would be switched to 0 when the stencil test + depth test succeeds (which is when my object actually gets drawn), but all I can get is a very werid behavior. Like only some “lines” of my object gets drawn.

Anyone has an idea or a hint for me ?


As a precision, I just tried something different.

glStencilFunc (GL_EQUAL, 1, 1);
glStencilOp (GL_KEEP, GL_ZERO, GL_ZERO);

Although the results seem better as far as the shape is concerned, my object seems to get drawn mostly in black. There is “almost” no color on it.

I’m saying “almost” because on some refreshs, I get a few tiny colored lines. But rarely and not even over 2% of the shape. This all looks really buggy.

This seems to show that I had a problem with my depth buffer values, but what about the color ?

so no one has an idea ? :confused:

Try adding


Polygonsmoothing caused some very weird bug, when i did stencil-shadows.