PDA

View Full Version : FBO that writes in the colorbuffer



Godlike
07-19-2009, 11:32 PM
I was wandering if there is a way to create a FBO that writes in the window system color buffer but reads depths from a texture.


More detail:
I render the scene using a FBO. The result is a texture with the depth values and one more with scene's color. In the next phase I make a post processing pass and I write the result in the window system color buffer (the window system depth buffer is always empty). In addition to that I want to draw a few debug primitives in the color buffer and I was wandering if I can use an FBO that writes in the color buffer and reads depths from the depth texture.

I've tried the above but it fails:


glGenFramebuffersEXT( 1, &fbo_id );
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo_id );


// inform the we will write in the color buffer
glDrawBuffer( GL_BACK );
glReadBuffer( GL_BACK );

// attach the depth texture
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, r::ms::fai_depth.gl_id, 0 );

// test if success
if( glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT )
FATAL( "Cannot create debug FBO" );

// unbind
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );

Any idea?

dletozeun
07-20-2009, 02:04 AM
Using MRT (Multiple Render Target) and shaders you can render in a RGBA texture and a depth texture at the same time:

See:
http://www.gamedev.net/reference/articles/article2333.asp

Then you can pass these two textures to your postprocessing shader and sample them in a second pass.

DmitryM
07-20-2009, 08:22 AM
After you render the scene in the first time, you should have 2 textures in the result:
-depth texture
-color texture

If you want to use this depth texture later in post-processing, you just need to *not* have this texture attached to GL_DEPTH_ATTACHMENT_EXT of the active framebuffer (Alternatively, disabling GL_DEPTH_CHECK may help). Just bind it to a free texture unit and use like any other texture in your fragment shader.

And a little advice:
Be sure to have a proper GL_TEXTURE_COMPARE_MODE_ARB parameter when accessing a depth texture

dletozeun
07-20-2009, 11:29 AM
If you want to use this depth texture later in post-processing, you just need to *not* have this texture attached to GL_DEPTH_ATTACHMENT_EXT of the active framebuffer (Alternatively, disabling GL_DEPTH_CHECK may help). Just bind it to a free texture unit and use like any other texture in your fragment shader.


It is not necessary to detach the depth texture, simply unbind the fbo that uses this one. He said that the final pass is rendered in the window system framebuffer.

DmitryM
07-20-2009, 01:17 PM
Read carefully:
"...of the *active* framebuffer..."

If he renders to a system framebuffer and has a depth texture from the off-screen framebuffer then I don't see a problem.

Godlike
07-21-2009, 01:23 AM
I will explain my situation once more. After the post processing phase I have the system color buffer filled with the final raster image, the system depth buffer is empty and the scene's depth is stored in a depth texture.

Now, I want to make an additional pass which I call it "debug pass". In debug pass I draw a few primitives (for lights, cameras, collision stuff etc) on top of the already filled system color buffer. I want this primitives be be tested against scene's depth. The problem is that I want to write in the system color buffer but read the depths from a texture. Apparently this is a problem be cause I dont know the way (if there is) to create a new FBO that writes in the system color buffer.


I hope I made myself clearer this time and thanks for the answers

BionicBytes
07-21-2009, 04:47 AM
perhaps you are right...but why is it such a problem to write to the FBO colour attachment(s)? At the end of the frame, simply draw a full screen quad using the FBO's colour attachment.
Not quite what you wanted, but still a simple and easy solution.

DmitryM
07-21-2009, 06:03 AM
Yes, you need to draw to an off-screen color buffer in order to use off-screen depth texture.

def
07-24-2009, 05:36 AM
Or write your "debug pass" to the window framebuffer, using the scene depth texture in a shader to do a manual "depth test".

I think that is what DmitryM suggested in his first post anyway, and it seems the obvious solution here.