Using the stencil buffer to cap solids

Hello,

I am trying to cap solids that have been sliced by a clipping plane. I thought I knew just what to do, but my results said differently.

The first thing I do is set up a clipping plane with glClipPlane. Next, I prepare the stencil buffer with:

glClear(… | GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_INCR, GL_INCR, GL_INCR);

Next, I draw my scene normally. My expectations are that OpenGL will update the depth and color buffers, while at the same time incrementing the value in the stencil buffer whenever the color buffer is drawn to.

Next, I set up the stencil buffer so that drawing will only take place where the stencil buffer is 1:

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

Next, I draw a polygon that fills the entire viewport and is drawn on the near plane. I do so with depth testing disabled.

Finally, I disable stencil testing:

glDisable(GL_STENCIL_TEST);

Does anyone see the error of my ways?

Thanks for any help,

  • Moe -

I am not sure if you are trying to exacly the same thing as I have done previously, but the algorithm I have used for Stencil CSG is as follows:

  1. Draw Front faces without updating stencil.

  2. Disable color writes and draw back faces, incrementing stencil onlyu when the ztest passes.

  3. Draw a quad aligned with the stencil plane, but set the stencil test to reject pixels unless the stencil is set.

There is a sample that does exactly this in the Radeon SDK. It is the volume texture sample. Here are the links:

Sample: http://www.ati.com/na/pages/resource_cen…umeTexture.html

Radeon SDK: http://www.ati.com/na/pages/resource_centre/dev_rel/sdk/RadeonSDK/index.html

Ehart,

Thanks for the tip. Your information led to a fix in a rather indirect way. It turns out my problem was that I didn’t disable the clipping plane before drawing the viewport-filling polygon and it was being clipped.

I came to this conclusion when I saw the radeonvolumetexture sample call glDisable(GL_CLIP_PLANE0) prior to drawing its capping polygon.

I also found an error in the logic with my buffer usage.

Anyway, thanks a lot for the information. This problem was driving me to an early grave!

  • Moe -