Stencil Buffer Artifacts

Hi everyone,

I’m drawing the intersection of a plane and a sphere using the stencil buffer.
However I’m getting some annoying artifacts. See the attached image.
These artifacts depend on the orientation and the position of the camera.
When viewed like on the left hand side of the image, the artifacts
trace out the silhouette of the sphere.

Here’s the algorithm I’m using:

  • clear stencil with value = 0
  • draw the plane
  • draw the sphere with depth.mask = false, color.mask = false, culling disabled and
    stencil.op.zpass = GL_INVERT for front and back faces
    (i use glStencilOpSeperate and draw the sphere once)
  • draw fullscreen rect where stencil value is != 0

Any ideas? I tried culling the triangles of the sphere that are viewed edge on.
But the artifacts only disappear when culling so many triangles that the roundish
shape of the red intersection is lost.