ofFbo access depth buffer / texture

hey guys,

in the ofFbo documenation it is said:

As an example of an advanced usage: Create an ofFbo. Attach the color buffer of the ofFbo to a texture. Attach the depth buffer of the ofFbo to a texture. Render the texture to screen with a pixel shader using ofShader.

and thats exactly what i want to do, but I dont know how. is there a way to create a ofTexture for the depthbuffer?
I’ve tried a lot to access, bind or draw the depthbuffer before i found this in the current develop brach:

bool	depthAsTexture;			// use a texture instead of a renderbuffer for depth (useful to draw it or use it in a shader later)  

sounds perfect! i tried to bind the texture and draw it in the way which works for the color texture, but still no luck.

ofTextureData  texData = fbo.getTextureReference().getTextureData();  
//glBindTexture( texData.textureTarget, (GLuint)texData.textureID );  
glBindTexture( texData.textureTarget, fbo.getDepthBuffer());  

in both cases - binding getDepthBuffer (=1) or texData.texureId (=2) - the quad is drawn with the color texture.

do you have any suggestions? whats the right way to do this?


I don’t have that branch set up for dev yet, but just out of curiosity you are making sure that your Settings object has useDepth and depthAsTexture true? If you look at the ofFbo::Settings it’s:

useDepth				= false;  
useStencil				= false;  
depthAsTexture			= false;  

so unless both useDepth and depthAsTexture are enabled, it’s not going to work. I wrote that documentation and I should really change it since it appears to be a bit trickier than I assumed it would be.

yes, i added the depthAsTexture parameter recently cause the default is a render buffer and you cannot draw them or access them from a shader.

i’ve just added a getDepthTexture method that returns an ofTexture in develop

thanks a lot arturo, now that works pretty easy. very very nice :slight_smile:

i think once it is integrated there is no reason to change the documentation. perhaps a warning would be useful if the settings have not been set correclty to use the new function.

i just started working with fbo, shaders and so on, but i thought after reading that article, that it must be possible to render the depth buffer into a texture and use it as you like. not sure about it and not even know the difference between the approaches. just wanted to mention it.

thanks for the fast replies, you made my day,

yes, now if you try to get the depth texture and the fbo hasn’t been allocated with the right parameters it shows an error in the console.

I’m looking at that article but i don’t see anything related to reading or rendering the depth renderbuffer to a texture, but perhaps i’m overlooking something, what makes you think that it’s possible?

I’m wrong, sorry, I think I just wanted to see it this way. but they are only attaching a color texture to the fbo:

Having created our texture the next job is to attach it to the FBO so we can render to it:


so you always can only use a render buffer or a texture? No matter if for depth or color? Is attaching a texture inefficient in any way?

i think a renderbuffer is faster than a texture but if you want to read it back there’s no other option