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!

13 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.

    Reply
  3. website

    But what are these government credit card debt help all about?
    Debt management companies and they can speak to banks and credit card debts,
    people are exploring various options for Credit Card Debt Help.
    If the sum that you will save which you can pay. Well, why don’t you make use of the expertise of a professional settlement company. That is why there may be other methods that the government has offered a huge stimulus package to credit card debt using great debt.

    Feel free to surf to my web page – website

    Reply
  4. handbags purses wholesale

    Wonderful goods from you, man. I have understand your stuff previous to and
    you are just too great. I really like what you’ve acquired here, really like what you’re stating and the way in which
    you say it. You make it entertaining and you still take care of
    to keep it wise. I can not wait to read far
    more from you. This is really a wonderful site.

    Reply
  5. is hostgator good

    A domain name is the URL you will type in to
    access your blog – if you’ve used the web, you know what this is.
    The top three niches are weight loss, relationships and how
    to make money online. The particular differences between the a few choices the number of internet domain names
    that could be managed, your assistance presented,
    as well as whether or not you receive a no cost devoted Ip address and
    also nameless File transfer protocol bank account or not.

    Reply
  6. batman arkham sin city masks

    A car crash, a desperate girl, a mysterious old hotel and a submerged town
    set the scene in this thrilling adventure. The company
    announced that ‘Naruto Shippuden: Ultimate Ninja Storm 3 Full
    Burst’ will be released first for the Playstation 3 and Xbox 360 current-generation systems on Oct.
    com also as it is the most trusted search engine around
    the globe.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>