使用 glMultMatrix 围绕固定点旋转对象 [英] Rotating an object around a fixed point using glMultMatrix

查看:46
本文介绍了使用 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 围绕固定点旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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