Here’s a problem i’ve encountered playing with gluPerspective:
I’ve tried to set the near clipping plane as
“near” as it could be , so I used FLT_MIN as defined in float.h win header (~1.175494351e-38F) but then appeared my z fighting problem…
now, my question is:
given zfar let’s say 100,
if I set znear to FLT_MIN, theoreticaly
my range would be 100-FLT_MIN (~100), so
I should have enough precission to
draw let’s say a cube of 3/3/3, right?
Then why that z fight problem?
now if i set znear to a low value (say 0.000001f so my range would be also ~100),
z fighting does not occur.
maybe someone could tell me where am I wrong…
Depth buffer does NOT map linearly (at least, not in perspective view). For instance, you have the same depth precision in the following [near,far] clipping distances :
[ul][li] [1,100] [0.1,10] [10,1000] [x1, x*100], for any positive real “x”.[/ul][/li]
So, when you set the near plane to FLT_MIN, it’s almost like setting it to 0, in terms of depth precision.
The precision of the depth buffer is directly dependent on the ratio of distance to the near and far clip plane. The ratio is calculated as ratio=far/near, and should be as small as possible. A 24-bit depth buffer can handle a ratio of about 5000-10000. Now, given your values, your ratio is WAAAAAAAY too large. If you have the far plane at 100, you should not have the near plane closer than about 0.01 for acceptable precision.
There is a formula (don’t remember where I saw it) which says that the precision loss can be approximated by saying that log2(ratio) bits of depth buffer precision are lost. So with your near and far plane, you have lost about 130 bits of precision. And since no consumer level hardware today supports more than 32 bits, you will not have a working depth buffer.
Just to clarify, for any [near1,far1] and [near2,far2] ranges :
[li] in orthographic projection, the depth precision equals if far1-near1==far2-near2[/li]
[li] in perspective projection, the depth precision equals if far1/near1==far2/near2[/li][/LIST]
Let’s take the example of your [FLT_MIN,100] and [0.000001f,100] ranges :
[li] if your projection is orthographic :[/li]* far1-near1=100 (approx.)
- does far1-near1==far2-near2 ? yes, almost. so we can say that the depth precision does not differ in orthographic.
[li] if your projection is orthographic :[/li]* far1/near1=infinity (approx.)
- does far1/near1==far2/near2 ? not at all. so we can say that the depth precision are different in perspective.
thanks guys for your help, now i can see where i was wrong !
shame on me for not thinking of far/near , thanks vincoof …