OpenGL变换矩阵顺序是向后的 [英] OpenGL transform matrix order is backwards

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

问题描述

如果我想围绕z轴旋转对象,然后翻译它,我必须做

If I want to rotate the object around z axis, and then translate it I must do

glm::mat4 transform;
GLfloat angle = 90f;
transform = glm::rotate(transform, angle, glm::vec3(0.0f, 0.0f, 1.0f));
transform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f));

但它工作倒退,它先旋转然后翻译,所以我需要写为



But it works backwards, it first rotates and then translates, so I need to write it as

glm::mat4 transform;
GLfloat angle = 90f;
transform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f));
transform = glm::rotate(transform, angle, glm::vec3(0.0f, 0.0f, 1.0f));

这项工作背后的数学如何?为什么我必须反向组合矩阵以实现期望的效果?

How do the maths behind this work? Why I must combine matrixes in reverse to achieve the desired effect?

推荐答案

从直观的角度来看,转换必须以与他们相反的方式应用。这是很容易的:

From an intuitive point of view, you are absolutely right: Transformations have to be applied the opposite way one thinks about them. The reason for this is quite easy:

在glm / OpenGL中,所有的向量假定为列向量,因此应用一个变换( M )以矩阵形式到向量 t 可以写成如下:

In glm/OpenGL all vectors are assumed to be column vectors, thus applying a transformation (M) in matrix form to a vector t can be written as follows:

t' = M * t

现在假设我们首先要翻译code> T )和rotate( R )。现在我们可以单独执行每个步骤,如

Now assume that we first want translate (T) and the rotate (R). We could now do each step separately like

t' = T * t       //Translate
t'' = R * t'     //Rotate result Translation

当我们要合并这两个变换时, c> t'在第二行并获取:

When we want to combine both transformations we substitute t' in the second line and get:

t'' = R * (T * t) = (R * T) * t

正如您所看到的,首先写最后(或更好地说更接近向量)。同样的原理可以应用于许多矩阵。

As you can see, the operation that is applied first is written last (or better to say closer to the vector). The same principle can be applied with as many matrices one wants.

注意,如果向量被视为行向量,整个矩阵顺序将改变。

Note, that if vectors are treated as row vectors, the whole matrix order would change.

 t' = t * M        //General case

让我们看一下上面的例子,但这次使用行向量:

Let's have a look at the same example as above, but this time with row vectors:

 t' = t * T
 t'' = t' * R

 t'' = (t * T) * R = t * (T * R)

结论:无论何时考虑转换和向量,请记住首先应用的操作必须写得更靠近向量。

Conclusion: Whenever you think about transformations and vectors, remember that the operation that is applied first has to be written closer to the vector.

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

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