the value you want I simply called wdepth, look how simple it is:
// read underwater depth
float wdepth = getDepth(refcoord) - dist;
where dist is the distance to vertex from eye... and for some reason you only calculate length() of the scalar.z which... is the same as doing sqrt(z*z) == z.
dist = length(v_pos);
Btw. you cant send "dist" in my example as input to fragment shader, because it doesnt interpolate well on stuff not in a grid pattern,
instead you need to send vec3 viewPos, and then take the length in the fragment shader.
----------------
Lets keep things simple and forget all about the window-space-ness of the depth-value and just use it directly.
refcoord here is the texture-coord of as seen on-screen. I hope that is abundantly clear, as you are reading a screen-space texture.
vec2 refcoord = gl_FragCoord.xy / screenSize.xy;
from this we can read from out texture:
float depth = getDepth(refcoord);
float getDepth(in vec2 uv)
{
// exp. depth in window-space
float wsDepth = texture(depthtexture, uv).x;
// linear depth in window-space
wsDepth = ZNEAR / (ZFAR - wsDepth * (ZFAR - ZNEAR));
// this converts it to eye-space (but lets ignore that for now, since the value is almost
// the same anyways, google nearplanehalfsize after you get the reconstruction working)
//return wsDepth * length(vec3((uv * 2.0 - vec2(1.0)) * nearPlaneHalfSize, -1.0));
return wsDepth;
}
You should now have calculated linear depth that is good enough to prove that this works with.
What you should be able to see if you use the depth difference correctly is that when you rotate the camera that depth changes "slightly" on edges of the screen on the left and right, as well as on the top and bottom. This is because the real eye-vector forms a small dome: The distance to a point on a Z plane are not constant.
Now that you have the depth difference, you need to use it for something useful.
The difference value is for example pretty small, so you need rescale it to fit your water
for example:
float deep = min(1.0, wdepth * 16.0);
color = mix(shallowColor, deepColor, deep);
Some proof here: http://fbcraft.fwsnet.net/proof.png
Calculation for nearPlaneHalfSize:
// calculate half near-plane size
const double pio180 = 4.0 * atan(1.0) / 180.0;
float halfTan = tan(fov * pio180 / 2.0);
nearPlaneHalfSize = vec2(halfTan * aspect, halfTan);