GLM矩阵乘法是左还是右

Is GLM matrix multiplication Left or Right

我对使用 C++ 的 GLM 中的矩阵乘法有些困惑。

是的,我知道

但是,输出令人困惑。为了清楚起见,我发布了代码。

//Initialize two empty 2dim vectors, and two empty 2x2 glm matrices
mat2 testMat (0.0f);
mat2 idMat(0.0f);
vec2 testVec (0.0f);
vec2 resultVec (0.0f);


//testVec: ( 1)
//         (-1)
testVec[0] = 1.0f;
testVec[1] = -1.0f;

// idMat: (1  0)
//        (0 -1)
idMat[0][0] = 1.0f;
idMat[1][1] = -1.0f;

// testMat: (2  3)
//          (4  5)
int blabla = 2;
for (int row = 0; row < testMat[0].length(); row++){
    for (int col = 0; col < testMat[0].length(); col++){
        testMat[row][col] = blabla;
        blabla += 1;
    }
}

//                     REAL RESULT   EXPECTED RESULT
// (2  3)      ( 1)       (-2)             (-1)
// (4  5)  *   (-1)   =   (-2)             (-1)
resultVec = testMat * testVec;


//                        REAL RESULT   EXPECTED RESULT
// (2  3)      (1   0)       ( 2  3)        (2  -3)
// (4  5)  *   (0  -1)   =   (-4 -5)        (4  -5)
mat2 resultMat = testMat * idMat;


//                        REAL RESULT   EXPECTED RESULT
// (2  3)      (1   0)       (2  -3)        ( 2   3)
// (4  5)  *   (0  -1)   =   (4  -5)        (-4  -5)
mat2 result2Mat = idMat * testMat;

我检查了定义 (*) 运算符的库代码(对于 mat2 * mat2),OpenGL 似乎正在执行 左乘法,即 A 乘法 B 是产生 (B * A) 的结果。

而 (*) 运算符(对于 mat2 * vec2)是将矩阵的列与向量元素相乘,而不是将矩阵的行与向量元素相乘。

问题:

  1. (*) 运算符以这种方式运行是因为 OpenGL 矩阵是列优先的吗?

以下是错误的:

testMat[row][col] = blabla;

glm 的 [] 矩阵运算符 returns 一列,而不是一行。数学没问题,但您正在以转置方式初始化矩阵。