3
votes

How can I rotate a camera in a axis? What matrix I have to multiply?

I am using glm::lookAt to construct the viewMatrix, but I tried to multiply it by a rotation matrix and nothing happened.

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 Answers

1
votes

Rotate the front and up vectors of your camera using 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);
}

Alternatively, you can add a multiplication with your rotation matrix to your MVP construction:

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

It is important that the rotation happens after the view matrix, so all objects will be rotated relative to the camera's position. Furthermore, you have to use transpose(Rotation) (the inverse of a rotation matrix is its transpose), since rotating the camera clockwise for example, is equivalent to rotating all objects counter-clockwise.