另外一篇 英文资料(转自
http://www.intel.com/cd/ids/developer/apac/zho/dc/games/optimization/170939.htm?page=4)
使用Z-Bias解决Z-Fighting问题的替代方案
|
目录
Introduction
Alternative Method 1: Projection Matrix
Alternative Method 2: Viewport
Alternative Method 3: Depth Bias
Conclusion
Additional Resources
Alternative Method 3: Depth Bias
The last method addressed in this article uses the DirectX 9 Depth Bias method to solve z-fighting. A check to verify that the graphics card is capable of performing depth bias is needed. Intel Integrated Graphics will support depth bias in the next graphics core code named Grantsdale. After checking the cap bits to verify that depth bias is supported, this technique merely requires setting D3DRS_SLOPESCALEDEPTHBIAS and D3DRS_DEPTHBIAS to the proper values to get the desired effect.
The following code snippet shows the depth-bias alternative to using a DirectX z-bias call:
BOOL m_bDepthBiasCap; // TRUE, if device has DepthBias Caps
// Globals used for Depth Bias
float g_fSlopeScaleDepthBias = 1.0f;
float g_fDepthBias = -0.0005f;
float g_fDefaultDepthBias = 0.0f;
// Check for devices which support the new depth bias caps
if ((pCaps->RasterCaps & D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS) &&
(pCaps->RasterCaps & D3DPRASTERCAPS_DEPTHBIAS))
{
m_bDepthBiasCap = true; // TRUE, if DepthBias Caps
}
// Billboards are rendered...
// DepthBias work around
if ( m_bDepthBiasCap ) // TRUE, if DepthBias supported
{
// Used to determine how much bias can be applied
// to co-planar primitives to reduce z fighting
// bias = (max * D3DRS_SLOPESCALEDEPTHBIAS) + D3DRS_DEPTHBIAS,
// where max is the maximum depth slope of the triangle being rendered.
m_pd3dDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, F2DW(g_fSlopeScaleDepthBias));
m_pd3dDevice->SetRenderState(D3DRS_DEPTHBIAS, F2DW(g_fDepthBias));
}
// Posters are rendered...
if ( m_bDepthBiasCap ) // TRUE, if DepthBias supported
{
// DepthBias work around
// set it back to zero (default)
m_pd3dDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, F2DW(g_fDefaultDepthBias));
m_pd3dDevice->SetRenderState(D3DRS_DEPTHBIAS, F2DW(g_fDefaultDepthBias));
}
. . .
Like the other methods (and like the original z-bias), some tweaking may be necessary, but using D3DRS_SLOPESCALEDEPTHBIAS and D3DRS_DEPTHBIAS is a relatively consistent technique for resolving z-fighting issues across a wide selection of graphics devices. The figure below shows the result of this alternate solution:
Figure 4. Z-fighting solved with depth bias solution.
As Figure 4 shows, care should be taken for adjusting the D3DRS_SLOPESCALEDEPTHBIAS and D3DRS_DEPTHBIAS. They can be very sensitive and lead to other issues like the problem below for distant objects:
Figure 5. Depth-bias solution possible issue: unwanted overlapping polygons.