OpenGL围绕一个点旋转相机 [英] OpenGL rotating a camera around a point

查看:49
本文介绍了OpenGL围绕一个点旋转相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 OpenGL 中,我试图围绕一个点旋转相机,相机与该点的距离为 r,并在旋转时面向该点.换句话说,我希望相机沿着距离中心半径为 r 的圆的圆周移动,相机在圆周上的任何一点都面向中心.

In OpenGL I'm trying to rotate a camera around a point, with camera being distance r from the point and facing the point when it rotates. In other words, I want the camera to move along the circumference of a circle at a radius r from the center, with the camera facing the center at any point along the circumference.

假设在 3d 空间中圆的中心是 (3, 0, 3);

Lets say that in 3d space the center of the circle is (3, 0, 3);

我试过了:

// move to center of circle    
glTranslatef(-3, 0, -3)
// move a distance away from the circle
glTranslatef(0, 0, r);
// rotate along the y "up" axis
glRotatef(CameraAngle, 0, 1, 0);

其中 CameraAngle 是围绕圆移动的度数.

where CameraAngle is the degrees being moved around the circle.

我的最终结果是相机仍在沿原点旋转,而不是圆心.谁能帮我解决这个问题?谢谢!

My end result is the camera is still rotating along the origin, not the center of the circle. Can anyone help me fix this problem? Thanks!

推荐答案

您需要:

  • 围绕原点旋转相机,然后将其平移 (*)

或:

  • 使用 gluLookAt 保持相机指向圆心
  • use gluLookAt to keep the camera pointing at the center of the circle

(*) 旋转函数通常绕原点旋转.要围绕另一个点 P 旋转,您必须:

(*) rotation functions normally rotate about the origin. To rotate around another point P you have to:

  • 翻译(-P)
  • 旋转
  • 翻译(P)

这篇关于OpenGL围绕一个点旋转相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