Stencil buffer question

Hi,

got a question with the stencil buffer. The following code
doesn’t work. I want to draw some shapes into the stencil buffer.
Then I clear the color buffer and draw some other shapes into
the color buffer. At last I want to draw a rectangle over the whole
screen, but only where the stencil buffer is filled.

Thats my source:

// Now draw the shapes for stencil mask:
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
DrawStencilShapes();

// Now draw some shapes which are not stenciled:
glDisable(GL_STENCIL_TEST);
glClear(GL_COLOR_BUFFER_BIT);
DrawShapes();

// Now draw a gray polygon over the whole screen, but only
// where the stencil was written with 1 before:
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glShadeModel(GL_SMOOTH);
glDisable(GL_LIGHTING);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (-50, 50, -50, 50, -150, 150);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,drawSizeX,drawSizeY);

glTranslatef(0,0,-80);

glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

glBegin(GL_POLYGON);
glColor3f(0.7f, 0.7f, 0.7f);
glVertex2f(-50, 50);
glVertex2f( 50, 50);
glVertex2f( 50,-50);
glVertex2f(-50,-50);
glEnd();

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

glDisable(GL_STENCIL_TEST);

Any suggestions?

Thanks!

This may sound obvious but are you certain you have available any stencil buffer bits? Because otherwise, at the moment, I don’t see any obvious errors in the logic.

Ok, I think I may have found a problem, or maybe not. But I notice that when you draw into the stencil buffer, you have not masked writes to the color buffer (ok because you later clear the color buffer) or depth buffer (not ok), nor disabled depth testing in general (potentially not ok). Perhaps you do that within DrawStencilShapes(). If so, then I still have no clue at the moment.

[This message has been edited by DFrey (edited 01-09-2001).]

Thanks DFrey, I must be blind…
Code snippet from my PixelFormatDescriptor:

32, // 32-bit z-buffer.
0, // No stencil buffer.
0, // No auxiliary buffer.

Why do I comment my source???

whoa!! I thought I wont never get stencil buffer works !! I got wrong pfd. When using glut it worked but not in w32 api.

This is cool!!!
Thanks!!

Hehehe is that NeHe code ?? It seems…

I’m not kidding you, I had the same problem when done the Glut to Win step !