0

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".

Kiord
  • 79
  • 7
  • When your content mesh is depth-sorted, do you even need a depth-buffer when rendering it? You could render with depthtesting set to GL_GREATER, then only parts behind the window will get rendered. – BDL Jul 02 '19 at 12:36
  • Thanks for you comment. I actually meant I need to use standard GL_LESS depth-testing when rendering the content mesh. – Kiord Jul 02 '19 at 15:33
  • 1
    I would still go for STENCIL ... what is wrong with it exactly? its not clear from your description can you show us a screenshot with description what is wrong with it? – Spektre Jul 03 '19 at 06:49
  • I didn't figure out a solution involving stencil buffer. But I agree it would be easier/faster... In the example I mentioned, there is no depth testing when drawing the character behind the wall (it is a plain color). I would like to draw a mesh (with depth testing enabled) but only if it is behind a wall. In fact, this very close to this problem : https://stackoverflow.com/questions/33336092/in-opengl-how-can-i-depth-test-between-two-depth-buffers I hope it is clear(er) – Kiord Jul 03 '19 at 16:10
  • @Spektre I've implemented the pseudo solution with a texture and it works fine, I have another z fighting issue but only between triangle from the content mesh, it is something else. So, I could close this thread, but I findthe problem interesting. – Kiord Jul 03 '19 at 16:15

1 Answers1

1

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.

Kiord
  • 79
  • 7