Jump to content
  • Advertisement
Sign in to follow this  
Dtag

Copy ZBuffer to Tex

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Iam trying to copy the frame buffers z buffer to a texture ( which is supposed to be used in a glsl fragment shader later on ). I created the texture using the GL_DEPTH_COMPONENT24 format, and Iam copying into it using glCopyTexSubImage2D. Now the first question is: How do I properly read this z value in the fragment shader? Ive tried just printing the rgb component of the vector that texture2D returns to the screen and I do get an output that looks somewhat like a zbuffer ( see here: http://www.cdtag.de/zprob.jpg ). The strange thing is: I would have expected some sort of gradient in the picture, but there is none ( I looked at the color values on a screenshot). So - any ideas what Iam doing wrong? Is there any way to directly retrieve the depth value as a float from the texture? I looked into the shadow2D lookup function ,which specifically seems to be for depth textures, but it enforces some sort of depth test against a reference value, and only returns whether the current texel passed the test or not. And thats not the functionality iam looking for. Thanks alot :)

Share this post


Link to post
Share on other sites
Advertisement
Well I thought it doesnt really make sense here because the copying "appears" to work, and its effectively only a single glsl line ;)

But nevertheless:
Initialisation code:

glGenTextures(1, &iTextureID);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, iTextureID);

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// Iam not too sure about these but changing them didnt cause any change in my results
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_ALWAYS );
glTexParameterf( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_ALPHA);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE );

glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT24,iWidth,iHeight,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL);





After every frame:

glBindTexture(...);
glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,1024,768);

Well thats about it for the GL part...

GLSL Frag Shader:


uniform sampler2D curDepthRender;

varying vec4 texCoord0;

void main ()
{
vec2 projected=texCoord0.xy/texCoord0.w;
vec4 d1=texture2D(curDepthRender,projected);
// i also tried rgb here etc... not really a difference
gl_FragColor=vec4(d1.aaa,1.0);
}

Share this post


Link to post
Share on other sites
do a readpixels of the zvalue under the cursor and move the cursor around youll see that all the values are close to one (thus appear white)
try setting your near far clip planes to say 50 + 200, now there should be a bigger range of numbers

Share this post


Link to post
Share on other sites
Hi,

i had the same problem some time ago, the problem is that most z values are around 0.9-1.0. Reversing the depth mapping back to something linear failed in my case due to precision issues (using a near plane of 2.0, far of 4096) resulted - after linerization - in a two color "gradient". I tried with a 16 and with 24 depth texture ( perhaps ARBfp TEX just returns 8bit?). But the situation remained the same. Depending on what you need the depth information for, put the distance from the camera into destination alpha instead of using the depth buffer. For shadowmapping depth textures are ok.

regards,
stephan

Share this post


Link to post
Share on other sites
I actually did not want to render the whole scene again - that would only slow things down. I wanted to use this for a number of post processing effects ( under water depth , depth of field ... ), so it would have been very handy to be able to just copy the depth over, as I do it with the color buffer.
It sounds really strange to me, that theres no way to properly access a depth texture inside glsl at full precision. Because what Iam rendering now is obviously not full precision.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dtag
I actually did not want to render the whole scene again - that would only slow things down. I wanted to use this for a number of post processing effects ( under water depth , depth of field ... ), so it would have been very handy to be able to just copy the depth over, as I do it with the color buffer.
It sounds really strange to me, that theres no way to properly access a depth texture inside glsl at full precision. Because what Iam rendering now is obviously not full precision.


I also tried to use a framebuffer depth copy for a DOF effect and as said above, precision is a huge issue. All papers covering DOF (using depth) i've read use a linear depth value written to destination alpha. That's just 8bit (if you dont use floating point buffers) but seems to be enough.

regards,
stephan

Share this post


Link to post
Share on other sites
I guess it will definately work if i switch to writing depth to alpha. But I dont really want to do that right now because i'd force every material in the world to have a shader that does a certain thing. And that would destroy the whole design.

Doesnt there have to be a way to read this depth texture I have properly? Why cant the fragment shader read the 24 bit depth value from the texture and actually use the 24 bit precision ?

Share this post


Link to post
Share on other sites
If feel I should point out that, atm, a depth read back on X800 hardware destorys your framerate, classic z-buffer readback shadow mapping solutions crash to ~1fps on my X800XT PE and the problem is indeed the readback.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!