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. xnd and ynd
First, observe the relationship between x-axis and z-axis. If moving the origin in EC to (0010)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, zeye=−fy∗⟹y∗=−fzeye
Note that ze is a negative value and −y∗′≤y∗≤y∗′ because y∗ is inside the view volume. Considering that y∗′=ftan(fovy/2) and −1≤ynd≤1, ynd=y∗′y∗=−zecot(2fovy)ye
Similarly, xnd can be also derived. Let asp be the aspect ratio. Then, tan(2fovx)=2fw,tan(2fovy)=2fh⟹asp=hw=tan(2fovy)tan(2fovx)
Looking at the previous results, xnd and ynd have ze in their denominators, which represents non-linearity. So, the derivation of znd 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β
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, 1=α+−fβ,−1=α+−nβ⟹α=f−nf+n,β=f−n2nf
Observing that xnd, ynd, and znd have −ze in their denomiators, the projection matrix M can be derived as follows. M⎝⎛xeyeze1⎠⎞≡⎝⎛aspcot(2fovy)0000cot(2fovy)0000−f−nf+n−100−f−n2nf0⎠⎞⎝⎛xeyeze1⎠⎞
Keep going!Keep going ×2!Give me more!Thank you, thank youFar too kind!Never gonna give me up?Never gonna let me down?Turn around and desert me!You're an addict!Son of a clapper!No wayGo back to work!This is getting out of handUnbelievablePREPOSTEROUSI N S A N I T YFEED ME A STRAY CAT