View Full Version : Z_Fighting whith FLT_MIN ?

dorinm

02-08-2003, 04:15 AM

hi ppl,

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...

thanks

vincoof

02-08-2003, 04:30 AM

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 :

[1,100] [0.1,10] [10,1000] [x*1, x*100], for any positive real "x".

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.

vincoof

02-08-2003, 04:47 AM

Just to clarify, for any [near1,far1] and [near2,far2] ranges :

in orthographic projection, the depth precision equals if far1-near1==far2-near2

in perspective projection, the depth precision equals if far1/near1==far2/near2

Let's take the example of your [FLT_MIN,100] and [0.000001f,100] ranges :

if your projection is orthographic :

* far1-near1=100 (approx.)

* far2-near2=99.999999

* does far1-near1==far2-near2 ? yes, almost. so we can say that the depth precision does not differ in orthographic.

if your projection is orthographic :

* far1/near1=infinity (approx.)

* far2/near2=100000000

* does far1/near1==far2/near2 ? not at all. so we can say that the depth precision are different in perspective.

dorinm

02-08-2003, 05:07 AM

thanks guys for your help, now i can see where i was wrong !

shame on me for not thinking of far/near http://www.opengl.org/discussion_boards/ubb/biggrin.gif , thanks vincoof ...

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.