记录一下:
根据
struct m3dtriangleinfo
{
float32 fCommonGradient; ///< Gradient constant.
const m3dvsoutput *pBaseVertex; ///< Base vertex for gradient computations.
/// z partial derivatives with respect to the screen-space x- and y-coordinates.
float32 fZDdx, fZDdy;
/// w partial derivatives with respect to the screen-space x- and y-coordinates.
float32 fWDdx, fWDdy;
shaderreg ShaderOutputsDdx[c_iPixelShaderRegisters]; ///< Shader register partial derivatives with respect to the screen-space x-coordinate.
shaderreg ShaderOutputsDdy[c_iPixelShaderRegisters]; ///< Shader register partial derivatives with respect to the screen-space y-coordinate.
/// Integer-coordinates of current pixel; needed by pixel shader for computation of partial derivatives.
uint32 iCurPixelX, iCurPixelY;
float32 fCurPixelInvW; ///< 1.0f / w of the current pixel; needed by pixel shader for computation of partial derivatives.
};
知道了 fZDdx, fZDdy 其实就是 z partial derivatives with respect to the screen-space x- and y-coordinates.就是 ∂z/∂x, ∂z/∂y
那么 怎么计算 ∂z/∂x, ∂z/∂y, 在Muli3D给出来结果:
// v1.x - v0.x, v2.x - v0.x
const float32 fDeltaX[2] = { i_pVSOutput1->vPosition.x - i_pVSOutput0->vPosition.x, i_pVSOutput2->vPosition.x - i_pVSOutput0->vPosition.x };
// v1.y - v0.y, v2.y - v0.y
const float32 fDeltaY[2] = { i_pVSOutput1->vPosition.y - i_pVSOutput0->vPosition.y, i_pVSOutput2->vPosition.y - i_pVSOutput0->vPosition.y };
// value = 1.0f / (v1.x - v0.x) * (v2.y - v0.y) - (v2.x - v0.x) * (v1.y - v0.y)
m_TriangleInfo.fCommonGradient = 1.0f / ( fDeltaX[0] * fDeltaY[1] - fDeltaX[1] * fDeltaY[0] );
// 设置pBaseVertex
m_TriangleInfo.pBaseVertex = i_pVSOutput0;
// The derivatives with respect to the y-coordinate are negated, because in screen-space the y-axis is reversed.
// v1.z - v0.z , v2.z - v0.z
const float32 fDeltaZ[2] = { i_pVSOutput1->vPosition.z - i_pVSOutput0->vPosition.z, i_pVSOutput2->vPosition.z - i_pVSOutput0->vPosition.z };
// ((v1.z - v0.z) * (v2.y - v0.y) - (v2.z - v0.z) * (v1.y - v0.y)) / (v1.x - v0.x) * (v2.y - v0.y) - (v2.x - v0.x) * (v1.y - v0.y)
m_TriangleInfo.fZDdx = ( fDeltaZ[0] * fDeltaY[1] - fDeltaZ[1] * fDeltaY[0] ) * m_TriangleInfo.fCommonGradient;
// -((v1.z - v0.z) * (v2.x - v0.x) - (v2.z - v0.z) * (v1.x - v0.x)) / (v1.x - v0.x) * (v2.y - v0.y) - (v2.x - v0.x) * (v1.y - v0.y)
m_TriangleInfo.fZDdy = -( fDeltaZ[0] * fDeltaX[1] - fDeltaZ[1] * fDeltaX[0] ) * m_TriangleInfo.fCommonGradient;
那么问题来了,上面是怎么推导出来的:
主要参考 : http://blog.youkuaiyun.com/aa20274270/article/details/70159075
开始推导:
这里就已经推导出来了,
对比代码的 m_TriangleInfo.fZDdx
((v1.z - v0.z) * (v2.y - v0.y) - (v2.z - v0.z) * (v1.y - v0.y)) / (v1.x - v0.x) * (v2.y - v0.y) - (v2.x - v0.x) * (v1.y - v0.y)
和 m_TriangleInfo.fZDdy
-((v1.z - v0.z) * (v2.x - v0.x) - (v2.z - v0.z) * (v1.x - v0.x)) / (v1.x - v0.x) * (v2.y - v0.y) - (v2.x - v0.x) * (v1.y - v0.y)
完全一致。
接下来,对于 m3dtriangleinfo 的 属性 fWDdx, fWDdy; 方法也是一样的。