OpenGL 对象绕线旋转
我正在使用 OpenGL 和 C++ 进行编程。我知道 1 条线(对角线)上有 2 个点,并且希望围绕该对角线旋转对象。我该怎么做呢?我知道如何使用 glrotatef
围绕 x、y 或 z 轴旋转它,但对此不确定。
I am programming in OpenGL and C++. I know 2 points on 1 line (a diagonal line) and wish to rotate an object around that diagonal line. How can I go about doing this? I know how to use glrotatef
to rotate it around the x, y or z axis but am not sure about this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
glrotate 绕轴旋转。一种方法是执行将旋转轴与其中一个坐标轴对齐的变换,执行旋转,然后反转第一步。如果您需要速度,您可以将这些操作组合成一个特殊的转换矩阵,然后一步应用它们。 此处有说明。
glrotate does the rotation about an axis. One method is to perform transformations which align rotation axis with one of coordinate axis, perform the rotation, then reverse the first step. If you need speed you can combine the operations into a special transformation matrix and apply them in one step. There's a description here.
glRotate
的 x、y 和 z 参数可以指定任意轴,而不仅仅是 x、y 和 z 轴。要找到穿过直线的轴,只需减去直线的端点即可得到轴向量:如果两个点是(x1, y1, z1)
和(x2, y2, z2)
,您需要的轴是(x2-x1, y2-y1, z2-z1)
。编辑:正如@chris_l指出的,只有当线穿过原点时,这才有效。如果不是,首先应用
(-x1, -y1, -z1)
平移,使直线穿过原点,然后应用上述旋转,并通过(x1 ,y1,z1)。
The x, y and z parameters to
glRotate
can specify any arbitrary axis, not just the x, y and z axes. To find an axis passing through your line, just subtract the end-points of the line to get an axis vector: if the two points are(x1, y1, z1)
and(x2, y2, z2)
, the axis you need is(x2-x1, y2-y1, z2-z1)
.Edit: As @chris_l pointed out, this works only if the line passes through the origin. If not, first apply a translation of
(-x1, -y1, -z1)
so that the line passes through the origin, then apply the above rotation, and translate it back by(x1, y1, z1)
.嘿,做一些四元数/向量数学怎么样? =) 我已经在 Vector 类上使用小“补丁”来完成此操作:
使用此类,您可以轻松地围绕任何其他向量旋转任何向量:
Hey, how about doing some quaternions / vector maths? =) I've done that using small "patch" on my Vector class:
Using this class you can easily rotate any vector around any other one: