Using the stencil buffer to cap solids


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:

glStencilFunc(GL_ALWAYS, 1, 1);

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);

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:


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:


Radeon SDK:


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 -