# Derivation of Projection Matrix

The projection matrix transforms points from EC(Eye Coordinates) to Normalized Device Coordinates(NDC). However, since the perspective projection is not an affine transformation and has non-linearity, two steps are needed to derive the projection matrix: move the origin to infinity and then normalize coordinates. The meaning of the variables is shown in the picture above. Note that $n$ and $f$ are the near and far planes of the camera.

### 1. $x_{nd}$ and $y_{nd}$

First, observe the relationship between $x$-axis and $z$-axis. If moving the origin in EC to $(0 \; 0 \; 1 \; 0)^t$, which is the point at infinity, all the projected lines in EC are parallel to $z_*$ as above. Let $(y_*, -f)$ be the intersection between a projected line and $z = -f$ in EC. By similarity, $\begin{aligned} \frac{y_e}{z_e} = \frac{y_*}{-f} \implies y_* = -f \frac{y_e}{z_e} \end{aligned}$

Note that $z_e$ is a negative value and $-y'_* \leq y_* \leq y'_*$ because $y_*$ is inside the view volume. Considering that $y'_* = f \tan (\text{fovy} / 2)$ and $-1 \leq y_{nd} \leq 1$, $\begin{aligned} y_{nd} = \frac{y_*}{y'_*} = \cfrac{\cot \left( \frac{\text{fovy}}{2} \right) y_e}{-z_e} \end{aligned}$

Similarly, $x_{nd}$ can be also derived. Let $\text{asp}$ be the aspect ratio. Then, $\begin{aligned} \tan \left( \frac{\text{fovx}}{2} \right) = \cfrac{w}{2f}, \quad \tan \left( \frac{\text{fovy}}{2} \right) = \cfrac{h}{2f} \implies \text{asp} = \cfrac{w}{h} = \cfrac{\tan \left( \frac{\text{fovx}}{2} \right)}{\tan \left( \frac{\text{fovy}}{2} \right)} \end{aligned}$

Therefore, $\begin{aligned} x'_* = f \tan \left( \frac{\text{fovx}}{2} \right) = f \text{asp} \tan \left( \frac{\text{fovy}}{2} \right) \implies x_{nd} = \frac{x_*}{x'_*} = \cfrac{\cot \left( \frac{\text{fovy}}{2} \right) x_e}{-\text{asp} \; z_e} \end{aligned}$

### 2. $z_{nd}$

Looking at the previous results, $x_{nd}$ and $y_{nd}$ have $z_e$ in their denominators, which represents non-linearity. So, the derivation of $z_{nd}$ is not that simple. **Changing the viewpoint, this non-linearity in Euclidean space can be viewed as the linearity in projective space.** That is, the following form can be derived. $\begin{aligned} z_{nd} = \alpha + \frac{\beta}{z_e} \end{aligned}$

Now, considering that $[-n, -f]$ in EC is mapped to $[-1, 1]$ in NDC and *the direction of $z$-axis is going to be opposite*, $\begin{aligned} 1 = \alpha + \frac{\beta}{-f}, \quad -1 = \alpha + \frac{\beta}{-n} \implies \alpha = \frac{f + n}{f - n}, \quad \beta = \frac{2nf}{f - n} \end{aligned}$

Therefore, $\begin{aligned} z_{nd} = \frac{f + n}{f - n} + \frac{2nf}{(f - n)z_e} = \frac{-\frac{f + n}{f - n} z_e - \frac{2nf}{f - n}}{-z_e} \end{aligned}$

### 3. Projection Matrix

Observing that $x_{nd}$, $y_{nd}$, and $z_{nd}$ have $-z_e$ in their denomiators, the projection matrix $M$ can be derived as follows. $\begin{aligned} M \left(\begin{array}{c} x_e \\ y_e \\ z_e \\ 1 \end{array}\right) \equiv \left(\begin{array}{cccc} \frac{\cot \left( \frac{\text{fovy}}{2} \right)}{\text{asp}} & 0 & 0 & 0 \\ 0 & \cot \left( \frac{\text{fovy}}{2} \right) & 0 & 0 \\ 0 & 0 & -\frac{f+n}{f-n} & -\frac{2nf}{f-n} \\ 0 & 0 & -1 & 0 \end{array}\right) \left(\begin{array}{c} x_e \\ y_e \\ z_e \\ 1 \end{array}\right) \end{aligned}$