yiannis
November 24, 2015, 10:22am
#1
Hi,

I am drawing the depth buffer from a a fbo where I draw some cubes, and I get a white image with some light gray areas where the cubes where very close. After some research I found that I have to make the depth buffer linear according to the near and far field of my camera. Any ideas how to do it?

Thanks

yiannis
December 1, 2015, 7:06pm
#2
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;
}
```