# How To Solve Z buffer Fighting

This topic is 2036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

Is there any other way to solve z buffer fighting without changing the near plane value.

I am using

gluPerspective(fov,(winWid/winHgt),0.001,100000.0);

Thanks,

##### Share on other sites
Yes, change the near plane. 0.001 is an absolutely ridiculous value for it; unless you can demonstrate an absolute proven need to have this value for your near plane (and I highly doubt that) - change it.

##### Share on other sites
On What basis I have to Change to Near and far values. I am new to this opengl 3d Programming

Edit1:If I push the near value 1.0 or greater till 1.0 distance Its not possible to see in the viewport Edited by Haris072

##### Share on other sites
Use a 24 bit depth buffer for better precision. It is supported on everything.
http://www.opengl.org/wiki/Common_Mistakes#Depth_Buffer_Precision

##### Share on other sites

gluPerspective(fov,(winWid/winHgt),0.001,100000.0);

This is not even remotely doable with the standard depth buffer - your near plane is absolutely ridonkulous in regards to the far plane.

You have two options:
* use sane near and far distances.
* use floating point depth buffer (if your target supports them).

##### Share on other sites

Use a 24 bit depth buffer for better precision. It is supported on everything.
http://www.opengl.or...uffer_Precision

Given his near far plane distances - that wont help.

##### Share on other sites
You need to explain what it is you're trying to achieve.

Assuming you're using a scale of 1 unit = 1 metre, you're asking for millimetre precision over one hundred kilometres. Which is impossible given the number of bits in the depth buffer.

Try 0.1 to 1000.0, which is really pushing it, but may be acceptable in some cases.

Basically, you need to reduce the number of zeros after the decimal point for the near value, and the number of zeros after the decimal point for the far. It's the ratio between the two that counts: you're asking for a ration of 1:100000000 which can't be expressed in a 24 bit depth buffer.

EDIT - having read you're post again, what are the furthest depth values you need? Maybe try 0.001 near to 1.0 far. Edited by mark ds

##### Share on other sites
If you gave us a scale for your scene we could give you better guesses as to what values you should use.
1 = centimeters?
1 = meters?

If 1 unit = 1 centimeter (Half-Life 2, GoldenEye 007, Final Fantasy VII), you could probably get away with ranges from 10.0 to 100000.0. The low value makes more of a difference than the high value, so increasing this improves your Z quality by the most amount. 100,000 is normally a ridiculous far value, but with a near of 10.0 it isn’t too bad. Still might have some Z-fighting in detailed areas at a long-medium range or somewhat-far range.

If 1 unit = 1 meter, 0.1 to 1000.0 would be the equivalent.

In any case, you generally do not need a near value smaller than the radius of your player. For example, in an FPS the player is a capsule or cylinder, both of which have some fixed radius that keeps them that distance from walls etc. Since you can’t get closer than that to anything in the scene, your near distance should be just slightly under that value. 0.95 is often reasonable if 1 unit = 1 foot.

In any case, a near value of 0.001 is never acceptable.

If you truly need large viewing distances, use logarithmic Z.

L. Spiro Edited by L. Spiro

##### Share on other sites
All of the above, plus: I guess you have a low near clipping distance because you want to draw a first person shooter hand model. In that case there is a trick that everyone uses. Render the hand model in a seperate clip space after you have rendered everything else.

##### Share on other sites
A bit off topic, but I feel that someone should tell you that glu is deprecated, as is all of the fixed function pipeline. Since you are new to 3d graphics, I figure its worth mentioning that. You should read up on the programmable pipeline. Also, this is a great book that I love which helped me transition from fixed function pipeline to programmable pipeline: http://www.amazon.com/OpenGL-4-0-Shading-Language-Cookbook/dp/1849514763/ref=sr_1_1?ie=UTF8&qid=1340288671&sr=8-1&keywords=opengl+4.0+shading+language+cookbook

##### Share on other sites
The reason for pushing the far plane has to do with depth values being written between 0 and 1, Most depth values lie in the range .95 and up and are smashed together. If things in your scene disappear when increasing the near plane, then your objects are way too small. Try scaling them up.

##### Share on other sites
yes. more than half of machines still have fixed pipeline yet.

##### Share on other sites
There is another solution to Z fighting: Multi-pass rendering. Draw your scene in multiple passes. Each pass gets the full 0-1 depth range, and your draw distance can be as long as you'll like. I know this is a good solution to drawing things in space, where the great distances may need to be handled differently that normal.

##### Share on other sites

yes. more than half of machines still have fixed pipeline yet.

But are those machines realistically in your target audience? Or are you compromising your program for the sake of hardware that nobody is even going to run it on?

##### Share on other sites
The real problem may be that you try to draw more than one plane at the same depth, if you have problems with depth buffer fighting. The result will be undefined, random, or flickering. Or all of the above. It is not realistic to draw two things at the same place and have any expectations on the outcome.

You have to decide what surface should be "above", and adjust the coordinates accordingly. If this doesn't help, then you need to adjust resolution (as described above).

The are special mechanisms in OpenGL that adds a small delta to target this problem. It is described in the OpenGL superbible, which I do not have access to just now.