[英]Multiplying a matrix and a vector in GLM (OpenGL)
I have a transformation matrix, m
, and a vector, v
. 我有一个变换矩阵,
m
和一个向量, 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:
我希望我能做到这样的事情:
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中进行此类操作的正确方法是什么?
Just a note to anyone who runs into a similar problem. 只是给遇到类似问题的人留言。 GLM requires all operands to use the same type.
GLM要求所有操作数使用相同的类型。 Don't try multiplying a
dvec4
with a mat4
and expect it to work, you need a vec4
. 不要尝试将
dvec4
与mat4
相乘并期望它能够工作,你需要一个vec4
。
glm::vec4
is represented as a column vector. glm::vec4
表示为列向量。 Therefore, the proper form is: 因此,正确的形式是:
glm::vec4 result = m * v;
(note the order of the operands) (注意操作数的顺序)
Since GLM is designed to mimic GLSL and is designed to work with OpenGL, its matrices are column-major. 由于GLM旨在模仿GLSL并且设计用于OpenGL,因此其矩阵是列专业。 And if you have a column-major matrix, you left-multiply it with the vector.
如果你有一个列主矩阵,你可以用向量乘以它。
Just as you should be doing in GLSL (unless you transposed the matrix on upload). 就像你应该在GLSL中做的那样(除非你在上传时转换矩阵)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.