<p>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.</p>

<p>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 ).</p>

<p>Let's look at a sketch of the whole situation:</p>

<p><span class="math-container">$$
\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}}}
$$</span></p>

<p><img src="https://i.sstatic.net/NnAhM.png" width="320" /></p>

<p>Your camera is located at <span class="math-container">$\vec o = \mvec{3 &amp; 3 &amp; 0}^T$</span> 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 <span class="math-container">$\vec e_3$</span> 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, <span class="math-container">$\vec e_3$</span> can only to be pointing in the direction away from the point at which the camera is looking. Once we have fixed <span class="math-container">$\vec e_3$</span>, we have to define the remaining two base vectors. Note that no matter what we choose for <span class="math-container">$\vec e_1$</span> and <span class="math-container">$\vec e_2$</span>, our camera will be looking at the origin. Our choice of <span class="math-container">$\vec e_1$</span> and <span class="math-container">$\vec e_2$</span> basically just fixes the rotation of the camera around the viewing direction, which is set by <span class="math-container">$\vec e_3$</span>. Generally, one choses <span class="math-container">$\vec e_1$</span> such that it is orthogonal to both, <span class="math-container">$\vec e_3$</span>, and a general "up" direction. <span class="math-container">$\vec e_2$</span> is then simply computed to be orthogonal to both, <span class="math-container">$\vec e_1$</span> and <span class="math-container">$\vec e_3$</span>.</p>

<h2>Approach 1</h2>

<p>From the above sketch, it is immediately apparent that the coordinates of <span class="math-container">$\vec e_3$</span> have to be
<span class="math-container">\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}</span>
Since our camera is located in the x-y-plane in this example, the base vector <span class="math-container">$\vec e_2$</span> is just <span class="math-container">$\vec e_3$</span> rotated by <span class="math-container">$90°$</span>:
<span class="math-container">\begin{equation}
  \vec e_2 = \mvec{-\frac{1}{\sqrt2} \\ \frac{1}{\sqrt2} \\ 0},
\end{equation}</span>
and <span class="math-container">$\vec e_1$</span> just points in the direction opposite of the world-space z-axis:
<span class="math-container">\begin{equation}
  \vec e_1 = \mvec{0 \\ 0 \\ -1}.
\end{equation}</span>
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:
<span class="math-container">\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}</span>
Since the camera frame has an <a href="https://en.wikipedia.org/wiki/Orthonormality" rel="nofollow noreferrer">orthonormal base</a> just offset by <span class="math-container">$\vec o$</span>, this matrix is composed of just the rotation defined by the orthonormal base vectors followed by a translation:
<span class="math-container">\begin{equation}
  \mat M_{cam}^{-1} = \mat T \mat R
\end{equation}</span>
where
<span class="math-container">\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}</span>
You can convince yourself that this is true by simply performing the multiplication. Knowing this, we can compute the camera matrix:
<span class="math-container">\begin{equation}
  \mat M_{cam} = (\mat T \mat R)^{-1} = \mat R^{-1} \mat T^{-1}.
\end{equation}</span>
Since <span class="math-container">$\mat R$</span> is composed of orthonormal base vectors, it is an orthogonal matrix, which means that its inverse is just its transpose. <span class="math-container">$\mat T$</span> is just a translation, its inverse is a translation in the opposite direction. Therefore,
<span class="math-container">\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}</span></p>

<h2>Approach 2</h2>

<p>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.</p>

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

CC BY-SA 4.0
2019-01-08T01:59:05.143Z
Thank you very much. I understood the part about setting up the matrix but I have some doubts about the initial steps. What does the fact that the camera is looking at the origin actually implies? Why did you start with e_3 and concluded that e_1 had to point in the opposite direction of the world-space z-axis? (sorry to be a pain)
JohnDoe
CC BY-SA 4.0
2019-01-14T08:35:47.200Z
@JohnDoe sorry, totally overlooked your comment here. I updated my answer to give some more detail on how $\vec e_1$, $\vec e_2$, and $\vec e_3$ are chosen.
Michael Kenzel
CC BY-SA 4.0
2019-01-07T14:57:56.453Z
2019-01-14T08:57:39.057Z
2019-01-14T08:57:39.057Z
Michael Kenzel
<p>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:</p>

