# Zbuffer and clip plane

## Recommended Posts

GaryNas    134
What's the relationship between zbuffers and perspective clipping planes? When I have a .x file with more than one subset I run into zbuffer problems if my near clip plane is 0.0:
D3DXMatrixPerspectiveFovLH(&g_matProj, D3DX_PI * 0.5f, 800.0f / 600.0f, 0.0f, 100.0f);

Screenshot of problem: http://www.visualization.ca/~vdi/Train/train_incorrect.jpg However, if I change the near clip plane to a value > 0.0, say 0.1, my mesh now renders correctly:
D3DXMatrixPerspectiveFovLH(&g_matProj, D3DX_PI * 0.5f, 800.0f / 600.0f, 0.1f, 100.0f);

Screenshot of correction: http://www.visualization.ca/~vdi/Train/train_correct.jpg Can anyone help me understand what exactly is going on?

##### Share on other sites
JohnBolton    1372
The distance to the near clip plane must be > 0.

Ideally, the distance to the near plane would be the smallest number possible (> 0), but the value determines the resolution of the Z-buffer and a smaller distance results in worse resolution. So, generally you want the near plane to be as far from the camera as you can get away with.

The usual rule of thumb is to place the near plane 1/1000th of the way to the farthest object that will be displayed. That will generally give you sufficient Z-buffer resolution for displaying that object. That means that if you want to display an object 1000 meters away, the near plane should be no closer than 1 meter. Keep in mind that this is just a rule of thumb.

As a side note, sometimes people say the near plane should be 1/1000th of the way to the far plane, but that's not as good -- and it doesn't work in the case where the far plane is at infinity.