Sign in to follow this  

GLSL depth texture access

This topic is 3100 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

I have bound a DEPTH_COMPONENT texture to an fbo (with CLAMP, CLAMP, NEAREST, NEAREST, TEXTURE_COMPARE_MODE=NONE, DEPTH_TEXTURE_MODE=LUMINANCE) Binding in GLSL as Sampler2D I am attempting to access the stored depth values I was not expecting the values to be linear, but was a little shocked to find that most values come in the range of 0.999 to 1.0 Clearly I'm reading this wrongly. The sampling on it is very, very steep What's the correct procedure for extracting linear values from the depth texture component? Am I going about this totally the wrong way? Looking @ other examples on the net I see most people seem to be generating their own depth data instead (either in the depth channel, or on an extra colour attachment etc). Ideally I'd like to do this with fixed functionality Any help would be very appreciated Cheers all

Share this post


Link to post
Share on other sites
Try this:


uniform vec2 CameraRange; // x is ZNear, y is ZFar

float linearize_depth(in float depth)
{
return (2.0 * CameraRange.x) / (CameraRange.y + CameraRange.x - depth * (CameraRange.y - CameraRange.x));
}


Share this post


Link to post
Share on other sites
Wow. Cheers
I'll give that a shot when I get home

So the zNear affects the depth gradient?
I'll have to be more careful with my choice of scale from now on
I was aware there were issues surrounding the use of particularily low zNear's, but never really understood why.

Once again - thanks a load

Share this post


Link to post
Share on other sites
Yes, since the depth buffer samples depth values between near and far non-lineraly the choice of near and far (especially the far/near ratio) can heavily affect depth precision.

Check this out, especially 12.050.

Share this post


Link to post
Share on other sites

This topic is 3100 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this