Sign in to follow this  

Linear Depth Buffer

This topic is 1264 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 I'm new here I hope I'm not making mistakes with this post! if so please tell me! 

 

I'm  rendering a scene on a render target, I've then to access its depth information in a post-processing stage. In this stage I need linear depth info. I've been browsing the internet quite a lot and found a lot of different approaches/alternatives. What do you think is the way to go? I'm a bit of a newbie with this stuff so can you give me an hint/pointer on the possible GLSL code?

 

Thank you very much :) 

 

Micheal

Share this post


Link to post
Share on other sites

Typically you'll read the value from your non-linear depth buffer and reconstruct the view-space z position at that point.

// let s and t be two values that satisfy the following two equations:

(s*f + t)/f = 1    // f is the distance from the eye to far plane
(s*n + t)/n = -1   // n is the distance from the eye to near plane

// the above is simply the mapping the occurs when you do a projection transform
// s and t are found in your projection matrix at positions 3,3 and 3,4, respectively

// let d be the value in your depth buffer

// the (xxx + 1) * 0.5 is to map the [-1,1] clip-space coordinates to the [0,1] space in the depth buffer
d = ((s*z + t)/z + 1) * 0.5  
2d = (s*z + t)/z + 1
2d - 1 = (s*z + t)/z
2d*z - z = s*z + t
2d*z - z - s*z = t
z*(2d - 1 - s) = t
z = t / (2d - 1 - s)   // reconstructed z value

That math is just off the top of my head and might not be totally correct, but you should be able to get the picture from that. You basically just want to use your knowledge of the projection matrix you used when transforming the verts to do the inverse of that transformation and reconstruct the view-space z.

Edited by Samith

Share this post


Link to post
Share on other sites

Typically you'll read the value from your non-linear depth buffer and reconstruct the view-space z position at that point.

// let s and t be two values that satisfy the following two equations:

(s*f + t)/f = 1    // f is the distance from the eye to far plane
(s*n + t)/n = -1   // n is the distance from the eye to near plane

// the above is simply the mapping the occurs when you do a projection transform
// s and t are found in your projection matrix at positions 3,3 and 3,4, respectively

// let d be the value in your depth buffer

// the (xxx + 1) * 0.5 is to map the [-1,1] clip-space coordinates to the [0,1] space in the depth buffer
d = ((s*z + t)/z + 1) * 0.5  
2d = (s*z + t)/z + 1
2d - 1 = (s*z + t)/z
2d*z - z = s*z + t
2d*z - z - s*z = t
z*(2d - 1 - s) = t
z = t / (2d - 1 - s)   // reconstructed z value

That math is just off the top of my head and might not be totally correct, but you should be able to get the picture from that. You basically just want to use your knowledge of the projection matrix you used when transforming the verts to do the inverse of that transformation and reconstruct the view-space z.

 

Thank you Samith, just one question. In which space t and s are distances eye-near/eye-far? If I look in my projection matrix (built via glm::perspective) at position 3,3 and 3,4 what I see is something like -1.03 and -1 where my clipping planes are [0.1, 8] 

Share this post


Link to post
Share on other sites

Thank you Samith, just one question. In which space t and s are distances eye-near/eye-far? If I look in my projection matrix (built via glm::perspective) at position 3,3 and 3,4 what I see is something like -1.03 and -1 where my clipping planes are [0.1, 8] 

 

Yep. The s and t values are computed given the near and far planes. In my post above I said that s and t are two values that satisfy the equations (s*f + t)/f = 1 and (s*n + t)/n = -1, but I didn't provide any actually solution for s and t tongue.png

 

If you are interested, the solutions for s and t can be found like so:

(sf + t)/f = 1
(sn + t)/n = -1

// here you have two linear equations and two unknowns. you can solve
// for s and t with your basic high school algebra techniques

eq1: sf + t = f
eq2: sn + t = -n

// subtract eq2 from eq1
eq3: s(f-n) = f+n
     s = (f+n)/(f-n)

// substitute s into eq1 and solve for t
eq4: sf + t = f
     f(f+n)/(f-n) + t = f
     f(f+n) + t(f-n) = f(f-n)
     f(f+n) - f(f-n) = -t(f-n)
     ff+fn - ff+fn   = -t(f+n)
     -2fn/(f+n)      = t

// now you can check s and t given a near and far plane of 0.1 and 8.0
// s = (f+n)/(f-n) = (8-0.1)/(8+0.1) =~ 1.025
// t = 2fn/(f+n)  =~ -0.19

Some caveats: these computations depend on some nitpicky details about your projection. For example, if you use your projection matrix to transform a right handed coordinate space into the left handed clip space, then the original equations will be slightly different (ie s*-f + t = f instead of sf + t = f, etc). Also, the s and t values will be found in your projection matrix in the 3,3 location and 3,4 location if your projection matrix is column major, but they'll be in 3,3 and 4,3 if your projection matrix is row major.

 

I don't know enough about your exact environment to give you all the answers, but I hope the algebra I've posted at least gets you started down the right path.

Edited by Samith

Share this post


Link to post
Share on other sites

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