Jump to content
  • Advertisement
Sign in to follow this  

How to convert gl_FragDepth to real world depth ?

This topic is 2782 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 am trying to generate a range image in a fragment shader.

I would like to know how to transform the gl_FragDepth (which is a non-linear transformed value of the actual range in the real world) to get the actual range ?

I have tried the following without success:

float MyLinearizeDepth(float z)
float n = 1.0; // camera z near
float f = 100.0; // camera z far
return (2.0 * n) / (f + n - z * (f - n));

I have used the FrontClipDistance for n and BackClipDistance (I am using JAVA3D) for f . I have my view set-up to look at a flat plane located 50 units in front of the camera. I get an actual range of 0.019 !

What I am doing wrong ?

Share this post

Link to post
Share on other sites
Would this not be easier by using gl_FragCoor?

The .z of that contains the window normal z, so you just need to add the near plane distance to it.

Share this post

Link to post
Share on other sites
gl_FragCoord.z is the depth value (0 to 1).
gl_FragDepth is a write-only value which lets you modify the depth written to the depth buffer.


To OP: from where did you come up with that equation? It could be right, but I just don't recognize it.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!