PDA

View Full Version : Copying Depth Buffer To Texture



asafc
11-23-2009, 03:35 AM
Hi!
I want to do DoF, and I need to send to the shader a texture with the depth valuse.
How can I copy the z-buffer to texture?

Dark Photon
11-23-2009, 05:00 AM
Hi!
I want to do FoD, and I need to send to the shader a texture with the depth valuse.
How can I copy the z-buffer to texture?
Create an FBO and a depth texture, set depth texture on FBO, bind FBO, render to it, unbind FBO, bind system FB, rebind texture to shader sampler, render with it.

And FoD? Assume you mean DoF?

skynet
11-23-2009, 08:13 AM
If you for some reason don't want to use render-to-depth-texture, you can also use
glCopyTex(Sub)Image2D() do copy the depthbuffer contents over into a depth-texture. The internal format of the depth texture will automatically cause the depthbuffer to be copied over (instead of the colorbuffer contents).

asafc
11-23-2009, 09:17 AM
You right, I don't want to render to (depth) texture. Because, I can't render my geometries twice every time.
When you say glTexImage2D() with depth format, do you mean like that:
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

skynet
11-23-2009, 09:38 AM
Yes, you create the texture

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

and later on you copy the depthbuffer contents into it:

glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, 0,0, m_width, m_height);


When we suggested to use render-to-depth-texture, we didn't mean to render the scene twice; instead, you may also use a depthtexture as depthbuffer-replacement right from the beginning.

asafc
11-25-2009, 09:44 AM
It works!
Thank you all