Need help with change of basis (world to camera frame) I'm reading up on coordinate systems and geometric transformations but the change of basis topic still confuses me. I found this exercise but, honestly, I wouldn't know how to begin:


  Given a world coordinate system in which the camera is at $(3,3,0)$ and pointed at the origin $(0,0,0)$ write an orthonormal set $(e_1, e_2, e_3)$ with an axis oriented like the camera axis and the matrix for the change of coordinates from world frame to camera frame $(O, e_1, e_2, e_3)$.


Do I need to use homogeneous coordinates or not? Currently, I'm using the book Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL. If anyone could suggest a better alternative I'd be grateful.
Since the transformation that is asked for includes a translation (the camera is located at a position other than the origin), you will indeed have to use homogeneous coordinates to describe this transformation in matrix form.

There are basically two ways to get to the matrix you're looking for. You can either set up the matrix that would transform from camera coordinates to world-space and then take the inverse of that, or you can build the matrix directly. The first approach is what is typically done and what the exercise seems to be aiming for since it explicitly asks for the base vectors of the camera system as seen from world-space, which is the first step in that process. The advantage of this approach is that it is arguably a bit more intuitive since what you're essentially doing is treat the camera as just another object that you place in world-space by setting up a matrix. Just that once you have that matrix, you invert it because you didn't actually want to get from camera-local coordinates to world coordinates but rather the other way around. I'll explain both approaches below using the concrete example given in your exercise. At least for now, I'll leave the generalization of the ideas presented below as an exercise to the reader (so that at least some of the fun remains for you ).

Let's look at a sketch of the whole situation:

$$
\def\mvec#1{\begin{pmatrix}#1\end{pmatrix}}
\def\ivec#1{\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}
\def\mmat#1{\begin{bmatrix}#1\end{bmatrix}}
\def\vec#1{\mathrm{\mathbf{#1}}}
\def\mat#1{\mathrm{\mathbf{#1}}}
$$



Your camera is located at $\vec o = \mvec{3 &amp; 3 &amp; 0}^T$ and is supposed to be looking at the origin. By general convention, the camera is set up to be looking along the line of the z-axis of its local coordinate frame, while the local x-axis points right and the local y-axis points up. Therefore, we know that $\vec e_3$ should be pointing either at or away from the origin. Since you're talking about OpenGL, I assumed right-handed world and camera coordinate systems as is typically done in OpenGL-land. To get a right-handed coordinate frame where the local x points to the right, local y points up, and local z lines up with the viewing direction, $\vec e_3$ can only to be pointing in the direction away from the point at which the camera is looking. Once we have fixed $\vec e_3$, we have to define the remaining two base vectors. Note that no matter what we choose for $\vec e_1$ and $\vec e_2$, our camera will be looking at the origin. Our choice of $\vec e_1$ and $\vec e_2$ basically just fixes the rotation of the camera around the viewing direction, which is set by $\vec e_3$. Generally, one choses $\vec e_1$ such that it is orthogonal to both, $\vec e_3$, and a general "up" direction. $\vec e_2$ is then simply computed to be orthogonal to both, $\vec e_1$ and $\vec e_3$.

Approach 1

From the above sketch, it is immediately apparent that the coordinates of $\vec e_3$ have to be
\begin{equation}
  \vec e_3 = \frac{1}{\sqrt{3^2 + 3^2 + 0}} \mvec{3 \\ 3 \\ 0} = \frac{1}{\sqrt{2\cdot3^2}} \mvec{3 \\ 3 \\ 0} = \mvec{\frac{1}{\sqrt2} \\ \frac{1}{\sqrt2} \\ 0}.
\end{equation}
Since our camera is located in the x-y-plane in this example, the base vector $\vec e_2$ is just $\vec e_3$ rotated by $90°$:
\begin{equation}
  \vec e_2 = \mvec{-\frac{1}{\sqrt2} \\ \frac{1}{\sqrt2} \\ 0},
\end{equation}
and $\vec e_1$ just points in the direction opposite of the world-space z-axis:
\begin{equation}
  \vec e_1 = \mvec{0 \\ 0 \\ -1}.
