OpenGL在一个点上旋转相机。

时间:2023-01-30 20:05:13

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.

在OpenGL中,我试着在一个点上旋转一个相机,相机是距离r的距离,当它旋转的时候,它会面对这个点。换句话说,我想让相机沿圆周半径r从中心移动,相机在圆周上的任何一点面向中心。

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

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

I've tried:

我试过了:

// 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);

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!

我的最终结果是相机仍然沿原点旋转,而不是圆心。有人能帮我解决这个问题吗?谢谢!

4 个解决方案

#1


24  

You need to either:

你需要:

  • rotate the camera around the origin and then translate it (*)
  • 旋转相机的原点,然后翻译它(*)

or:

或者:

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

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

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

  • translate(-P)
  • 翻译(- p)
  • rotate
  • 旋转
  • translate(P)
  • 翻译(P)

#2


9  

it is a little confusing, but i think you should:

这有点让人困惑,但我认为你应该:

// move camera a distance r away from the center
glTranslatef(0, 0, -r);

// rotate 
glRotatef(angley, 0, 1, 0);
glRotatef(anglex, 1, 0, 0);

// move to center of circle    
glTranslatef(-cx, -cy, -cz)

note the order must NOT be changed.

注意顺序不能更改。

#3


2  

Why bother with all the trouble of rotating the camera and not rotate the scene itself?
It's much more straight forward. just rotate the modelview matrix around the origin. You'll get the exact same result.

为什么要费那么大力气转动相机而不旋转镜头本身呢?它更直接。只需围绕原点旋转模型视图矩阵。你会得到完全一样的结果。

#4


1  

I find problems like this much easier to solve with gluLookAt(). You define a path for the camera (a circle is easy!) and keep the "center" point fixed (i.e. the thing you're looking at).

我发现这样的问题更容易用gluLookAt()解决。你为相机设定了一条路径(一个圆圈很简单!),并保持“中心”点固定(即你正在看的东西)。

The only possible trick is defining a good up vector--but not usually too much work. If the path and the target point are in the same plane, you can use the same up vector each time!

唯一可能的诀窍是定义一个好的向上向量——但通常不是太多的工作。如果路径和目标点在同一平面,那么每次都可以使用相同的up向量!

#1


24  

You need to either:

你需要:

  • rotate the camera around the origin and then translate it (*)
  • 旋转相机的原点,然后翻译它(*)

or:

或者:

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

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

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

  • translate(-P)
  • 翻译(- p)
  • rotate
  • 旋转
  • translate(P)
  • 翻译(P)

#2


9  

it is a little confusing, but i think you should:

这有点让人困惑,但我认为你应该:

// move camera a distance r away from the center
glTranslatef(0, 0, -r);

// rotate 
glRotatef(angley, 0, 1, 0);
glRotatef(anglex, 1, 0, 0);

// move to center of circle    
glTranslatef(-cx, -cy, -cz)

note the order must NOT be changed.

注意顺序不能更改。

#3


2  

Why bother with all the trouble of rotating the camera and not rotate the scene itself?
It's much more straight forward. just rotate the modelview matrix around the origin. You'll get the exact same result.

为什么要费那么大力气转动相机而不旋转镜头本身呢?它更直接。只需围绕原点旋转模型视图矩阵。你会得到完全一样的结果。

#4


1  

I find problems like this much easier to solve with gluLookAt(). You define a path for the camera (a circle is easy!) and keep the "center" point fixed (i.e. the thing you're looking at).

我发现这样的问题更容易用gluLookAt()解决。你为相机设定了一条路径(一个圆圈很简单!),并保持“中心”点固定(即你正在看的东西)。

The only possible trick is defining a good up vector--but not usually too much work. If the path and the target point are in the same plane, you can use the same up vector each time!

唯一可能的诀窍是定义一个好的向上向量——但通常不是太多的工作。如果路径和目标点在同一平面,那么每次都可以使用相同的up向量!