# Derivation of the Orthographic Projection Matrix

The orthographic projection matrix takes a rectangular prism with corners at (l, t, -n) and (r, b, -f) (with l being left, t being top, n being near, r being right, b being bottom, and f being far) and maps it to a cube with the coordinates of the same corners being (-1, 1, 1) and (1, -1, -1)

Achieving this is going to take a scale and a translate on each axis. To solve for the scale in x and translation in x we have the system of equations:

So we have that $2 = (r s_x + m_x) - (l s_x + m_x) = (r - l)s_x \Rightarrow s_x = \frac{2}{r-l}$ then we have that $-l s_x - m_x = r s_x + m_x \Rightarrow -l s_x - r s_x = 2 m_x$$\Rightarrow m_x = \frac{-s_x (r + l)}{2} = \frac{-(r + l)}{r - l} = \frac{r+l}{l-r}$

With exactly the same algebra we can obtain $s_y = \frac{2}{t-b}$ and $m_y = \frac{t+b}{b-t}$

Then for the z-axis we have the system of equations:

which, with the same algebra as before, leads to the solutions $s_z = \frac{2}{f-n}$ and $m_z = \frac{f + n}{f - n}$

So putting all of that together we get the final matrix: