OpenGL - 使用四元数轨道运行的相机

时间:2013-01-11 12:10:39

标签: c++ opengl camera rotation quaternions

所以我目前使用四元数来存储和修改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();

最终发生的事情是相机围绕其他点旋转 - 当然不是物体,但显然也不是自己,就好像它是在螺旋路径中穿过空间一样。

所以这显然不是围绕一个点围绕相机轨道运行的方法,但是什么呢?

1 个答案:

答案 0 :(得分:2)

我会首先在球面坐标上对相机进行操作,并根据需要转换为四元数。

考虑以下假设:

  • 相机没有滚动
  • 您要看的是[x,y,z]
  • 你有偏航,俯仰角
  • [0,1,0]是&#34; up&#34;

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

  • 视图矢量:v = [vx,vy,vz] = [cos(yaw)* cos(pitch),sin(pitch), - sin(yaw)* cos(pitch)]
  • 摄像机位置:p = [x,y,z] - r * v
  • 右矢量:交叉乘积v与[0,1,0]
  • 向上矢量:使用右矢量交叉乘积v
  • 您的视图四元数为[0,vx,vy,vz](具有0 w分量的视图向量)

现在,在您的模拟中,您可以在俯仰/偏航上操作,这非常直观。如果你想进行插值,将前后音高+偏航转换成四元数并进行四元数球面线性插值。