# Honouring Depth Test with Stencil Test

I'm using OpenGL's stencil test to carve out a semicircular hole in a transition frustum with a cylinder oriented perpendicularly. However as one can see below, while this objective is achieved, the cylinder is still visible through the frustum which shouldn't be the case.

I'm using the following code fragment that I found at https://en.wikibooks.org/wiki/OpenGL_Programming/Mini-Portal

glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_NEVER, 1, 0xFF);
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);  // draw 1s on test fail (always)

// draw stencil pattern
DRAW_CYLINDER ();

// draw where stencil's value is 0
glStencilFunc(GL_EQUAL, 0, 0xFF);
/* (nothing to draw) */
// draw only where stencil's value is 1
glStencilFunc(GL_EQUAL, 1, 0xFF);

DRAW_FRUSTUM();

glDisable(GL_STENCIL_TEST);


Moreover I've found that the results are insensitive to the parameters of the glStencilOp and DepthMask commands.

I seem to vaguely remember from several years ag that a multi-pass rendering may be required. If so, could someone point me to how to accomplish this with the proper set of command parameters?

This is an example of what I'm expecting - The cylinder should cut a hole through the frustum. I've rendered it here by trimming the black cylinder within the frustum so that it just just out past the frustum surface.

• Dan, I've added an image describing my expectation as an "answer". – Sharat V Chandrasekhar Jun 29 '17 at 16:28
• You probably want to search for CSG (Constructive Solid Geometry) and Stencils. For example, this usenix.org/legacy/events/usenix05/tech/freenix/full_papers/… might allow you to achieve the desired results. Similarly, perhaps look at opencsg.org ? Actually, I might add this as a possible answer if no other answers appear – Simon F Jun 29 '17 at 16:43
• Thanks! In fact, I was reading that paper last night, but was hoping that OpenGL would have provided a way of accomplishing this with just basic stencil functions. It looks like quite an involved task! – Sharat V Chandrasekhar Jun 29 '17 at 16:50
• Simon, one of the issues that I'd have with using the EXCELLENT CSG resources is that I'm accessing OpenGL via Visual Basic modules linked to an Excel spreadsheet from which I drive my application. I do not know whether or not the project files for MSVC6 will include extensions for VBA. – Sharat V Chandrasekhar Jun 29 '17 at 17:49
• I´d treat the cylinder as a "light source", putting its geometry as a shader uniform and test fragment position against the cylinder, clipping fragments inside. Not sure if it is feasible for you or even interesting but in case you or others are stuck I´m just saying there is a way :-) – Andreas Sep 13 '17 at 18:08