1
\$\begingroup\$

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?

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

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\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
    Commented Mar 29, 2016 at 20:41
0
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\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
    Commented Jan 26, 2016 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\$ Commented Jan 27, 2016 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
    Commented Jan 27, 2016 at 16:00
  • \$\begingroup\$ How is the interior like, a quad ? Does it cover the entire hole ? \$\endgroup\$ Commented Jan 27, 2016 at 17:14
  • \$\begingroup\$ just a water well model \$\endgroup\$
    – Yevheniy8
    Commented Jan 27, 2016 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .