I bet if you change the split point from 6000 to 1208 your problems will mostly disappear.

The normalized depth value in the range [-1, 1] of a given distance into the screen is given by:

(f + n) / (f - n) - 1/z * 2 f n / (f - n),

where f = far clip plane distance, n = near clip plane distance, and z = distance into the screen.

If you want to map that to the range [A, B], the equation becomes:

D = A + f / (f - n) * (B - A) * (1 - n / z)

If a is the value you give for the near depth range and b is the value you give for the far depth range, and you have S bits in your depth buffer, then A = a * (2^S - 1), B = b * (2^S - 1), and D = the actual integer value stored in the depth buffer.

You can simplify these equations to find the minimum and maximum distinguishable depth values for any combination of near clip plane, far clip plane, and depth buffer precision. In other words, this is how far apart two pixels have to be in screen depth for them to be guaranteed to have different depth values.

The minimum value for minimum distance occurs when D = A + 1, ie just past the near clip plane. The minimum separation at the near clip plane for distinguishable depth values is:

n / [ f/(f-n) * (B-A) - 1 ]

The maximum value for minimum distance occurs when D = B - 1, ie just before the far clip plane. The minimum separation at the far clip plane for distinguishable depth values is:

f / [ n/(f-n) * (B-A) - 1 ]

If you plug the values for n and f in your sample code, and assume a 24-bit depth buffer, you find that your minimum separation ranges from 5.96*10^-8 to 8.59 for the near region, and it ranges from 6.44*10^-4 to 0.0644 for the far region. If you use a split at 1208 instead, your minimum separation in the near region will range from 5.96*10^-8 to 0.348, and in the far region it will range from 1.41*10^-4 to 0.348.

The best results happen when the maximum value for minimum distinguishable distance for the near region and far regions are identical. If you solve the set of equations for where the split x should be given a near distance n, far distance f, and a depth buffer that is to be divided into two equal regions each having S unique values, you get the cubic equation:

x^3 - x^2 * [n + 2 * f / S] + x * [n * f + (n + f) * f / S] - n * f * f = 0

I plugged the values for n, f, and S of 0.5, 60000, and 0.5 * 2^24, respectively, to get the coefficients for the cubic. I then told my calculator to solve for x, and it said 1208.2725, which is close enough to 1208 for me.

[This message has been edited by Coriolis (edited 11-28-2002).]