问题描述
我正在用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绕线旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!