Matrix 矩阵乘法

Matrix 矩阵乘法,matrix,hlsl,Matrix,Hlsl,我只是在玩HLSL。我想得到向量“pos”中的向量“inputPos”。案例2起作用,但案例1不起作用。为什么?这两种情况不一样吗? M*M_Inv*inputPos=inputPos。为什么案例1没有给出正确的值 //case 1 pos = mul( float4( inputPos, 1), c_mView ); // Line1 pos = mul ( pos , c_mViewInverse ); // Line2 //case2 pos = mul

我只是在玩HLSL。我想得到向量“pos”中的向量“inputPos”。案例2起作用,但案例1不起作用。为什么?这两种情况不一样吗? M*M_Inv*inputPos=inputPos。为什么案例1没有给出正确的值

//case 1
pos = mul( float4( inputPos, 1), c_mView );     // Line1
pos = mul ( pos ,  c_mViewInverse );            // Line2

//case2
pos = mul ( mul( float4( inputPos, 1), c_mView ) ,  c_mViewInverse );

谢谢。

在您的情况下,变量pos可能是float3,因此如果您在第二个操作中不提供w分量,将导致计算混乱。(在案例2中,您直接使用第一个mul的结果,该结果将是浮点4)

pos = mul( float4( inputPos, 1), c_mView );
pos = mul ( float4(pos,1) ,  c_mViewInverse );