围绕一个轴旋转摄像机?

3

如何在一个轴上旋转相机?我需要乘以哪个矩阵?

我正在使用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;
1个回答

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)(旋转矩阵的逆矩阵是其转置),因为例如顺时针旋转相机等效于逆时针旋转所有对象。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接