I came across this tutorial on how to create a movable camera in OpenGL using glm::lookAt(glm::vec3 position, glm::vec3 target, glm::vec3 up). In the tutorial, in order to keep the camera always facing in one direction while moving, the view matrix is created as such: view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);, where cameraPos, cameraFront, and cameraUp are all glm::vec3 type.

What I would like to ask is why does the second argument have to be cameraPos + cameraFront? If the camera position moved to the right without changing cameraFront, wouldn't cameraPos + cameraFront have an effect of rotating to the right as opposed to staying in the same direction (which I think is what should be needed)?


1 Answer 1


The cameraFront vector is defined like this:

glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);

or like this, later in the tutorial:

cameraFront = glm::normalize(front);

In both cases, we see that it is a unit vector, which means a vector of length 1.

In the 'world of 3d' (and 2d), when you see a unit vector, it's generally because it is a direction.

And it is the case here.

When calling

glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);

you instruct glm to create a matrix that will look from cameraPos, at cameraPos + cameraFront, so just in front of the camera. The trick here is that you move both the position and target at the same time, with the same offset, as the second parameter is based on the first parameter. This is why the camera does not 'rotate', it has the effect of 'strafing' in First person shooter games.

As an analogy: take a compass in your right hand, stretch your right arm as far as you can in front of you and make sure it is align with the North.

If you look toward your right hand, you can move around, but if you always see in the same direction.

That's what is happening here: the cameraPos vector is represented by you, moving around, while the cameraFront is represented by your right arm, stretched in front of you, always in the same direction, and always of the same distance.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .