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)
.
相关文章