Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:28:26+00:00 2026-05-23T15:28:26+00:00

Sampling from a depth buffer in a shader returns values between 0 and 1,

  • 0

Sampling from a depth buffer in a shader returns values between 0 and 1, as expected.
Given the near- and far- clip planes of the camera, how do I calculate the true z value at this point, i.e. the distance from the camera?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:28:26+00:00Added an answer on May 23, 2026 at 3:28 pm

    From http://web.archive.org/web/20130416194336/http://olivers.posterous.com/linear-depth-in-glsl-for-real

    // == Post-process frag shader ===========================================
    uniform sampler2D depthBuffTex;
    uniform float zNear;
    uniform float zFar;
    varying vec2 vTexCoord;
    void main(void)
    {
        float z_b = texture2D(depthBuffTex, vTexCoord).x;
        float z_n = 2.0 * z_b - 1.0;
        float z_e = 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
    }
    

    [edit] So here’s the explanation (with 2 mistakes, see Christian’s comment below) :

    An OpenGL perspective matrix looks like this : from songho.ca

    When you multiply this matrix by an homogeneous point [x,y,z,1], it gives you: [don’t care, don’t care, Az+B, -z] (with A and B the 2 big components in the matrix).

    OpenGl next does the perspective division: it divides this vector by its w component. This operation is not done in shaders (except special cases like shadowmapping) but in hardware; you can’t control it. w = -z, so the Z value becomes -A/z -B.

    We are now in Normalized Device Coordinates. The Z value is between 0 and 1. For some stupid reason, OpenGL requires that it should be moved to the [-1,1] range (just like x and y). A scaling and offset is applied.

    This final value is then stored in the buffer.

    The above code does the exact opposite :

    • z_b is the raw value stored in the buffer
    • z_n linearly transforms z_b from [-1,1] to [0,1]
    • z_e is the same formula as z_n=-A/z_e -B, but solved for z_e instead. It’s equivalent to z_e = -A / (z_n+B). A and B should be computed on the CPU and sent as uniforms, btw.

    The opposite function is :

    varying float depth; // Linear depth, in world units
    void main(void)
    {
        float A = gl_ProjectionMatrix[2].z;
        float B = gl_ProjectionMatrix[3].z;
        gl_FragDepth  = 0.5*(-A*depth + B) / depth + 0.5;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select a random 10% sampling from a small table. I thought
Sampling drag-and-drop between WinForm RichTextBoxes within one application and between them and external applications
Sampling uniformly at random from an n-dimensional unit simplex is the fancy way to
I am doing a study to between profilers mainly instrumenting and sampling. I have
Presently I am trying to read the pixel data from the frame Buffer in
I'm looking for a way to access OpenGL states from a shader. The GLSL
Sampling with Activity Monitor/Instruments/Shark will show stack traces full of C functions for the
I'm used to sampling C-based apps, which every few milliseconds sees what function stack
I'm sampling a real-world sensor, and I need to display its filtered value. The
Does windows have any decent sampling (eg. non-instrumenting) profilers available? Preferably something akin to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.