OpenGL -相机轨道点与四元数

OpenGL - Camera Orbiting a Point with Quaternions

本文关键字:四元数 轨道 相机 OpenGL      更新时间:2023-10-16

所以我目前使用四元数来存储和修改我的OpenGL场景中物体的方向,以及相机的方向。当直接旋转这些对象时(游戏邦注:例如,我想围绕Z轴旋转摄像机Z量,或者我想围绕X轴旋转物体X,然后沿着其局部Z轴平移它T),我没有问题,所以我只能假设我的基本旋转代码是正确的。

然而,我现在正试图实现一个函数,使我的相机在空间中的任意点轨道上运行,而且我有相当困难的时间。这是我到目前为止想出的,它不起作用(这发生在Camera类中)。

    //Get the inverse of the orientation, which should represent the orientation 
    //"from" the focal point to the camera
    Quaternion InverseOrient = m_Orientation;
    InverseOrient.Invert();
    ///Rotation
    //Create change quaternions for each axis
    Quaternion xOffset = Quaternion();
    xOffset.FromAxisAngle(xChange * m_TurnSpeed, 1.0, 0.0, 0.0);
    Quaternion yOffset = Quaternion();
    yOffset.FromAxisAngle(yChange * m_TurnSpeed, 0.0, 1.0, 0.0);
    Quaternion zOffset = Quaternion();
    zOffset.FromAxisAngle(zChange * m_TurnSpeed, 0.0, 0.0, 1.0);
    //Multiply the change quats into the inversed orientation quat
    InverseOrient = yOffset * zOffset * xOffset * InverseOrient;
    //Translate according to the focal distance
    //Start with a vector relative to the position being looked at
    sf::Vector3<float> RelativePos(0, 0, -m_FocalDistance);
    //Rotate according to the quaternion
    RelativePos = InverseOrient.MultVect(RelativePos);
    //Add that relative position to the focal point
    m_Position.x = m_FocalPoint->x + RelativePos.x;
    m_Position.y = m_FocalPoint->y + RelativePos.y;
    m_Position.z = m_FocalPoint->z + RelativePos.z;
    //Now set the orientation to the inverse of the quaternion 
    //used to position the camera
    m_Orientation = InverseOrient;
    m_Orientation.Invert();

最终发生的是相机围绕其他点旋转——当然不是物体,但显然也不是它本身,就好像它在空间中以螺旋路径循环。

所以这显然不是让相机绕点运行的方法,但什么才是呢?

我将首先在球坐标中操作相机,并在必要时转换为四元数。

给定以下假设:

  • 相机没有胶卷
  • 你正在看的点是[x, y, z]
  • 你有偏航,俯仰角
  • [0,1,0]为"up"

下面是如何计算一些重要的值:

  • 视图向量:v = [vx, vy, vz] = [cos(偏航)*cos(俯仰),sin(俯仰),-sin(偏航)*cos(俯仰)]
  • 相机位置:p = [x, y, z] - r*v
  • 右向量:v与[0,1,0]的叉乘
  • 上向量:v与右向量的叉乘
  • 你的视图四元数是[0,vx, vy, vz](这是一个0 w分量的视图向量)

现在在你的模拟中,你可以操作俯仰/偏航,这是非常直观的。如果你想做插值,把前后俯仰+偏航角转换成四元数,然后做四元数球面线性插值。