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 nn and ff are the near and far planes of the camera.

1. xndx_{nd} and yndy_{nd}

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

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


Similarly, xndx_{nd} can be also derived. Let asp\text{asp} be the aspect ratio. Then, tan(fovx2)=w2f,tan(fovy2)=h2f    asp=wh=tan(fovx2)tan(fovy2)\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, x=ftan(fovx2)=fasptan(fovy2)    xnd=xx=cot(fovy2)xeasp  ze\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. zndz_{nd}

Looking at the previous results, xndx_{nd} and yndy_{nd} have zez_e in their denominators, which represents non-linearity. So, the derivation of zndz_{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. znd=α+βze\begin{aligned} z_{nd} = \alpha + \frac{\beta}{z_e} \end{aligned}

Now, considering that [n,f][-n, -f] in EC is mapped to [1,1][-1, 1] in NDC and the direction of zz-axis is going to be opposite, 1=α+βf,1=α+βn    α=f+nfn,β=2nffn\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, znd=f+nfn+2nf(fn)ze=f+nfnze2nffnze\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 xndx_{nd}, yndy_{nd}, and zndz_{nd} have ze-z_e in their denomiators, the projection matrix MM can be derived as follows. M(xeyeze1)(cot(fovy2)asp0000cot(fovy2)0000f+nfn2nffn0010)(xeyeze1)\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}


[1] 3D Graphics Programming Using OpenGL: Introduction

© 2024. All rights reserved.