<blockquote>
  <p>Given a world coordinate system in which the camera is at <span class="math-container">$(3,3,0)$</span> and pointed at the origin <span class="math-container">$(0,0,0)$</span> write an orthonormal set <span class="math-container">$(e_1, e_2, e_3)$</span> with an axis oriented like the camera axis and the matrix for the change of coordinates from world frame to camera frame <span class="math-container">$(O, e_1, e_2, e_3)$</span>.</p>
</blockquote>

<p>Do I need to use homogeneous coordinates or not? Currently, I'm using the book <a href="https://www.pearson.com/us/higher-education/product/Angel-Interactive-Computer-Graphics-A-Top-Down-Approach-with-Shader-Based-Open-GL-6th-Edition/9780132545235.html" rel="nofollow noreferrer"><em>Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL</em></a>. If anyone could suggest a better alternative I'd be grateful. </p>

CC BY-SA 4.0
2019-01-07T11:37:52.920Z
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. I'm not sure how much help you want beyond this simple hint. I'll happily explain in more detail below, but I wouldn't wanna take away from you the opportunity to find an answer yourself…
Michael Kenzel
CC BY-SA 4.0
2019-01-07T11:48:30.883Z
@MichaelKenzel Thank you. I'd appreciate if you could offer an explanation, as right now I have no clue and maybe I could use this as a reference to further my study on the subject.
JohnDoe
<transformations><projections>
Need help with change of basis (world to camera frame)
JohnDoe
# Need help with change of basis (world to camera frame)

(Asked by: username_0 on 2019-01-06)

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*](https://www.pearson.com/us/higher-education/product/Angel-Interactive-Computer-Graphics-A-Top-Down-Approach-with-Shader-Based-Open-GL-6th-Edition/9780132545235.html). If anyone could suggest a better alternative I'd be grateful.

**Tags:** transformations, projections

> **Comments:**
> **username_1**: 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. I'm not sure how much help you want beyond this simple hint. I'll happily explain in more detail below, but I wouldn't wanna take away from you the opportunity to find an answer yourself…  
> **username_0**: @username_1 Thank you. I'd appreciate if you could offer an explanation, as right now I have no clue and maybe I could use this as a reference to further my study on the subject.  

---

## 1 answers

### Answer by username_1 on 2019-01-07. Score: 5 | Accepted answer

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}}}
$$

![](https://i.sstatic.net/NnAhM.png)

Your camera is located at $\vec o = \mvec{3 & 3 & 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 & \vec e\_2 & \vec e\_3 & \vec o \\ 0 & 0 & 0 & 1}
= \mmat{
0 & -\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 3 \\
0 & \frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 3 \\
-1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1}.
\end{equation}
Since the camera frame has an [orthonormal base](https://en.wikipedia.org/wiki/Orthonormality) 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 &= \mmat{
0 & -\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 \\
0 & \frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 \\
-1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1} &
\mat T &= \mmat{
1 & 0 & 0 & 3 \\
0 & 1 & 0 & 3 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 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} &= \mmat{
0 & 0 & -1 & 0 \\
-\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 & 0 \\
\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 & 0 \\
0 & 0 & 0 & 1} \cdot \mmat{
1 & 0 & 0 & -3 \\
0 & 1 & 0 & -3 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1} \\
&= \mmat{
0 & 0 & -1 & 0 \\
-\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 & 0 \\
\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 & -3\sqrt2 \\
0 & 0 & 0 & 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' &= \mvec{0 \\ \frac{1}{\sqrt2} \\ \frac{1}{\sqrt2}} &
\vec z' &= \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} &= \mmat{
0 & 0 & -1 & 0 \\
-\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 & 0 \\
\frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 & -3\sqrt2 \\
0 & 0 & 0 & 1}
\end{split},
\end{equation}
and sleep well tonight because both ways did indeed lead to the same result…

> **Comments:**
> **username_0**: Thank you very much. I understood the part about setting up the matrix but I have some doubts about the initial steps. What does the fact that the camera is looking at the origin actually implies? Why did you start with e_3 and concluded that e_1 had to point in the opposite direction of the world-space z-axis? (sorry to be a pain)  
> **username_1**: @username_0 sorry, totally overlooked your comment here. I updated my answer to give some more detail on how $\vec e_1$, $\vec e_2$, and $\vec e_3$ are chosen.  

---

