>[!note]
> *Not* an [[../../../02 Areas/Math/Affine Transformations|Affine Transformation]].
> Relies on [[Homogeneous Equivalence]]
>[!example]-
![[Pasted image 20231116114629.png]]
A perspective projection is a form of [[../Math/Function|Transformation]] from $\R^3$ to a screen ([[Perspective Projection|Viewport Rectangle]]) in $\R^2$ / a projection from [[View Space|Camera Space]] onto [[Viewport]]
![[../../00 Excalidraw/Perspective Projection .excalidraw.dark.svg]]
%%[[../../00 Excalidraw/Perspective Projection .excalidraw.md|🖋 Edit in Excalidraw]], and the [[../../00 Excalidraw/Perspective Projection .excalidraw.light.svg|light exported image]]%%
### Formula
>[!tip] Note: Variables used are described in [[View Volume#Variables Required]]
The given formula for [[Perspective Projection]] is *not* an [[../Math/Affine Transformations|Affine Transformation]] as it relies on dividing the output point by its $w$ component ([[../Math/Homogeneous Equivalence|Homogeneous Equivalence]] / [[Clip Coordinates]]).
$\huge
\begin{align}
P &= \pa{P_{x},P_{y},P_{z}}\\
P' &= \left( -\frac{DP_{x}}{P_{z}}, -\frac{DP_{y}}{P_{z}}, -D \right)
\end{align}
$
We can represent this using a matrix by viewing this by leveraging [[../Math/Homogeneous Equivalence|Homogeneous Equivalence]], eg. applying the following matrix and then following the convention of dividing the outputted [[../Math/Point|Point]] by its $w$ component.
$\begin{align}
\tilde{P'} &= \mat{ -\frac{DP_{x}}{P_{z}}\\ -\frac{DP_{y}}{P_{z}} \\ -D \\ 1 }\\
&= -\frac{D}{P_{z}}\mat{P_{x} \\ P_{y}\\ P_{z}\\ -\frac{P_{z}}{D}}\\
\tilde{P'} &=
\mat{1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0& -\frac{1}{D}&0}\tilde{P}
\end{align}$
$\huge
\therefore \mathcal{P}_{o} = \mat{
1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0& -\frac{1}{D}&D
}
$
This represents a [[Canonical Camera]] [[Perspective Projection]], barring the required division by $w$.
$\huge
\Delta_{0}= \mat{
\frac{2}{w}&0&0&0\\
0& \frac{2}{h} &0&0\\
0&0&1&D\\
0&0&0&1
} $
We can them get the following matrix that maps the [[View Frustum]] in [[View Space|Camera Space]] onto the [[Standard Square]].
$\huge
\begin{align}
\Delta_{0} \circ \mathcal P_{0} &= \mat{
\frac{2}{w}&0&0&0\\
0& \frac{2}{H} &0&0 \\
0&0&0&0\\
0&0& -\frac{1}{D}&0
}
\end{align}
$
>[!tip]
>Any positive non-zero [[../Math/Matrix Arithmetic|multiple]] of this [[../Math/Matrix|Matrix]] represents the exact same [[Perspective Projection]] due to [[../Math/Homogeneous Equivalence|Homogeneous Equivalence]].
>>[!success]- Proof
>>$ \begin{align}
>>\mat{
>>\frac{2\beta}{w} &0&0&0\\
>>0& \frac{2\beta}{h} &0 &0 \\
>>0&0&0&0\\
>>0&0& -\frac{\beta}{D}&0\\
>>}\mat{
>>x\\y\\z\\1
>>}
>>&=
>>\mat{
>>\frac{2\beta x}{w}\\
>>\frac{2\beta y}{w}\\
>>0\\
>>-\frac{\beta z}{D}
>>}
>>\mapsto
>>-\frac{D}{\beta z}
>>\mat{
>>\frac{2\beta x}{w}\\
>>\frac{2\beta y}{w}\\
>>0\\
>>-\frac{\beta z}{D}
>>} =
>>
>>\mat{
>>-\frac{2Dx}{zw}\\
>>-\frac{2Dy}{zh}\\
>>0\\
>>1
>>}
>>\end{align}
>>$
For the partial [[../Math/Function|Transformation]] from [[Object Space]] to [[Device Space]]:
$\huge
\begin{align}
\Pi_{0} &= D \circ \Delta_{0} \circ \mathcal P_{0}\\
&=
\mat{
\frac{2D}{w} &0&0&0\\
0& \frac{2D}{h}&0&0\\
0&0&0&0\\
0&0&-1&0
}\\
&= \mat{
\cot\left( \frac{\theta}{2} \right) &0&0&0\\
0& \frac{w}{h} \cot\pa{ \frac{\theta}{2}} &0&0\\
0&0&0&0\\
0&0&-1&0
}
\end{align}
$
Where $\theta$ is the [[Field of View]] of the [[View Frustum|Camera]].
>[!note]
>$\Pi_{0}$ loses depth information, meaning that it loses information that is needed to draw a 3D scene correctly (eg. objects obscuring others)
![[../../00 Excalidraw/Perspective Projection _2.excalidraw.dark.svg]]
%%[[../../00 Excalidraw/Perspective Projection _2.excalidraw.md|🖋 Edit in Excalidraw]], and the [[../../00 Excalidraw/Perspective Projection _2.excalidraw.light.svg|light exported image]]%%
$\huge
\begin{align}
\tan\pa{ \frac{\theta}{2} } &= \frac{w}{2D} \\
\alpha = \frac{w}{h} &\implies \frac{h}{2D} = \frac{w}{2\alpha D} =
\frac{1}{\alpha} \cdot \frac{w}{2D}
\end{align}
$
To adhere to the OpenGL convention and preserve distance, the [[Perspective Projection]] [[../Math/Matrix|Matrix]] can be changed from mapping to the [[Standard Square]] to the [[Standard Cube]].
$\huge
\begin{align}
\Pi &= \mat{
\frac{2D}{W} &0&0&0\\
0& \frac{2D}{H}&0&0\\
0&0& \frac{{N+F}}{N-F} & \frac{2NF}{N-F}\\
0&0&-1&0
}\\
&= \mat{
\cot\left( \frac{\theta}{2} \right) &0&0&0\\
0& \frac{w}{h} \cot\pa{ \frac{\theta}{2}} &0&0\\
0&0& \frac{{N+F}}{N-F} & \frac{2NF}{N-F}\\
0&0&-1&0
}
\end{align}
$