过程
- Viewing变换:按顺序作model(相机位置信息=>空间移动到以相机为原点)、view(相机方向信息=>空间旋转到相机镜头向-z,镜头上方向+y)、projection变换(视角、长宽比、远近平面信息=>视锥变换为$[-1,1]^3$的立方体)。
- projection变换包含perspective peojection变换(挤压frustom到cubiod)、平移变换(平移cubiod中心到原点)、标准化变换(伸缩cubiod到cononical);后两步合称orthographic projection变换。
- Viewing变换即MVP。
- Viewport变换:伸缩cononical到其长宽与screen的分辨率匹配且恢复原深度,平移cubiod到正交投影变换前的cubiod深度且左下角为原点。
- Rasterize光栅化:凸包(有简单做法)、深度插值(前文)、深度测试(后文)、反走样(抗锯齿,有简单做法)……
如何进行深度测试?
循环网格面,先对每个三角形网格面做凸包。
对凸包内所有点测试是否在该三角形内部。
进行深度插值,判断该点是否被其他三角形覆盖(深度大),以选择像素颜色。
在判断过程中维护了一个zbuffer和一个framebuffer。zbuffer生命周期应该在开始渲染三角形之前,初始值为最远;在每帧渲染结束后清除。
变换矩阵

$$
\begin{pmatrix}
1 & 0 & 0 & \cfrac{\mathrm{Width}}{2} \\
0 & 1 & 0 & \cfrac{\mathrm{Height}}{2} \\
0 & 0 & 1 & \cfrac{n+f}{2} \\
0 & 0 & 0 & 1
\end{pmatrix}\begin{pmatrix}
\cfrac{\mathrm{Width}}{2} & 0 & 0 & 0 \\
0 & \cfrac{\mathrm{Height}}{2} & 0 & 0 \\
0 & 0 & \cfrac{n-f}{2} & 0 \\
0 & 0 & 0 & 1
\end{pmatrix}
$$
$$
\begin{pmatrix}
\cfrac{1}{n\tan\cfrac{\theta}{2}} & 0 & 0 & 0 \\
0 & \cfrac{1}{\alpha \times n\tan\cfrac{\theta}{2}} & 0 & 0 \\
0 & 0 & \cfrac{2}{n-f} & 0 \\
0 & 0 & 0 & 1
\end{pmatrix}\begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & -\cfrac{n+f}{2} \\
0 & 0 & 0 & 1
\end{pmatrix}\begin{pmatrix}
n & 0 & 0 & 0 \\
0 & n & 0 & 0 \\
0 & 0 & n+f & -nf \\
0 & 0 & 1 & 0
\end{pmatrix}
$$
$$
\begin{pmatrix}
x_{\hat{g}\times\hat{t}} & x_{\hat{t}} & x_{-\hat{g}} & 0 \\
y_{\hat{g}\times\hat{t}} & y_{\hat{t}} & y_{-\hat{g}} & 0 \\
z_{\hat{g}\times\hat{t}} & z_{\hat{t}} & z_{-\hat{g}} & 0 \\
0 & 0 & 0 & 1
\end{pmatrix}^T\begin{pmatrix}
1 & 0 & 0 & x_{-\vec{e}} \\
0 & 1 & 0 & y_{-\vec{e}} \\
0 & 0 & 1 & z_{-\vec{e}} \\
0 & 0 & 0 & 1
\end{pmatrix}
$$
透视投影矩阵的几何意义
$$
M_{persp}\begin{pmatrix}
x \\
y \\
z \\
1 \\
\end{pmatrix}=\begin{pmatrix}
nx \\
ny \\
(n+f)z-nf \\
z \\
\end{pmatrix}\xlongequal{\text{Homogeneous division}}\begin{pmatrix}
\cfrac{n}{z}x \\
\cfrac{n}{z}y \\
(n+f)-\cfrac{nf}{z} \\
1 \\
\end{pmatrix}
$$
考虑在xOy平面上投影,则相当于对平面空间做伸缩,比例$\dfrac{n}{z}$,越远则压缩越严重。
考虑每个平面变换前后的深度变化,有(实在描述不出):

comment 评论区
star_outline 咱快来抢个沙发吧!