OpenGL Rotation of an object around a line(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.
推荐答案
glRotate 的 x、y 和 z 参数可以指定任意轴,而不仅仅是 x、y 和 z 轴.要找到穿过您的线的轴,只需减去线的端点即可获得轴向量:如果两个点是 (x1, y1, z1) 和 (x2,y2, z2),你需要的轴是(x2-x1, y2-y1, z2-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).
正如@chris_l 指出的那样,这仅在线条通过原点时才有效.如果不是,首先应用 (-x1, -y1, -z1) 的平移,使线通过原点,然后应用上述旋转,并通过 (x1, y1, z1).
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).
这篇关于OpenGL围绕一条线旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenGL围绕一条线旋转对象
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
