C++ 在 GLM (OpenGL) 中乘以矩阵和向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7574125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:09:58  来源:igfitidea点击:

Multiplying a matrix and a vector in GLM (OpenGL)

c++glm-math

提问by extropic-engine

I have a transformation matrix, m, and a vector, v. I want to do a linear transformation on the vector using the matrix. I'd expect that I would be able to do something like this:

我有一个变换矩阵m和一个向量v。我想使用矩阵对向量进行线性变换。我希望我能够做这样的事情:

glm::mat4 m(1.0);
glm::vec4 v(1.0);

glm::vec4 result = v * m;

This doesn't seem to work, though. What is the correct way to do this kind of operation in GLM?

不过,这似乎行不通。在 GLM 中进行此类操作的正确方法是什么?

Edit:

编辑:

Just a note to anyone who runs into a similar problem. GLM requires all operands to use the same type. Don't try multiplying a dvec4with a mat4and expect it to work, you need a vec4.

只是对遇到类似问题的任何人的说明。GLM 要求所有操作数使用相同的类型。不要尝试将 advec4与 a相乘mat4并期望它起作用,您需要一个vec4.

回答by andand

glm::vec4is represented as a column vector. Therefore, the proper form is:

glm::vec4表示为列向量。因此,正确的形式是:

glm::vec4 result = m * v;

(note the order of the operands)

(注意操作数的顺序)

回答by Nicol Bolas

Since GLM is designed to mimic GLSL and is designed to work with OpenGL, its matrices are column-major. And if you have a column-major matrix, you left-multiply it with the vector.

由于 GLM 旨在模仿 GLSL 并且旨在与 OpenGL 一起使用,因此其矩阵是列优先的。如果你有一个列主矩阵,你可以将它与向量左乘。

Just as you should be doing in GLSL (unless you transposed the matrix on upload).

就像您在 GLSL 中应该做的一样(除非您在上传时转置了矩阵)。