在 OpenGL 中绕 3 个轴旋转对象 [英] Rotate object about 3 axes in OpenGL

查看:33
本文介绍了在 OpenGL 中绕 3 个轴旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过增加轴的旋转角度值来实现围绕 3 个轴的对象旋转,并显示这些轴以使观看者可以预测下一个旋转方向.但旋转几圈后,只按照显示的轴绕Z轴旋转.有没有可能不仔细研究四元数而简单地完成它?

I'm trying to achieve an object rotation about 3 axes by incrementing values of rotation angles for axes, and displaying those axes to make next rotation direction predictable for the viewer. But after a few rotations, only rotation about Z-axis is performed in accord with displayed axis. Is there a chance it can be done simply, without poring over quaternions?

glPushMatrix ();
glRotatef (angleX, 1.0, 0.0, 0.0);
glRotatef (angleY, 0.0, 1.0, 0.0);
glRotatef (angleZ, 0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(80.0, 0.0, 0.0);   //x axis
glVertex3f(-80.0, 0.0, 0.0);
glVertex3f(0.0, 80.0,  0.0);  //y axis
glVertex3f(0.0, -80.0,  0.0);
glVertex3f( 0.0, 0.0, 80.0);  //z axis
glVertex3f( 0.0, 0.0, -80.0);
//here is some code for drawing arrows at axes ends
glEnd();
glPopMatrix();

我有这样的轴:http://img841.imageshack.us/i/arrowsx.jpg/angleX、angleY、angleZ 是在按键时增加的全局变量 - 例如按A"后,angleX 增加 10,我希望 X 轴在此之后不会移动,Y 轴和 Z 轴将围绕 X 轴旋转.但只有当我只更改角度变量之一时才如此.绕多于一个轴旋转后,当我改变时,例如angleX - 图片中的所有轴都会改变它们的位置.

I have axes like those: http://img841.imageshack.us/i/arrowsx.jpg/ angleX, angleY, angleZ are globals incremented on key press - e.g. after pressing 'A' angleX is incremented by 10, and I expect axis X won't move after that and axes Y and Z will rotate about axis X. But it is true only when I change only one of angle variables. After rotation about more then one axis, when I change, e.g. angleX - all axes from picture will change their positions.

推荐答案

您发布的代码基本上将围绕每个轴的所有旋转合并为围绕该轴的单个旋转.正如您所注意到的,只要您所做的只是围绕单个轴进行增量旋转,它就可以很好地工作.从您的更新来看,您似乎还希望按键将旋转应用于显示的图像,而不是像您编码的那样聚合.实现您正在寻找的行为的一种方法是将您应用的每个旋转存储在队列或可调整大小的向量(std::vector,或 std::queue 或其他容器,如果您使用 C++)中.每次用户按下一个键时,将围绕所需轴的固定角度大小的另一个旋转添加到列表中.在播放过程中,按照输入的顺序应用旋转(也许是相反的顺序,我必须多考虑一下).然后,这会将旋转应用于显示的几何图形,而不是规范的(即未旋转的)几何图形.

The code you posted essentially lumps all of your rotations about each axis into a single rotation about that axis. As you note, it works well as long as all you're doing is incremental rotations about a single axis. From your update, it also appears that you want the key strokes to apply rotations to the image as displayed, rather than in aggregate as you have coded. One way to achieve the behavior you're looking for is to store in a queue or resizable vector (std::vector, or std::queue or some other container if you're using C++) each of the rotations that you apply. Each time the user presses a key, add another rotation of the fixed angle size about the desired axis to the list. During playback, apply the rotations in the order they were entered (maybe reverse order, I'll have to give it more thought). This will then apply the rotations to the displayed geometry, rather than to the canonical (i.e. un-rotated) geometry.

使用四元数可能有一种更简单且占用内存更少的方法;这是我真正需要精通的一个领域.不过,请说出我现在所知道的,这是我实现我认为你所追求的行为的方式.

There might be an easier and less memory intensive way to do this using quaternions; that's one area that I really need to get better versed on. Give what I know right now, though, this is the way I'd go about achieving the behavior I think you're after.

这篇关于在 OpenGL 中绕 3 个轴旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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