本来在ogre中只是设置下depth-bias和slope bias。发现ogre还多了Iteration depth bias。看到代码里发现,原来ogre对于多个pass使用偏移有一定的优化,优化方式就是引入了一个变量mDepthBiasPerIteration。从而确保一个模型的材质多个pass都发生z-flighting的话而产生z-flighting。
他引擎里的那部分设置的源码我拷出来好了。大家看看知道就好了。自己做引擎不一定要和这个一样。
mDestRenderSystem->setCurrentPassIterationCount(pass->getPassIterationCount());
// We might need to update the depth bias each iteration
if (pass->getIterationDepthBias() != 0.0f)
{
float depthBiasBase = pass->getDepthBiasConstant() +
pass->getIterationDepthBias() * depthInc;
// depthInc deals with light iteration
// Note that we have to set the depth bias here even if the depthInc
// is zero (in which case you would think there is no change from
// what was set in _setPass(). The reason is that if there are
// multiple Renderables with this Pass, we won't go through _setPass
// again at the start of the iteration for the next Renderable
// because of Pass state grouping. So set it always
// Set modified depth bias right away
mDestRenderSystem->_setDepthBias(depthBiasBase, pass->getDepthBiasSlopeScale());
// Set to increment internally too if rendersystem iterates
mDestRenderSystem->setDeriveDepthBias(true,
depthBiasBase, pass->getIterationDepthBias(),
pass->getDepthBiasSlopeScale());
}
else
{
mDestRenderSystem->setDeriveDepthBias(false);
}
depthInc += pass->getPassIterationCount();
然后我在网上搜了下,发现原来intel做过一个实验,他用了三种方法来测试z-bias。
第一种方法Projection Matrix偏移。
D3DXMatrixPerspectiveFovLH( &mZBiasedProjectionMat, D3DX_PI/4,(mProjectionMat._22/mProjectionMat._11),
g_fBaseNearClip + g_fNearClipBias,
g_fBaseFarClip + g_fFarClipBias );
第二个是Viewport矩阵偏移。
// Change by the bias
mNewViewport.MinZ -= g_fViewportBias;
mNewViewport.MaxZ -= g_fViewportBias;
第三个就是我们通用的硬件偏移就是ogre的做法。
先检查硬件是否能用,能用就设置数字。
// 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));
其实深度远了都会丢失。
我自己也没测试,我就用硬件法测试了下,因为公式是(max * D3DRS_SLOPESCALEDEPTHBIAS) + D3DRS_DEPTHBIAS
我D3DRS_SLOPESCALEDEPTHBIAS设置0.1,然后照相机原理发生z-filghting的物体,很快就又纠缠到一起了。设置D3DRS_DEPTHBIAS就会好很多。但是照相机还是不能退太远。具体硬件原理是怎么弄的,我也懒的去想了。每次把值设大就好了。效果和intel实验的结果差不多了。