Stencil Test - this is what I'm doing, where am I screwing up?

The fragment below shows how I’ve setup my stencil test. What I’m trying to do is draw a load of polygons with holes in (using alpha test) and then to draw a further bunch of polygons which may or may not show through the holes. I have had problems with z buffer accurracy when two polygons are really close to each other. Polygon offset hasn’t worked too well because I’m dealing with cylinders (see www.thermoteknix.com for a screenshot!). Thanks for any advice.

glEnable ( GL_STENCIL_TEST );
glStencilFunc ( GL_ALWAYS, 1, 1 );
glStencilOp ( GL_KEEP, GL_ZERO,GL_REPLACE );

DrawPolygonsWithHolesIn ();

glStencilFunc ( GL_EQUAL, 1, 0 );
glStencilMask ( GL_FALSE );

DrawPolygonsUnderneath ();

glDisable ( GL_STENCIL_TEST );

If your problem is depth buffer precision, then you should alter your near and far clip planes, since they are the source of the problem. Don’t use a larger view frustum that you absolutely need.

Thanks bob. I’ve tried that solution already. The problem here is that the depth-buffer works on a per-pixel basis, with a limited accuracy whatever my near\far planes are and the alpha-test works on a per-fragment basis (assuming a fragment is larger than a pixel). At places my two cylinders touch geometry-wise but the alpha in the texture might not cover exactly those areas that do touch and so I’m getting z fighting in these places. Someone said stencil might help some time ago and I’ve only just got around to trying it out.