本文介绍了使用glMultMatrix围绕固定点旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

围绕(x,y,0)旋转对象://2D

Rotating an object around (x,y,0) : //2D

glTranslatef(-x, -y, 0);
glRotatef(theta, 0.0, 0.0, 1.0);
glTranslatef(x, y, 0);

对吗?!怎么用"glMultMatrix"代替"glTranslatef"和"glRotatef"?!

is it correct?!how about using "glMultMatrix" instead of "glTranslatef" and "glRotatef" ?!

glLoadIdentity();
glMultMatrix(Translation_Matrix);      //glTranslatef(x, y, 0);
glMultMatrix(Rotation_Matrix);         //glRotatef(theta, 0.0, 1.0, 0.0);
glMultMatrix(Neg_Translation_Matrix);  // glTranslatef(-x, -y, 0);

矩阵序列正确吗?!

推荐答案

由于先前的答案都不是完全正确和完整的,因此让我尝试一下:

Since none of the previous answers look entirely correct and complete, let me try:

要理解的关键点是,将变换按指定顺序按相反的顺序应用到您的顶点.您指定的最后一个变换是应用于顶点的第一个变换

The key point to understand is that the transformations are applied to your vertices in reverse order of the order you specify them in. The last transformation you specify is the first one that is applied to your vertices.

这既适用于使用glTranslateglRotate之类的调用,也适用于使用glMultMatrix. glTranslateglRotate是用于构建特定类型的转换的便捷功能,但它们的作用与glMultMatrix相同.

This applies both to using calls like glTranslate and glRotate, and to using glMultMatrix. glTranslate and glRotate are convenience functions to build specific types of transformations, but they operate just like glMultMatrix.

在您的示例中,要绕任意点旋转,您需要首先平移顶点,以使旋转点移至原点,即您要平移(-x,-y,0).然后,您应用旋转.然后将原点平移回旋转点,即平移(x,y,0).

In your example, to rotate around an arbitrary point, you need to first translate your vertices so that the rotation point moves to the origin, i.e. you want to translate by (-x, -y, 0). Then you apply the rotation. Then translate the origin back to the rotation point, which means a translation by (x, y, 0).

现在,由于我们需要以相反的顺序指定这些步骤,因此这意味着您最初的转换集顺序错误.它必须是:

Now, since we need to specify these steps in reverse order, this means that your initial set of transformations is in the wrong order. It needs to be:

glTranslatef(x, y, 0);
glRotatef(theta, 0.0, 0.0, 1.0);
glTranslatef(-x, -y, 0);

使用glMultMatrix的版本看起来正确,因为您已经按此顺序指定了转换.

Your version using glMultMatrix looks correct, since you're already specifying the transformations in this order.

这篇关于使用glMultMatrix围绕固定点旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:11