QuentinR
QuentinR

Reputation: 115

Use default framebuffer depth buffer in a fbo

I want to achieve a post process outline effect on some actors in my game and I want the outline be occluded by other actors closer to the camera. To do so I planed to render the elements that should be outline in an FBO and an outline shader. To discard the pixel that are occluded I want to use the depth buffer of the default framebuffer.

I read and searched but I didn't find how to properly use the default framebuffer depth buffer in another framebuffer or how to copy or anyway to use the default depth buffer informations on an fbo.

How can I achieve it?

Upvotes: 0

Views: 1951

Answers (2)

derhass
derhass

Reputation: 45372

I read and searched but I didn't find how to properly use the default framebuffer depth buffer in another framebuffer

Unfortunately, there is no way in OpenGL to do that.

or how to copy or anyway to use the default depth buffer informations on an fbo.

That is quite easy. Just create an FBO with a appropriate depth attachment (no matter if texture or renderbuffer), and blit the default depth buffer to it:

glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, your_target_fbo);
glBlitFramebuffer(0,0,width,height,0,0,width,height,GL_DEPTH_BUFFER_BIT, GL_NEAREST);

However, since you do some post-processing anyway, it might be a good option to render the intial scene into an FBO and re-use the same depth buffer in different FBOs.

Upvotes: 2

BDL
BDL

Reputation: 22170

Unfortunately, it is not possible to attach the default depth-buffer to a FBO or to read it from a shader.

You can copy the default framebuffer into a renderbuffer (or a framebuffer attached texturem, see glBlitFramebuffer), but this could be relatively slow.

Another option is to render the complete scene first to a FBO (color + depth), then use the outline shader to read from this FBO, combine the result with the outline calculation and write the final result to the default framebuffer.

Upvotes: 1

Related Questions