I've been going over some OpenGL learning resources and one of them is this article: Modern OpenGL 04 - Cameras, Vectors & Input in which the author presents an approach to building a Camera
class that can be used to derive the view and projection matrices.
In the accompanying source code there is a function that returns the orientation of the camera as a transform matrix (permalink to source code) which is implemented like this:
glm::mat4 Camera::orientation() const {
glm::mat4 orientation;
orientation = glm::rotate(orientation, glm::radians(_verticalAngle), glm::vec3(1,0,0));
orientation = glm::rotate(orientation, glm::radians(_horizontalAngle), glm::vec3(0,1,0));
return orientation;
}
If I'd like to translate the camera relative to the direction it is facing then I would assume that I could multiply the orientation matrix by the translation vector to get a new vector that does the translation in the direction the camera is facing.
However, this does not yield the result I was expecting. After looking at how the forward, up and right axis are derived (permalink to source code):
glm::vec3 Camera::forward() const {
glm::vec4 forward = glm::inverse(orientation()) * glm::vec4(0,0,-1,1);
return glm::vec3(forward);
}
glm::vec3 Camera::right() const {
glm::vec4 right = glm::inverse(orientation()) * glm::vec4(1,0,0,1);
return glm::vec3(right);
}
glm::vec3 Camera::up() const {
glm::vec4 up = glm::inverse(orientation()) * glm::vec4(0,1,0,1);
return glm::vec3(up);
}
I can see that I need to multiply the translation vector with the inverse of the orientation matrix to get the expected result but I don't understand why. Isn't the orientation (as used in this article) a rotation matrix? If that's the case then why do I need to inverse it?
orientation
because he calls thisorientation
inCamera::view
and thenCamera::matrix
without transpose or inversion of it. And becauseglfwGetCursorPos
will give you flipped Y coordinates, theorientation
does compute the inverse of orientation coincidentally. However X coordinates are not flipped, which should cause problem but I cannot explain why it does not. $\endgroup$