查找 3D 中 X、Y 和 Z 轴的角度 - OpenGL/C++
我目前正在尝试使用OpenGL(使用SDL)将一个立方体绘制到我在屏幕中左键单击的位置,然后让它指向我在屏幕中右键单击的位置。
我可以使用 gluUnproject 在我想要的位置成功绘制一个立方体 - 这意味着我已经知道我的立方体所在的坐标。
但是我不知道如何计算使我的立方体指向新位置所需的所有角度。
当然,我仍然使用 gluUnproject 来查找右键单击的坐标,但我只知道如何使用 2D 图形绕 Z 轴旋转。
例如之前,如果我想在 2D 中围绕 Z 轴旋转四边形(当然,这将是自上而下的视图,其中 Z 轴仍然“穿过”屏幕),我会这样做:
angle = atan2(mouseCoordsY - quadPosY, mouseCoordsX - quadPosX);
glRotatef(angle*180/PI, 0, 0, 1);
我的问题是,我该如何在 3D 中进行此操作?
- 我需要像上面那样计算每个轴的角度吗?
- 如果是这样,我如何计算绕 X 轴和 Y 轴旋转的角度?
- 如果不是,我应该用什么方法才能达到我想要的结果?
非常感谢任何帮助。
I am currently trying to use OpenGL (With SDL) to draw a cube to the location where I left click in the screen and then get it to point at the position in the screen where I right click.
I can successfully draw a cube at my desired location using gluUnproject - Meaning I already know the coordinates of which my cube is situated.
However I do not know how to calculate all of the angles required to make my cube point at the new location.
Of course I am still using gluUnproject to find the coordinates of my right click, but I only know how to rotate around the Z axis from using 2D graphics.
For example before, if I wanted to rotate a quad around the Z axis (Of course, this would be a top down view where the Z axis is still "going through" the screen) in 2D I would do something like:
angle = atan2(mouseCoordsY - quadPosY, mouseCoordsX - quadPosX);
glRotatef(angle*180/PI, 0, 0, 1);
My question is, how would I go about doing this in 3D?
- Do I need to calculate the angles for each axis as I did above?
- If so how do I calculate the angle for rotation around the X and Y axis?
- If not, what method should I use to achieve my desired results?
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的立方体位于 A = (x0,y0,z0)
如果您的立方体当前正在查看 B=(x1,y1,z1)
并且如果您希望它查看 C=(x2,y2,z2) 那么;
令 v1 为从 A 到 B 的向量
v1 = B - A
,v2 为从 A 到 C 的向量
v2 = C - A
首先对它们进行归一化。
然后计算旋转角度和旋转轴,
您可以调用 glRotate
If your cube is at A = (x0,y0,z0)
If your cube is currently looking at B=(x1,y1,z1)
and if you want it to look at C=(x2,y2,z2) then;
let v1 be the vector from A to B
v1 = B - A
and v2 be the one from A to C
v2 = C - A
First normalize them.
then calculate the rotation angle and the rotation axis as
You can call glRotate with