Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL, how to use depthbuffer from framebuffer as usual depth buffer

I have frame buffer, with depth component and 4 color attachments with 4 textures

I draw some stuff into it and unbind the buffer after, using 4 textures for fragment shader (deferred lighting). Later i want to draw some more stuff on the screen, using the depth buffer from my framebuffer, is it possible?

I tried binding the framebuffer again and specifying glDrawBuffer(GL_FRONT), but it does not work.

like image 348
ShPavel Avatar asked Mar 28 '12 19:03

ShPavel


People also ask

How does OpenGL depth test work?

When depth testing is enabled, OpenGL tests the depth value of a fragment against the content of the depth buffer. OpenGL performs a depth test and if this test passes, the fragment is rendered and the depth buffer is updated with the new depth value. If the depth test fails, the fragment is discarded.

How does the depth buffer work?

A depth buffer, also known as a z-buffer, is a type of data buffer used in computer graphics to represent depth information of objects in 3D space from a particular perspective. Depth buffers are an aid to rendering a scene to ensure that the correct polygons properly occlude other polygons.

What is OpenGL framebuffer?

From OpenGL Wiki. A Framebuffer is a collection of buffers that can be used as the destination for rendering. OpenGL has two kinds of framebuffers: the Default Framebuffer, which is provided by the OpenGL Context; and user-created framebuffers called Framebuffer Objects (FBOs).

What is the different between Renderbuffer and framebuffer?

Without any attachment, a Framebuffer object has very low footprint. Now each buffer attached to a Framebuffer can be a Renderbuffer or a texture. The Renderbuffer is an actual buffer (an array of bytes, or integers, or pixels).


2 Answers

Like Nicol already said, you cannot use an FBOs depth buffer as the default framebuffer's depth buffer directly.

But you can copy the FBO's depth buffer over to the default framebuffer using the EXT_framebuffer_blit extension (which should be core since GL 3):

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

If this extension is not supported (which I doubt when you already have FBOs), you can use a depth texture for the FBO's depth attachment and render this to the default framebuffer using a textured quad and a simple pass through fragment shader that writes into gl_FragDepth. Though this might be slower than just blitting it over.

like image 140
Christian Rau Avatar answered Jan 02 '23 02:01

Christian Rau


You cannot attach images (color or depth) to the default framebuffer. Similarly, you can't take images from the default framebuffer and attach them to an FBO.

like image 37
Nicol Bolas Avatar answered Jan 02 '23 01:01

Nicol Bolas