Camera

We will use a very simple camera that only requires three vectors to define: what we are looking at, which way is up, and the camera location. Given these vectors, we can compute the basis of the camera. We will define \(\mathbf{g}\) as the look vector, \(\mathbf{t}\) as the up vector, and \(\mathbf{e}\) as the position vector. The basis vectors will continue to be \(\mathbf{u}\) \(\mathbf{v}\), \(\mathbf{w}\).

If we begin by saying that the camera is oriented so that the positive x-axis goes right and the positive y-axis goes up, then we are looking down the negative z-axis. So, the look vector will point down negative z. From this we can construct the camera basis: $$ \mathbf{w} = -\mathbf{g} $$ $$ \mathbf{u} = \mathbf{up} \times \mathbf{w} $$ $$ \mathbf{v} = \mathbf{w} \times \mathbf{u} $$ If you make sure the look and up vector are normalized before building the basis, then the resulting camera basis \( \mathbf{C} \) is orthonormal: $$ \mathbf{C} = \begin{bmatrix} u_x & w_x & v_x \\ u_y & w_y & v_y \\ u_z & w_z & v_z \\ \end{bmatrix} $$

OpenGL doesn't support moving cameras. The OpenGL camera is always at the origin and always looks down -z with +y being up. Since we want to have cameras anywhere in the world, we need build our camera matrix, then use it to move our scene to the OpenGL camera view. An inverse camera basis matrix can do this.

The first step is to translate to the OpenGL origin. Given the camera position \( \mathbf{e} \), this is just \( -\mathbf{e} \). Now all our world objects are near the OpenGL origin. But they still have incorrect orientation.

To adjust the orientation, we use the inverse camera basis. Since the camera basis is: $$ \mathbf{C} = \begin{bmatrix} u_x & w_x & v_x \\ u_y & w_y & v_y \\ u_z & w_z & v_z \\ \end{bmatrix} $$ Since the basis is orthonormal, the inverse is the transpose: $$ \mathbf{C}^{-1} = \mathbf{C}^{T} = \begin{bmatrix} u_z & u_y & u_x \\ v_z & v_y & v_x \\ w_z & w_y & w_x \\ \end{bmatrix} $$ The final inverse camera matrix includes the translation and the inverse basis: $$ \mathbf{C}^{-1} = \begin{bmatrix} u_z & u_y & u_x & 0 \\ v_z & v_y & v_x & 0 \\ w_z & w_y & w_x & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & -e_x \\ 0 & 1 & 0 & -e_y \\ 0 & 0 & 1 & -e_z \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} $$