# How to find the “up” direction of the view matrix, with GLM

Using OpenGL and the GLM matrix library, I want to translate my camera relative to the world coordinate system. This requires me to compute the necessary view matrix. To initialise the view matrix, I used:

view_matrix = glm::lookAt(eye, centre, up);


Where eye = (0, 0, 10), centre (0, 0, 0), and up = (0, 1, 0). Suppose I want to now translate the view matrix by 5 unites in the camera's y-direction, i.e. to move upwards relative to the model. I tried:

view_matrix = glm::translate(view_matrix, glm::vec3(0, 5, 0));


This works fine at first, because the view and world coordinate systems are aligned. However, if I then rotate my camera a bit, and then perform this translation, it no longer works. Instead, it moves the camera along the world coordinate system, rather than the camera coordinate system.

So what I need to do, is to find the vector which represents the "up" direction of the camera, and then translate along this vector. This is similar to the inverse of the glm::lookAt(eye, centre, up) function.

In summary: Is it possible to find the "up" vector of the view matrix using the GLM library?

## 2 Answers

When you rotate your camera you should apply the same rotation matrix to your up vector. That should result in an up vector in the same direction as your view matrix's up direction.

The View Matrix is usually built with the "Look At" data, the Eye, Center and Up vectors. And it looks like this (in column major):

| ux vx -nx -eyex |
| uy vy -ny -eyey |
| uz vz -nz -eyez |
| 0  0    0     1 |


Where u, v and n are the normalized vectors for the camera referential. u is the up vector, n is the direction the camera is looking at and v is perpendicular to both n and u.

If you want to get the up vector from the matrix you could get the indices view[0], view[4] and view[8]. If the up vector you provided is not orthogonal then it may change a bit after the cross products, so you may get a slightly different up vector than the one you provided.

You actually want to do the rotation after the translation.

glm::translate( ... )
glm::rotate( ... )


So that the translation happens in the camera coords. If you translate in front, the camera moves in the direction its facing. (Should be z+)

However, if you need to keep track of the current position/rotation of the camera, you should calculate the new Eye and Center vectors yourself instead of applying stand-alone rotations and translations to the camera.