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):
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:
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;
I really can't just keep letting this stuff go over my head.
PS. The original shader code is here.