1

I have an OpenGL problem to solve. I have an object/mesh A, an object/mesh B and a background texture C.

Initially the framebuffer is filled with background texture C. We draw both A & B in the framebuffer. We want to keep object A visible, and object B always invisible.

In the beginning, A is in front of B. During rotation, at a certain angle, B is in front of A based on the depth test result, but since B is always invisible, B's part should be filled with background C.

Does anyone know a simple approach to solve this issue?

Is stencil test a good approach? Basically set object B with a color, compare the color of B with background C, and show the background C when the test fail.

Does anyone have any sample code I can read?

Howy
  • 825
  • 2
  • 10
  • 20
  • just to clarify C is always the background - if B is rendered above A, you want C to be shown, and if A renders above B, you want to see A? – MuertoExcobito May 29 '15 at 17:18
  • Yes, you are right. C is always the background. – Howy May 29 '15 at 17:26
  • ... so it's like B is a hole you render on A? – Tommy May 29 '15 at 17:43
  • I think he just wants B+A to be the hole - if it's just B over C, then you should see B? – MuertoExcobito May 29 '15 at 17:49
  • Oh, that's not how I read it. My answer isn't appropriate for that. Will edit if needed. – Tommy May 29 '15 at 17:50
  • 1
    I don't understand the requirements, what happens if B does not overlap A? Do you want the background to be displayed instead of B only for the parts of B that are in front of A? – peppe May 29 '15 at 17:50
  • I want to make B always invisible, meaning B's part is always filled with Background C. But I want depth test to pass. If B is in front of A, don't show A, but show C. If A is in front of B, show A. – Howy May 29 '15 at 17:52
  • You want the depth test to pass _what_? As in, you want the depth values of A left in the depth buffer irrespective of the location of B? ... or you want the depth values from C wherever B was? So that you could actually see a hypothetical D, somewhere between A/B and C, through A where B was? While being allowed to draw D after drawing A/B? – Tommy May 29 '15 at 17:53
  • @Howy maybe you should edit your question so it's a little clearer exactly what you want. "We draw both A & B on top of it" - means to me that A and B were both being rendering into the color buffer. If B is always invisible, then Tommy's answer should work. – MuertoExcobito May 29 '15 at 17:56
  • Sorry, Tommy, Yeah. Your answer is correct. I tried and it works. Thanks MuertoExcobito, now I clarified the question. Please post your answer again. and I will vote it. – Howy May 29 '15 at 18:04

1 Answers1

2

The easiest solution is to:

  1. draw C;
  2. draw B with the colour mask preventing writes to the frame buffer (but don't touch the depth mask, so that writes are still made to the depth buffer);
  3. draw A, subject to the depth test.

The specific thing to use is glColorMask — if you supply GL_FALSE for each channel via that then subsequent geometry won't write any colour output. But assuming you haven't touched glDepthMask it'll still write depth output.

So, you've probably currently got the code:

drawBackground(C);
render(A);
render(B);

You'd just adapt that to:

drawBackground(C);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
render(B);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
render(A);
Tommy
  • 99,986
  • 12
  • 185
  • 204