Quaternion LookAt for camera

Posted by Homar on Game Development See other posts from Game Development or by Homar
Published on 2013-11-10T20:22:54Z Indexed on 2013/11/10 22:25 UTC
Read the original article Hit count: 257

Filed under:
|
|
|
|

I am using the following code to rotate entities to look at points.

glm::vec3 forwardVector = glm::normalize(point - position);
float dot = glm::dot(glm::vec3(0.0f, 0.0f, 1.0f), forwardVector);

float rotationAngle = (float)acos(dot);
glm::vec3 rotationAxis = glm::normalize(glm::cross(glm::vec3(0.0f, 0.0f, 1.0f), forwardVector));
rotation = glm::normalize(glm::quat(rotationAxis * rotationAngle));

This works fine for my usual entities. However, when I use this on my Camera entity, I get a black screen. If I flip the subtraction in the first line, so that I take the forward vector to be the direction from the point to my camera's position, then my camera works but naturally my entities rotate to look in the opposite direction of the point.

I compute the transformation matrix for the camera and then take the inverse to be the View Matrix, which I pass to my OpenGL shaders:

glm::mat4 viewMatrix = glm::inverse(
    cameraTransform->GetTransformationMatrix()
);

The orthographic projection matrix is created using glm::ortho.

What's going wrong?

© Game Development or respective owner

Related posts about opengl

Related posts about math