For a project of mine in OpenGL, I want to render a series of tunnels through a terrain mesh. The immediate problem is that the terrain still renders inside of the tunnel. At the moment I solve this through using the stencil buffer, preventing the terrain from rendering where the tunnel has been rendered; the tunnel mesh is double-sided, each side incrementing the stencil buffer by one. Only odd values will prevent the terrain mesh from appearing.
The new problem becomes that this method doesn't care whether the terrain is inside or in front of the tunnel, making tunnel entrances appear in front of any terrain. A solution may lie in rendering "end caps" to tunnels into the depth buffer, but I perform important rendering after the tunnels are drawn and cannot have the depth buffer permanently modified like this.
Modifying the terrain mesh to actually have holes is also infeasible and would hurt the dynamic nature of the terrain/tunnel system I want. Some terrain surfaces are dynamic water surface meshes and would need to be recomputed every frame.