public static MatrixEx Mult(MatrixEx M1, MatrixEx M2) { MatrixEx tmp = new MatrixEx(); tmp.v[0][0] = M1.v[0][0] * M2.v[0][0] + M1.v[0][1] * M2.v[1][0] + M1.v[0][2] * M2.v[2][0] + M1.v[0][3] * M2.v[3][0]; tmp.v[0][1] = M1.v[0][0] * M2.v[0][1] + M1.v[0][1] * M2.v[1][1] + M1.v[0][2] * M2.v[2][1] + M1.v[0][3] * M2.v[3][1]; tmp.v[0][2] = M1.v[0][0] * M2.v[0][2] + M1.v[0][1] * M2.v[1][2] + M1.v[0][2] * M2.v[2][2] + M1.v[0][3] * M2.v[3][2]; tmp.v[0][3] = M1.v[0][0] * M2.v[0][3] + M1.v[0][1] * M2.v[1][3] + M1.v[0][2] * M2.v[2][3] + M1.v[0][3] * M2.v[3][3]; tmp.v[1][0] = M1.v[1][0] * M2.v[0][0] + M1.v[1][1] * M2.v[1][0] + M1.v[1][2] * M2.v[2][0] + M1.v[1][3] * M2.v[3][0]; tmp.v[1][1] = M1.v[1][0] * M2.v[0][1] + M1.v[1][1] * M2.v[1][1] + M1.v[1][2] * M2.v[2][1] + M1.v[1][3] * M2.v[3][1]; tmp.v[1][2] = M1.v[1][0] * M2.v[0][2] + M1.v[1][1] * M2.v[1][2] + M1.v[1][2] * M2.v[2][2] + M1.v[1][3] * M2.v[3][2]; tmp.v[1][3] = M1.v[1][0] * M2.v[0][3] + M1.v[1][1] * M2.v[1][3] + M1.v[1][2] * M2.v[2][3] + M1.v[1][3] * M2.v[3][3]; tmp.v[2][0] = M1.v[2][0] * M2.v[0][0] + M1.v[2][1] * M2.v[1][0] + M1.v[2][2] * M2.v[2][0] + M1.v[2][3] * M2.v[3][0]; tmp.v[2][1] = M1.v[2][0] * M2.v[0][1] + M1.v[2][1] * M2.v[1][1] + M1.v[2][2] * M2.v[2][1] + M1.v[2][3] * M2.v[3][1]; tmp.v[2][2] = M1.v[2][0] * M2.v[0][2] + M1.v[2][1] * M2.v[1][2] + M1.v[2][2] * M2.v[2][2] + M1.v[2][3] * M2.v[3][2]; tmp.v[2][3] = M1.v[2][0] * M2.v[0][3] + M1.v[2][1] * M2.v[1][3] + M1.v[2][2] * M2.v[2][3] + M1.v[2][3] * M2.v[3][3]; tmp.v[3][0] = M1.v[3][0] * M2.v[0][0] + M1.v[3][1] * M2.v[1][0] + M1.v[3][2] * M2.v[2][0] + M1.v[3][3] * M2.v[3][0]; tmp.v[3][1] = M1.v[3][0] * M2.v[0][1] + M1.v[3][1] * M2.v[1][1] + M1.v[3][2] * M2.v[2][1] + M1.v[3][3] * M2.v[3][1]; tmp.v[3][2] = M1.v[3][0] * M2.v[0][2] + M1.v[3][1] * M2.v[1][2] + M1.v[3][2] * M2.v[2][2] + M1.v[3][3] * M2.v[3][2]; tmp.v[3][3] = M1.v[3][0] * M2.v[0][3] + M1.v[3][1] * M2.v[1][3] + M1.v[3][2] * M2.v[2][3] + M1.v[3][3] * M2.v[3][3]; return tmp; }
public MatrixEx ReverseMat(MatrixEx M1) { //MatrixExOrg.origin return M1; }
void angleMatrix(Vector3fEX v, MatrixEx m) { //定义中间变量 float cr,cp,cy,sr,sp,sy; float tempAngle; /* 计算roll,pitch,yaw角的cos,sin值,用来构造旋转矩阵 */ //yaw:表示围绕y轴的旋转,但数值是向量在XOZ面上的投影与z负的夹角 tempAngle = v.z; sy = (float)System.Math.Sin(tempAngle); cy = (float)System.Math.Cos(tempAngle); //pitch:表示围绕x轴的旋转,但数值是向量在YOZ面上的投影与z负的夹角 tempAngle = v.y; sp = (float)System.Math.Sin(tempAngle); cp = (float)System.Math.Cos(tempAngle); //roll:表示围绕y轴的旋转,但数值是向量在XOY面上的投影与y正的夹角 tempAngle = v.x; sr = (float)System.Math.Sin(tempAngle); cr = (float)System.Math.Cos(tempAngle); m.xBasis = new Vector3fEX(cp * cy, cp * sy, (-sp)); m.yBasis = new Vector3fEX(sr * sp * cy + cr * (-sy), sr * sp * sy + cr * cy, sr * cp); m.zBasis = new Vector3fEX((cr * sp * cy + (-sr) * (-sy)), (cr * sp * sy + (-sr) * cy), cr * cp); }
//俯仰角 左右角 前后角 void animation(int pitches, int rolls, int yaws) { for(int i = 0; i < m_Joint.Count; i ++) { /* 求当前动作矩阵,该矩阵基于初始局部变换 */ MatrixEx motionMatrix = new MatrixEx(); Vector3fEX vTemp = new Vector3fEX(pitches,rolls,yaws); //修改关节角 // vTemp.x = pitches; //这些取值的依据是什么 rotation // vTemp.z = rolls; // vTemp.y = yaws; //设定矩阵的旋转 angleMatrix(vTemp,motionMatrix); //angleMatrix(joint->vCurrent, &motionMatrix); // //当前局部矩阵 = 初始局部矩阵*动作矩阵,程序设定的变换是基于初始局部 m_Joint[i].m_CurLocalMartic = MatrixEx.Mult(m_Joint[i].m_StaLocalMartic , motionMatrix); // 求当前全局矩阵 if (m_Joint[i].ParentId == -1) { m_Joint[i].m_CurGlobMartic = m_Joint[i].m_CurLocalMartic; } else { m_Joint[i].m_CurGlobMartic = MatrixEx.Mult(m_Joint[m_Joint[i].ParentId].m_CurGlobMartic , m_Joint[i].m_CurLocalMartic); } //至此 joint中的所有值都已经赋值完毕了。 } }
public void SetupJointMatrices() { for(int i = 0; i < m_Joint.Count; i++) { MatrixEx relativeMatrix = new MatrixEx(); Vector3fEX vTemp = new Vector3fEX(m_Joint[i].rotation.x, m_Joint[i].rotation.y, m_Joint[i].rotation.z); //设定矩阵的旋转 angleMatrix(vTemp,relativeMatrix); //设定矩阵的平移 relativeMatrix.origin = new Vector3fEX(m_Joint[i].location.x, m_Joint[i].location.y, m_Joint[i].location.z); //这个值 是不是用来补齐矩阵的最后一行的 或者最后一列的 //为关节点的初始局部矩阵赋值 m_Joint[i].m_StaLocalMartic = relativeMatrix; //如果是根节点,则局部矩阵则为全局矩阵 if(m_Joint[i].ParentId == -1) //根节点 { //joint->matStaticGlobal = joint->matStaticLocal; m_Joint[i].m_StaGlobaclMartic = m_Joint[i].m_StaLocalMartic; } else { m_Joint[i].m_StaGlobaclMartic = MatrixEx.Mult(m_Joint[m_Joint[i].ParentId].m_StaGlobaclMartic , m_Joint[i].m_StaLocalMartic); //需要重写乘法运算符 } } //绑定顶点,初始化顶点的相对坐标 //SetupVertices(); //更新顶点信息 }