GLM矩阵乘法是左还是右 我对GLM中的矩阵乘法有一些困惑,用C++ + < /P>
是的,我知道GLM矩阵乘法是左还是右 我对GLM中的矩阵乘法有一些困惑,用C++ + < /P>,c++,matrix-multiplication,glm-math,C++,Matrix Multiplication,Glm Math,是的,我知道 OpenGL使用列主矩阵 行主视图与列主视图完全不同 然而,输出令人困惑。为了清楚起见,我发布了代码 //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) testV
- OpenGL使用列主矩阵
- 行主视图与列主视图完全不同
//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;
//初始化两个空2dim向量和两个空2x2 glm矩阵
mat2测试MAT(0.0f);
mat2 idMat(0.0f);
vec2-testVec(0.0f);
vec2结果(0.0f);
//testVec:(1)
// (-1)
testVec[0]=1.0f;
testVec[1]=-1.0f;
//idMat:(10)
// (0 -1)
idMat[0][0]=1.0f;
idMat[1][1]=-1.0f;
//测试材料:(2 3)
// (4 5)
int blabla=2;
对于(int row=0;row
我检查了定义(*)运算符的库代码(对于mat2*mat2),似乎OpenGL正在执行左乘法,即A乘法B生成(B*A)的结果
而(*)运算符(对于mat2*vec2)是将矩阵的列与向量元素相乘,而不是将矩阵的行与向量元素相乘
问题:
以下是错误的:
testMat[row][col]=blabla代码>
glm对矩阵的[]
运算符返回一列,而不是一行。数学很好,但您正在以转置方式初始化矩阵。这里没有OpenGL代码。您能否向我们展示演示您遇到的问题的最少(但最好是完整的)代码?您使用的是什么矩阵/向量类(它们定义乘法运算符)?glm::mat2和glm::vec2,如第一行中所述。即使(*)运算符来自glmDid,您实际上也检查了向量和矩阵的大小?请您更具体地定义“输出”好吗?您是否使用ostreamglm!=OpenGL。OpenGL是一个受广泛支持且稳定的库。GLM甚至还没有达到1.0版。那么,你的问题真的是关于GLM,而不是OpenGL吗?谢谢你的快速回答。如果我理解正确,testMat[0]将给出第一列,而不是预期的第一行,对吗?@surveyCorps“预期”是什么意思?GLSL的文档对此非常清楚。在C++中,如果我想访问3x3矩阵的第一行的元素,我必须编写矩阵[0 ] [0 ];矩阵[0][1];矩阵[0][2]。但在GLM中,要访问3x3矩阵第一行的元素,我必须写入矩阵[0][0];矩阵[1][0];矩阵[2][0]。这是正确的吗?@测绘团队在C++的数组中没有行或列的概念,这是不正确的。矩阵[n]只是“第n个子数组”。没有数学上的意义,只是一个记忆存储器。非常感谢。