• 10
• 12
• 12
• 14
• 15

# depth-buffer precision

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

## Recommended Posts

Hi, I have a problem of z-buffer precision, and I don't really know how to solve it. I work on a car driving simulator. We have a scene where we are on a highway. In this scene, the far clip is quite far away since we use a 5km² heightmap. But I also need to have a near clip quite close, since our car can come really close to other objects. I use 1 DirectX unit == 1 meter. So I end up having my near clip at something like 0.1 and my far clip at 5000 ... but this gives a lot of z-fighting artefacts. We solved them by increasing the distance between planes where this problem arise, but I'd like to know if there is a technique to avoid z-fighting on scenes with a big range of view. I think it's possible, a lot of games have a really big range of view, but no z-fighting. I just don't know how they do it ^^ Thx for any help.

##### Share on other sites
What depth-format are you using? D3DFMT_D16 is pretty weak, but most hardware supports one of the 24bit formats (e.g. D24X8 or D24S8) which is a simple way of getting better results.

As a pure guess, you might be able to implement some sort of "painters algorithm" to render the far-distant objects (e.g. 1000-5000 meters) seperately from the near-distance objects (0.1-1000 meters). Split them into two passes and tweak the near/far planes and clear the depth buffer before each one. That way you should get maximum resolution for relatively independent parts. The big problem is if you get any artifacts where you split the rendering [oh]

hth
Jack

##### Share on other sites
A common trick to increase the Z-Buffer precision is “divide and conquer”. If you can’t get enough precision in one pass split your rendering in multiple passes. Each pass has to use different near and far planes. Using this technique you can get the full z-buffer precession for every range you use an own pass. To make sure that you don’t get cut lines the ranges should overlap.

Example (in MDX I hope you don’t mind)

            device.BeginScene();            device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 4), 1.0f, 4.95f, 100.0f);            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);// render anything that is in the range [4.05-100]            device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 4), 1.0f, 0.1f, 5.05f);            device.Clear(ClearFlags.ZBuffer, System.Drawing.Color.Black, 1.0f, 0);// render anything that is in the range [0.1-5.05]            //End the scene            device.EndScene();            device.Present();

The number of slices and the range for each slices depends on your assests. Remind that you need to render the Z-slices back to front. Inside one slices the z-buffer will work as always. If you use any multipasse render techniquies make sure that the first pass don’t use alpha blending or you will see strange artifacts on the slice borders.

EDIT: Jack beat me on this. I should write faster.