围绕相机自身的y轴旋转相机

时间:2019-02-27 00:58:05

标签: c++ opengl 3d

我对绕摄像机的y轴旋转感到困惑。我正在使用OpenGL图形库,因此-z进入了屏幕。到目前为止,这是我的代码:

if (KEY_DOWN(F_WindowInfo.KeyStates['Q']))
{
    float Yaw = -1.0f;
    m3 RotationMatrix;
    RotationMatrix.Rc[0][0] = Math_Cosine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[0][2] = Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[1][1] = 1.0f;
    RotationMatrix.Rc[2][0] = -Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[2][2] = Math_Cosine(Math_DegToRad(Yaw));

    F_WindowInfo.Camera->ForwardVector = 
        Math_Normalize(&(RotationMatrix * F_WindowInfo.Camera->ForwardVector));

}
if (KEY_DOWN(F_WindowInfo.KeyStates['E']))
{
    float Yaw = 1.0f;
    m3 RotationMatrix;
    RotationMatrix.Rc[0][0] = Math_Cosine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[0][2] = Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[1][1] = 1.0f;
    RotationMatrix.Rc[2][0] = -Math_Sine(Math_DegToRad(Yaw));
    RotationMatrix.Rc[2][2] = Math_Cosine(Math_DegToRad(Yaw));

    F_WindowInfo.Camera->ForwardVector = 
        Math_Normalize(&(RotationMatrix * (F_WindowInfo.Camera->ForwardVector)));
}
F_WindowInfo.Camera->ViewMatrix = Math_LookAtMatrix(&F_WindowInfo.Camera->Position,
    &(F_WindowInfo.Camera->Position + F_WindowInfo.Camera->ForwardVector), 
    &F_WindowInfo.Camera->UpVector);

和Math_LookAtMatrix()函数:

m4
Math_LookAtMatrix(v3* Eye, v3* Target, v3* Up)
{
    v3 ZAxis = Math_Normalize(&(*Target - *Eye));
    v3 XAxis = Math_Normalize(&Math_CrossProduct(&ZAxis, Up));
    v3 YAxis = Math_CrossProduct(&XAxis, &ZAxis);

    m4 Result;
    Result.Rc[0][0] = XAxis.x;
    Result.Rc[0][1] = YAxis.x;
    Result.Rc[0][2] = ZAxis.x;
    Result.Rc[1][0] = XAxis.y;
    Result.Rc[1][1] = YAxis.y;
    Result.Rc[1][2] = ZAxis.y;
    Result.Rc[2][0] = -XAxis.z;
    Result.Rc[2][1] = -YAxis.z;
    Result.Rc[2][2] = -ZAxis.z;
    Result.Rc[3][0] = -Math_InnerProduct(&XAxis, Eye);
    Result.Rc[3][1] = -Math_InnerProduct(&YAxis, Eye);
    Result.Rc[3][2] = Math_InnerProduct(&ZAxis, Eye);
    Result.Rc[3][3] = 1.0f;
    return Result;
}

请注意,ForwardVector,UpVector和Position均为3维向量,并且 RotationMatrix是一个3x3的矩阵。

ForwardVector从(0.0f,0.0f,-1.0f)开始

upvector为(0.0f,1.0f,0.0f)

我遇到的问题是,我当前正在做的旋转使它看起来像世界在绕着世界y轴​​移动,而不是摄影机仅绕着自己的局部y轴旋转。有人可以让我知道我在做什么错吗?

编辑:我相信我的lookAtMatrix函数是错误的,我从Result.Rc [3] [0]和Result.Rc [3] [1]中删除了-号,并得到了我本来想要的Q和E运动。现在,x轴刚刚向后。

0 个答案:

没有答案