Linear depth texture

In some occasions a linear depth texture is preferred over the usual non-linear depth buffer (z-buffer). The linearized depth could for example be used when doing SSAO or depth of field. Rendering linear z-buffers is very easy and only the following lines of code is required ( a float texture should be set as render target):

Vertex shader:

varying float depth;
void main()
{
    vec4 viewPos = gl_ModelViewMatrix * gl_Vertex; // this will transform the vertex into eyespace
    depth = -viewPos.z; // minus because in OpenGL we are looking in the negative z-direction
    gl_Position = ftransform();
}

Fragment shader:

varying float depth;
void main()
{
    gl_FragColor.r = depth;
}

 
You might want to scale the depth in the vertex shader to a more appropriate range as the the distance will otherwise be the same as the distance from the camera to the vertex. The following vertex shader will map the near and far distances to 0..1 which is often a good idea.

varying float depth;
void main()
{
    vec4 viewPos = gl_ModelViewMatrix * gl_Vertex; // this will transform the vertex into eyespace
    depth = (-viewPos.z-near)/(far-near); // will map near..far to 0..1
    gl_Position = ftransform();
}

Here’s how the shader looks like if you render a cube.

Linear Depth Rendering of a Cube

Link to a page about the depth buffer in DirectX
http://www.mvps.org/directx/articles/linear_z/linearz.htm

A good introduction to the depth buffer in OpenGL
http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html

Be Sociable, Share!

3 thoughts on “Linear depth texture

  1. Pingback: SSAO in stage 3D « flashing in public

  2. Texter

    I’ve been exploring for a bit for any high-quality articles or weblog posts on this kind of area . Exploring in Yahoo I eventually stumbled upon this web site. Studying this info So i’m glad to conνey that
    I’ve an incredibly just right uncanny feeling I came upon exactly what I needed. I so much surely will make certain to do not disregard this web site and give it a look regularly.

  3. Curs Valutar

    Un Curs Valutar de Linear depth texture conversie valutara nu este o gestatie
    usoara de a efectua de zi cu zi cu modificarea ratelor Curs Valutar straine .

    Fiind o para straina , soroc la cine piesa locala se va aprecia pentru Curs Valutar puterea de achizitionare
    in stat straina se a ramplasa in fiecare moment uro .

Comments are closed.