The perspective 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)
Note that w and h are scaled width and height of the camera. It is fine to use these values because these are only for asp. Therefore, x∗′=ftan(2fovx)=fasptan(2fovy)⟹xnd=x∗′x∗=−aspzecot(2fovy)xe
2. znd
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⎠⎞
Frustum
The perspective projection makes the special frustum with symmetric near and far planes. That is, the line L between the origin of EC and the centers of these planes is exactly the z-axis of EC. However, this frustum can be more generalized for L to be not necessarily the z-axis of EC. Note that L always passes through the origin of EC and the near and far planes are perpendicular to the z-axis of EC. This probably sheared frustum can be also mapped into NDC like the perspective projection matrix is derived. But the main difference is finding the shearing matrix for L to match the z-axis of EC at first. The matrix shearing in parallel to xy-plane, which means z is not changed, looks like ⎝⎛10000100pq100001⎠⎞⎝⎛xyz1⎠⎞=⎝⎛x+pzy+qzz1⎠⎞
The frustum matrix is calculated with the top t, bottom b, left l, and right r values on the near plane. The t is the maximum ye on the near plane, the b is the minimum ye, the l is the minimum xe, and the r is the maximum xe. Since the center of the near plane should be on the z-axis of EC after shearing, ⎝⎛10000100pq100001⎠⎞⎝⎛2r+l2t+b−n1⎠⎞=⎝⎛2r+l−pn2t+b−qn−n1⎠⎞=⎝⎛00−n1⎠⎞
So, p=(r+l)/(2n) and q=(t+b)/(2n). Therefore, the frustum matrix is M⎝⎛100001002nr+l2nt+b100001⎠⎞
where M is the perspective projection derived above. =⎝⎛aspcot(2fovy)0000cot(2fovy)0000−f−nf+n−100−f−n2nf0⎠⎞⎝⎛100001002nr+l2nt+b100001⎠⎞⎝⎛aspcot(2fovy)0000cot(2fovy)00asp 2ncot(2fovy)(r+l)2ncot(2fovy)(t+b)−f−nf+n−100−f−n2nf0⎠⎞
In this case here, asp is (r−l)/(t−b) because asp is not changed before and after shearing. Also, tan(fovy/2) is (t−b)/(2n) because t−b, the height of the near plane, is not changed before and after shearing. Accordingly, cot(2fovy)=t−b2n,aspcot(2fovy)=t−b2nr−lt−b=r−l2n
From this result, it makes the frustum matrix simpler as follows. ⎝⎛r−l2n0000t−b2n00r−lr+lt−bt+b−f−nf+n−100−f−n2nf0⎠⎞
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