Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: How does this simple fragment shader function work?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009

    How does this simple fragment shader function work?

    This code is used in a DoF blur fragment shader I found online (I am trying to make one of my own):

    Code :
    uniform sampler2D gdepth; //<- the depth buffer? How/where is this set?
    float getDepth(vec2 coord)
        return 2.0 * near * far / (far + near - (2.0 * texture2D(gdepth, coord).x - 1.0) * (far - near)); //is gdepth equivalent to gl_FragCoord.z?

    How do the mathematics in this depth retrieval function work? I assume it's converting from screen to world space. The terms look strikingly like those found in the projection matrix I ended up using in my CPU-side code:

    Code :
    public static Matrix4 getProjectionMatrix(float fovYDeg, float aspectRatio, float near, float far)
        Matrix4 result = new Matrix4();
        float fovYRad = fovYDeg * (float)PI / 180f; 
        float e = 1f/(float)(tan(fovYRad / 2)); //focal length
        result.val[Matrix4.M00] = e / aspectRatio;
        result.val[Matrix4.M11] = e;
        result.val[Matrix4.M22] = (far + near) / (near - far);
        result.val[Matrix4.M23] = (2 * far * near) / (near - far);
        result.val[Matrix4.M32] = -1f;
        result.val[Matrix4.M33] = 0f;
        return result;

    I really can't just keep letting this stuff go over my head.

    PS. The original shader code is here.
    Last edited by Nick Wiggill; 10-01-2012 at 02:32 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts