I have a continuous height-mapped mesh to represent landscape. I use the stencil test to create holes in the mesh. I draw holes to the stencil buffer and then use it to discard mesh fragments. Everything works fine except that my holes are visible through the mesh even if they are occluded by higher ground.

enter image description here

Is there any way to apply depth testing to a hole, keeping my mesh drawn in a single call to glDrawElements?

  • \$\begingroup\$ What does your picture illustrate? I can't see anything related to the question on it \$\endgroup\$ – Kromster Mar 28 '16 at 6:23
  • \$\begingroup\$ it illustrates that "my holes are visible through the mesh even if they are occluded by higher ground" \$\endgroup\$ – Yevheniy8 Mar 28 '16 at 7:14
  • \$\begingroup\$ I don't see no holes on the picture. \$\endgroup\$ – Kromster Mar 28 '16 at 7:23
  • \$\begingroup\$ the blue square is a hole \$\endgroup\$ – Yevheniy8 Mar 29 '16 at 20:15

The easiest way to do this is to render the holes as geometry first (a simple quad is ok), with color writes off and depth writes on. This allows terrain in front of the hole to draw over it and terrain behind the hole will fail the depth test. That leaves a [clear color] hole in the terrain that you can render through.

This is often done to clip the water plane out of the interior of a boat's hull.

  • \$\begingroup\$ Oh, I finally got it. The thing is I don't need stencil test at all! First I draw a model of a well, then write a quad to depth buffer slightly higher than ground, and then I draw the terrain \$\endgroup\$ – Yevheniy8 Mar 29 '16 at 20:41

I would advise you to consider using glStencilOp function.

In particular, you can use different operation if the stencil test passes but not the depth test.

See that reference page for additional details.

Hope it helps.

  • \$\begingroup\$ As far as I understand, it won't help. Because it tests against the value already stored in depth buffer. But in my case terrain mesh is not drawn at the moment of stencil writing, so depth buffer is empty and any fragment will pass. Am I right? \$\endgroup\$ – Yevheniy8 Jan 26 '16 at 16:58
  • \$\begingroup\$ In fact, the depth buffer will be filled when you'll write the holes data. Then when writing the terrain, there will be a comparison between the hole depth and the terrain depth. Using the function mentioned in my answer, you'll be able to react differently if there is a hole behind the terrain. \$\endgroup\$ – elenfoiro78 Jan 27 '16 at 14:16
  • \$\begingroup\$ The thing is I disable depth-buffer writing when I render holes. This is because I need to draw hole's interior model which is also visible on top of the terrain. What I really need is just prevent some terrain fragments from drawing to any buffer. \$\endgroup\$ – Yevheniy8 Jan 27 '16 at 16:00
  • \$\begingroup\$ How is the interior like, a quad ? Does it cover the entire hole ? \$\endgroup\$ – elenfoiro78 Jan 27 '16 at 17:14
  • \$\begingroup\$ just a water well model \$\endgroup\$ – Yevheniy8 Jan 27 '16 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.