Finally I managed to make the depth buffer linear using shaders. Here is the shaders' code for anyone that might be interested.
void main(void)
{
gl_FrontColor = gl_Color;
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}
and the frag:
#version 110
#extension GL_ARB_texture_rectangle : enable
//Shader for visualizing a depth texture as a focal distance map
//paired with dofblur for debugging purposes
uniform sampler2DRect depthTex;
uniform float focalDistance;
uniform float focalRange;
float LinearizeDepth(float zoverw){
float n = 1.0; // camera z near
float f = 2000.0; // camera z far
return (2.0 * n) / (f + n - zoverw * (f - n));
}
void main()
{
float depth = LinearizeDepth( texture2DRect(depthTex, gl_TexCoord[0].st).r ) * 20000.;
gl_FragColor = vec4(min( abs(depth - focalDistance) / focalRange, 1.0) );
gl_FragColor.a = 1.0;
}