Muli3D <6> Struct m3dtriangleinfo 的属性 fZDdx,fZDdy 的推导

来源:互联网 发布:网络语洗白白啥意思 编辑:程序博客网 时间:2024/06/05 16:22

记录一下:

根据 

struct m3dtriangleinfo{float32fCommonGradient;///< Gradient constant.const m3dvsoutput*pBaseVertex;///< Base vertex for gradient computations./// z partial derivatives with respect to the screen-space x- and y-coordinates.float32fZDdx, fZDdy;/// w partial derivatives with respect to the screen-space x- and y-coordinates.float32fWDdx, fWDdy;shaderregShaderOutputsDdx[c_iPixelShaderRegisters];///< Shader register partial derivatives with respect to the screen-space x-coordinate.shaderregShaderOutputsDdy[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.xconst 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.yconst 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] );// 设置pBaseVertexm_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.zconst 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.csdn.net/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; 方法也是一样的。



0 0