Reputation: 739
OpenGL stencil testing does not work as expected in old OpenGL versions. So I use the following test rendering code to find out what is wrong:
int imgWidth = 256;
int imgHeight = 256;
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
// Render invisible triangle.
glColorMask(false, false,false,false);
glStencilFunc(GL_EQUAL, 0, 0x1);
glStencilMask(1);
glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);
glColor4ub(0,0,0,0);
glBegin(GL_TRIANGLES);
glVertex2d(0,0);
glVertex2d(128,128);
glVertex2d(128,0);
glEnd();
// Enable color rendering
glColorMask(true,true,true,true);
// Render renctangle with a triangle hole in it.
glStencilMask(0xff);
glStencilFunc(GL_EQUAL, 0x0, 0xff);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
glColor4ub(255,0,0,0);
glBegin(GL_QUADS);
glVertex2d(0,0);
glVertex2d(imgWidth,0);
glVertex2d(imgWidth,imgHeight);
glVertex2d(0,imgHeight);
glEnd();
The code above works as expected in my OpenGL 3.3 machine. But failed at OpenGL 1.4 machine , there is no hole in the renctangle. I tried to modify the stencil related parameters only to find that OpenGL seems to ignore the stencil test compeletely. Cannot figure out why.
Upvotes: 0
Views: 723
Reputation: 739
As @genpfault said glGetIntergerv(GL_STENCIL_BITS, bits) get 0 stencil bits. That explains what happened.
Ways to solve the problem: Under Windows change the cStencilBits of PIXELFORMATDESCRIPTOR to the desired number of bits (e.g. 8) when creating the rendering context.
However, that doesn't explain why stencil works fine under OpenGL verion 3.3. Higher version of OpenGL may force the stencil bits non-zero I guess. Can someone shed some light on this?
Upvotes: 1