矩阵/向量乘法顺序 [英] Matrix / vector multiplication order

查看:276
本文介绍了矩阵/向量乘法顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线阅读了十几篇有关OpenGL中旋转,平移和比例矩阵乘法的正确顺序的文章.但是,既然我自己开始实施它,我就感到非常困惑.

I've read a dozen articles online about the correct order of rotation, translation and scale matrix multiplication in OpenGL. However, now that I started implementing it myself, I came to the point where I'm really confused.

让我们假设在我的代码中,我正在计算转换矩阵,并将其作为一个结果矩阵传递给着色器:

Let's assume that in my code I'm calculating the transformation matrix, and I'm passing it to the shader as a one result matrix:

shader.SetUniform("u_Matrix", scale * rotation * translation);

然后在顶点着色器中,将顶点乘以该矩阵:

And in the vertex shader I'm multiplying the vertices by this matrix:

gl_Position = u_Matrix * vec4(a_Position, 0.0, 1.0);

现在,按此顺序(比例*旋转*平移),我得到的正是我想要的: 旋转对象,然后将其移动到特定点,然后缩放它. 有人可以解释一下为什么这是正确的顺序吗?

Now, in this order (scale * rotation * translation) I'm getting exactly what I want: I rotate the object, then I move it to the specific point, and then I scale it. Can someone explain me why this is the correct order?

我一直认为所有转换都是从向量侧"进行的.

I always thought that all the transformations were applied "from the vector side".

例如,如果我们扩展"乘法:

For example, if we "expand" the multiplication:

gl_Position = scale * rotation * translation * vec4(a_Position, 0.0, 1.0);

然后应首先应用平移,然后再进行旋转和缩放.如果不是平移和旋转的顺序,一切对我来说似乎都不错.如果我们不想围绕某个点旋转,我们应该先旋转然后平移,这里不是这种情况.

then first the translation should be applied, then the rotation and the scale after that. Everything would seem ok to me, if it wasn't for the order of translation and rotation. If we don't want to rotate around a certain point, we should first rotate and then translate, which isn't the case here.

为什么这种转换能按预期进行?

Why does this transformation work as intended?

推荐答案

您的C ++矩阵很可能以行主存储在幕后.这意味着将它们从左到右相乘是一个原点"转换,而从右到左将是一个局部"增量转换.

Your C++ matrices are probably stored as row major under the hood. Which means that multiplying them from left to right is an "origin" transformation, while right to left would be a "local" incremental transformation.

但是OpenGL使用列主排序内存布局(将16个元素数组中的第13、14和15个元素视为转换组件).

OpenGL however uses a column major ordering memory layout (The 13th, 14th and 15th elements in the 16 element array are treated as the translation component).

要在OpenGL中使用行主要矩阵,您可以执行以下两项操作:

To use your row major matrices in OpenGL, there are 2 things you can do:

  1. glUniformMatrix *函数,具有一个参数,可将GL_TRUE传递给转置:

void glUniformMatrix4fv(GLint location, GLsizei count, GL布尔转置 , const GLfloat *value);

这会将它们重新安排为主列.

This will re-arrange them to be column major.

  1. 另一种方法是还原着色器中的操作顺序:

gl_Position = vec4(a_Position, 0.0, 1.0) * u_Matrix;

但是您会发现的大多数GLSL文献都使用本地的从左到右列的主要排序,因此最好还是坚持下去.

But most GLSL literature you will find will use local left to right column major ordering, so it may be better to stick with that.

另一种选择是更改C ++一侧的布局,使它们成为列主(但我个人认为行主要么在那儿处理).

Another alternative is to change the layout on the C++ side to make them column major (but I personally think row major is either to deal with there).

这篇关于矩阵/向量乘法顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