\end{equation}
Using the world-space base vectors of the camera frame, we can write down a matrix that transforms from camera-space to world-space. The camera matrix that transforms from camera to world coordinates is then simply the inverse of that matrix:
\begin{equation}
  \mat M_{cam}^{-1} = \mmat{\vec e_1 &amp; \vec e_2 &amp; \vec e_3 &amp; \vec o \\ 0 &amp; 0 &amp; 0 &amp; 1}
         = \mmat{
    0 &amp; -\frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 3 \\
    0 &amp; \frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 3 \\
    -1 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1}.
\end{equation}
Since the camera frame has an orthonormal base just offset by $\vec o$, this matrix is composed of just the rotation defined by the orthonormal base vectors followed by a translation:
\begin{equation}
  \mat M_{cam}^{-1} = \mat T \mat R
\end{equation}
where
\begin{align}
  \mat R &amp;= \mmat{
    0 &amp; -\frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 \\
    0 &amp; \frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 \\
    -1 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1} &amp; 
  \mat T &amp;= \mmat{
    1 &amp; 0 &amp; 0 &amp; 3 \\
    0 &amp; 1 &amp; 0 &amp; 3 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1}.
\end{align}
You can convince yourself that this is true by simply performing the multiplication. Knowing this, we can compute the camera matrix:
\begin{equation}
  \mat M_{cam} = (\mat T \mat R)^{-1} = \mat R^{-1} \mat T^{-1}.
\end{equation}
Since $\mat R$ is composed of orthonormal base vectors, it is an orthogonal matrix, which means that its inverse is just its transpose. $\mat T$ is just a translation, its inverse is a translation in the opposite direction. Therefore,
\begin{equation}
  \begin{split}
  \mat M_{cam} &amp;= \mmat{
    0 &amp; 0 &amp; -1 &amp; 0 \\
    -\frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 &amp; 0 \\
    \frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1} \cdot \mmat{
    1 &amp; 0 &amp; 0 &amp; -3 \\
    0 &amp; 1 &amp; 0 &amp; -3 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1} \\
    &amp;= \mmat{
    0 &amp; 0 &amp; -1 &amp; 0 \\
    -\frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 &amp; 0 \\
    \frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 &amp; -3\sqrt2 \\
    0 &amp; 0 &amp; 0 &amp; 1}.
  \end{split}
\end{equation}

Approach 2

An alternative to the approach above is to just directly compute the coordinates of the world-space base vectors as seen from the perspective of the camera. Given these coordinates, we can, again, just write down a matrix that applies the corresponding transformation from world to camera space.

Looking at the figure again, we see that the coordinates of the world-space x-axis as seen from camera space are
\begin{equation}
  \vec x' = \mvec{0 \\ -\frac{1}{\sqrt2} \\ \frac{1}{\sqrt2}}.
\end{equation}
Analogously, we find the camera-space coordinates of the world-space y- and z-axes:
\begin{align}
  \vec y' &amp;= \mvec{0 \\ \frac{1}{\sqrt2} \\ \frac{1}{\sqrt2}} &amp; 
  \vec z' &amp;= \mvec{-1 \\ 0 \\ 0}.
\end{align}
Also looking at the figure, we note that the world-space origin as seen from the camera is simply located at a distance of $\sqrt{3^2 + 3^2} = 3\sqrt{2}$ units along the negative $\vec e_3$ axis. Therefore, the camera-space coordinates of the world origin are
\begin{equation}
  \vec o' = \mvec{0 \\ 0 \\ -3\sqrt{2}}.
\end{equation} and we can, once again, write down the matrix
\begin{equation}
  \begin{split}
  \mat M_{cam} &amp;= \mmat{
    0 &amp; 0 &amp; -1 &amp; 0 \\
    -\frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 &amp; 0 \\
    \frac{1}{\sqrt2} &amp; \frac{1}{\sqrt2} &amp; 0 &amp; -3\sqrt2 \\
    0 &amp; 0 &amp; 0 &amp; 1}
  \end{split},
\end{equation}
and sleep well tonight because both ways did indeed lead to the same result…