Search code examples
c++openglglslshaderstencil-buffer

Rendering a mesh only if behind another mesh


I'm trying to render a mesh ("content mesh") (with depth testing) only when it is behind another mesh ("window mesh").

The first solution would be to use a stencil buffer to only write where the "window mesh" has been written.

My issue is that the "content mesh" can be in front of the "window mesh", and I want to discard the fragments in this case (only visible if behind).

I can simply save the depth coordinates of the "window mesh" in a texture, and discard fragments of the "content mesh" using that information.

However, this implies a texture look-up and the use of 16 bit floating point numbers for the custom deph-test.

Is there another way to do this ?

PS : How to render a mesh behind another mesh, like a mask? This thread almost got me the solution but in my case I don't want to render at all the "content mesh" when it is in front of the "window mesh".


Solution

  • I used a texture to store the depth, and used it to discard fragment that are closer than this depth. It works fine. I'm satisfied enough with that.