Near and Far Clipping – Interactive 3D Graphics

July 31, 2019 posted by

Here you can see the effect of moving the near and far clipping planes through the scene. Seeing this kind of clipping is usually a bug, not a feature. An algorithm such as ray tracing doesn’t have this sort of problem, as the mechanism there is to shoot rays from the eye. These two values, near and far, are necessary for rasterization to work sensibly, due to its use of the projection matrix. Well, at the minimum, you need to set the near distance. It’s possible to form a projection matrix that gives a frustum with no far limit, the pyramid. The key thing about these two values is you want to set them to be as close together as you can, without causing any clipping to occur. The near plane is particularly important to move as far as possible away from the camera. The near and far values determine how the z buffer value is computed. Internally the z buffer value typically gets stored as an integer value with some number of bits. For example, 24 bits is common with 8 bits for what’s called the stencil buffer. This is a separate buffer I’m not going to talk about in this course but that can be used for on-screen clipping and other effects. The z buffer has lots of bits, but not an infinite number of them. For example, if you’re rendering a sheet of paper on top of your desk you can easily get z fighting even if you’ve modeled everything correctly and the sheet is slightly above the desk. At some pixels, the z value of the paper and the desktop will have the same value and the desktop can then bleed on through. The z depth range of values is spread between the near and far distances. It’s clear that having these two distances close together directly benefits precision. However, with the perspective transform in particular, you want to move the near plane as far from the I as possible. Here’s an example. Say we have our near plane at a distance of one unit away from the camera, and the far plane ten units away. The NDC z depth does not vary linearly, but instead forms a hyperbolic curve. For example, say we have an object that’s seven units away. The NDC z value’s about 9.0 when the near distance is one unit. In other words, the z depths of more distant objects are relatively higher. These objects that are farther away have to share a small range of z depth values. And so are more likely to exhibit z fighting. The reason the z depth values vary in this non linear way has to do with interpolation. We want straight lines to stay straight when using perspective projection. I won’t prove it to you here. But think of train tracks disappearing into the distance. Near the camera, the railroad ties are visually far apart. As you move toward the horizon, the tracks get closer and closer together. The distance between the tracks is the same, of course. And the track stays straight. But the distance between them on the image changes. The w value for homogeneous coordinates is interpolated linearly. But when used for division, gives us this differing rate of change. To get back to setting the near and far planes. Say we’re able to safely move the near plane to a distance of five units, and not cause clipping. We’re effectively taking this piece of our original graph, and stretching it to our new range. First, we get the simple benefit of having a smaller range between the near and far. We also get a more linear graph. The more you increase the near plane relative to the far plane, the slower the NDC z depth actually goes to 1. The long and short is that moving the near plane away from the camera has a large benefit, much larger than moving the far plane in by a similar distance. Of course this all begs the question, how do we know where to set these planes. The far distance is usually relatively easy to compute. Either we know in advance or perform some rough computation to determine the distance to the farthest object in the scene. The near clipping plane is trickier. You usually have to have some rules such as not letting your camera get too close to the walls. Or some rule of thumb such as the, the near plane will be 1 1,000th the distance of that of the far plane. Some more elaborate systems will do a prepass, setting the near plane very close to the camera. It’ll do a quick render of nearby objects, to determine a good distance for setting of the scene, and then render the whole scene. There’s no single perfect solution.

1 Comment

One Reply to “Near and Far Clipping – Interactive 3D Graphics”

  1. Halbes Hähnchen says:

    If only all tutorials were this good.

Leave a Comment

Your email address will not be published. Required fields are marked *