4

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.

enter image description here

leopardGeckos
  • 91
  • 2
  • 8
  • Maybe fill your depth buffer with your terrain before doing the stencilPass, you can also keep your depthBuffer in a separate texture and use it when you want – Paltoquet Apr 02 '19 at 06:02
  • see [I have an OpenGL Tessellated Sphere and I want to cut a cylindrical hole in it](https://stackoverflow.com/a/39466130/2521214) may be it help ... I do not see any problems like yours in mine output ... but I render slightly different stuf but still I have more discs like that on top of each so its similar ... – Spektre Apr 02 '19 at 08:14
  • Can't you use some "masking" tricks ? I mean, you render the caps in a separate render target, without writing the z buffer, and you use that texture as a mask in your mesh fragment shader. If there is data, you discard the fragment, otherwise you draw it. Something like that ? (just a guess, I don't know if it can apply to your case) – cmourglia Apr 02 '19 at 08:56

0 Answers0