Home > Blockchain >  How do I switch to another depth buffer temporarily in open gl
How do I switch to another depth buffer temporarily in open gl

Time:11-22

We have a CAD app where 3D entities can be selected to be in 'overlay' mode. Overlay entities always appear in front of non overlay entities.

To achieve this, ideally I want to do the non overlay entities render first, to the default frame buffer depth buffer, then switch to another depth buffer, clear it, and render the overlay entities. Then swap back to the default depth buffer.

  • its important the default depth buffer is maintained with the info from the non overlay render. (boring third party library reasons)
  • its important that the overlay entities are drawn with a depth test on so they composite with other overlay entities correctly.
  • We need the full precision of the depth buffer in both cases so can't just mess with the near/fars to partition out half the same depth buffer to each.

Is it possible to swap out just the depth buffer part of the default frame buffer with another depth buffer FBO (and then swap back)

OR

What's the best way of 'backing up' the default depth buffer before doing the overlay render, and then restoring it after? FBO blits? Obviously we wouldn't want the data copying in and out of system memory.

Ideally I don't want to rely on hardware features that are super new. We need to support older hardware, within the last 5 years or so.

CodePudding user response:

Is it possible to swap out just the depth buffer part of the default frame buffer

No, you cannot change a buffer or format of the default frame buffer in any way. You need to create your own named Framebuffer Object and render into it. However you can copy framebuffers and parts of framebuffers with glBlitFramebuffer (target and destination can be a named framebuffer and/or the default framebuffer). e.g.:

glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo1);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo2);
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  • Related