光栅化过程(MVP矩阵)

在谈光栅化之前,我们不妨来简单梳理一下如何用相机拍好一张照片,

第一步我们要摆放好物体的位置(Model transformation);

第二步我们要找到一个好的角度去放置相机(View transformation);

第三步就是进行投影变换(View transformation),无论是正交投影(Orthographic projection)还是透视投影(Perspective projection),我们的目的都是要把这个空间的物体转换到单位立方体里面来

然后就是要把单位立方体映射到屏幕空间(Canonical Cube to Screen)

摆放物体位置

Scale物体缩放

$$ S(s_x , s_y , s)z)\begin{pmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_y & 0 \\ 0 & 0 & 0 & 0\end{pmatrix} $$

Translate物体移动

$$ T(t_x , t_y , t_z) = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1\end{pmatrix} $$

齐次坐标系的性质

(1 , 0 ,0 , 1) (2 , 0 , 0, 2) 均表示为(1 , 0 , 0)

三维物体绕x , y , z轴进行旋转 旋转矩阵

$$ R_x = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & cos\alpha & -sin\alpha & 0 \\ 0 & sin\alpha & cos\alpha & 0 \\ 0 & 0 & 0 & 1\\ \end{pmatrix} $$

$$ R_y = \begin{pmatrix} cos\alpha & 0 & sin\alpha & 0 \\ 0 & 1 & 0 & 0 \\ -sin\alpha & 0 & cos\alpha & 0 \\ 0 & 0 & 0 & 1\\ \end{pmatrix} $$

$$ R_z = \begin{pmatrix} cos\alpha & -sin\alpha & 0 & 0 \\ sin\alpha & sin\alpha & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1\\ \end{pmatrix} $$

根据任意旋转角得到以下矩阵

$$ R(n , \alpha) = cos\alpha I + (1 - cos\alpha )nn^T + sin\alpha\begin{pmatrix} 1 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0 \\\end{pmatrix} $$