【问题标题】:Rotate a camera around a axis?绕轴旋转相机?
【发布时间】:2016-05-07 19:03:32
【问题描述】:

如何沿轴旋转相机?我必须乘以什么矩阵?

我正在使用 glm::lookAt 构造 viewMatrix,但我试图将它乘以旋转矩阵,但什么也没发生。

glm::mat4 GetViewMatrix()
{
    return glm::lookAt(this->Position, this->Position + this->Front, glm::vec3(0.0f, 5.0f, 0.0f));
}

glm::mat4 ProjectionMatrix = glm::perspective(actual_camera->Zoom, (float)g_nWidth / (float)g_nHeight, 0.1f, 1000.0f);
glm::mat4 ViewMatrix = actual_camera->GetViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

【问题讨论】:

    标签: c++ opengl


    【解决方案1】:

    使用glm::rotate 旋转相机的前向和向上矢量:

    glm::mat4 GetViewMatrix()
    {
        auto front = glm::rotate(this->Front, angle, axis);
        auto up    = glm::rotate(glm::vec3(0, 1, 0), angle, axis);
        return glm::lookAt(this->Position, this->Position + front, up);
    }
    

    或者,您可以在 MVP 构造中添加与旋转矩阵的乘法:

    glm::mat4 MVP = ProjectionMatrix * glm::transpose(Rotation) * ViewMatrix * ModelMatrix;
    

    旋转发生在视图矩阵之后很重要,因此所有对象都将相对于相机的位置进行旋转。此外,您必须使用 transpose(Rotation) (旋转矩阵的逆是它的转置),因为例如顺时针旋转相机,就相当于逆时针旋转所有对象。

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2012-07-23
      • 2016-09-22
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多