Hello, I am fooling arround with webgl and I would like to add fog as a postprocess effect.

So I'm trying to get the original depth value to apply a linear fog.

I've already read through a lot of forum posts but I can't get it to work.

In my depthpass I output the depth as

float depth = gl_FragCoord.z / gl_FragCoord.w; gl_FragColor = vec4( vec3(depth), 1.0 );

And in the postprocessing pass I try to reconstruct it using this method http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffer-in-glsl/.

(my nearplane is 0.1 and farplane of the camera is 20000.0 )

float my_z = (-0.1 * 20000.0) / (depth1 - 20000.0);

Using my_z as depth my output isn't the same as when I just visualize the depth from my depthpass with:

float depth = gl_FragCoord.z / gl_FragCoord.w; float color = 1.0 - smoothstep( 1.0, 200.0, depth ); gl_FragColor = vec4( vec3(color), 1.0 );

So I expected to get the same result when using the reconstructed Z in my postprocessing pass

float my_z = (-0.1 * 20000.0) / (texture2D( tDepth, texCoord ).x - 20000.0); float color = 1.0 - smoothstep( 1.0, 200.0, depth ); gl_FragColor = vec4( vec3(color), 1.0 );

Outputting my_z gives me this result: http://i.imgur.com/8C3reNd.png

So what am I doing wrong here?

**Edited by coffeecup, 19 February 2014 - 06:50 AM.**