|
IntroductionAs one will quickly find if working with a wide range of depths in a scene, the use of Z based depth can leave much to be desired. While depth resolution near to the viewer may be quite good, the resolution rapidly degrades farther into the scene. An alternative to this, supported through many available Direct3D compliant video adapters, is eye-relative depth buffers, known as W buffers. We will take a look at the differences between these techniques of depth buffering, and learn how to implement W-buffers in Direct3D. Non-Linearity of Z-BuffersAfter transformation, the location of a vertex has been transformed to a position relative to the viewer. Following transformation, the resulting 3D coordinates are divided by the results from the fourth column of the projection transform (W):
This provides the visual effect of perspective, by drawing points in the distance closer together. However, it is in this operation that the issue arises. The Z values are scaled to a range of 0.0 to 1.0, but the distribution of values is very non-linear. For example, with a near clipping plane of 10 and a far clipping plane of 1000, the Z depth scaling is as shown below:
As you can see, half of the number space is used in the first 10 units of distance after the near plane. At a distance of 100, over 90% of the range (and thus the resolution) has been used. W Based Depth BufferingW-Buffers offer an alternative to Z based depth buffers, and provide greatly improved linearity. Rather than being based upon Z/W, depth is determined from W. As W is directly proportional to the Z depth relative to the viewer, the result is a linear interpretation of depth. The resulting values are scaled to a range of 0.0 to 1.0, based upon the third column of the matrix. The depth is scaled within the same range as Z, so that at the near plane Z=W=0.0, and at the far plane Z=W=1.0. For such scaling to properly occur, the value of W must be equal to the input value of Z. That is, the fourth column of the projection matrix must be equal to [0,0,1,0]. This is what is known as a W-friendly projection matrix, as shown below:
Where:
Note that the helper functions provided with the D3D framework and the D3DX library generate W-friendly projection matrices. Implementing a W-BufferUtilizing W based depth buffers is actually a trivial task. A depth buffer is created and attached to the rendering surface, in the same manner as for a Z buffer. Once a D3D device has been created, it must first be evaluated to determine if W-buffers are supported: D3DDEVICEDESC7 caps; If W-Buffers are supported, then depth buffering is enabled for W-Buffering: lpDevice->SetRenderState(D3DRENDERSTATE_ZENABLE,D3DZB_USEW); Otherwise, normal Z-Buffering is enabled: } else { A couple of final notes about using W-buffers. Even if you are performing your own transformation, you must set a valid projection matrix, as the factors for Z scaling and offset will be utilized to scale W. Also, be sure that your transformed vertices have valid RHW values, as W will be derived by taking the reciprocal of this value (1/RHW). Finally, note that W based depth buffers are not necessarily the right choice for all applications. In some cases, the increased resolution at near distances is needed, and Z-buffers are indicated. To determine which is best for your application, I recommend that you test with both Z and W based depth buffers. |
Visitors Since 1/1/2000:
|