matMul 函数执行矩阵乘法,将其存储在矩阵 C 中。对于 computeTransformationMatrix 函数,目标是将旋转矩阵、缩放矩阵、平移矩阵和投影矩阵组合成单个变换矩阵 M。所有变换矩阵都是 4x4,除了投影矩阵,它是一个 2X4 矩阵。常量在别处正确定义,尽管下面没有显示。是组合变换以对每个变换矩阵应用一次 matMul 函数(显示在最底部)直到最终创建复合变换矩阵 M 的唯一方法吗?
void matMul(Matrix A, Matrix B, int ARows, int ACols, int BCols, Matrix C){
int i,j,k, sum;
for(i=0;i<ARows;i++){
sum = 0;
for(j=0;j<ACols;j++){
for(k=0;k<BCols;k++){
sum += A[i][k]*B[k][j];
}
C[i][j] = sum;
}
}
}
void computeTransformationMatrix(Matrix M, float scale, float xt, float yt, float zt) {
// to return final transformation in M
Matrix P; // projection matrix
Matrix S; // scaling matrix
Matrix T; // translation matrix
Matrix R_X, R_Y, R_Z; //rotation matrices
// initialize transformation matrices
rotationMatrixX(ROTATION_ANGLE_X, R_X);
rotationMatrixY(ROTATION_ANGLE_Y, R_Y);
rotationMatrixZ(ROTATION_ANGLE_Z, R_Z);
projectionMatrix(P);
scalingMatrix(scale, scale, -scale, S);
translationMatrix(xt, yt, zt, T);
Matrix TM1, TM2, TM3, TM4;//store transformed matrices
matMul(R_X, R_Y, 4, 4, 4, TM1);
matMul(R_Z, TM1, 4, 4, 4, TM2);
matMul(T,TM2, 4, 4, 4, TM3);
matMul(S, TM3, 4, 4, 4, TM4);
matMul(P, TM4, 2, 4, 4, M);
}