TinyRenderer项目地址:Better camera – Playing with code
线性变换:将向量与矩阵相乘。特点是变换后的原点(0,0)始终在原点。
2维旋转矩阵
\[
R(\theta) =
\begin{bmatrix}
\cos\theta & -\sin\theta \\
\sin\theta & \cos\theta
\end{bmatrix}
\]
\[ \begin{bmatrix} \cos90° & -\sin90° \\ \sin90° & \cos90° \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
=
\begin{bmatrix}
0 & -1 \\ 1 & 0
\end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
=
\begin{bmatrix}
-y \\ x
\end{bmatrix}
\]
2维缩放矩阵,sx和sy是x,y轴的缩放系数
\[
S(s_x, s_y) =
\begin{bmatrix}
s_x & 0 \\
0 & s_y
\end{bmatrix}
\]
2维剪切矩阵,可以使图形沿一个轴向倾斜
\[
H_y(k) =
\begin{bmatrix}
1 & kx\\
ky & 1
\end{bmatrix}
\]
仿射变换:比线性变换更加通用,更好的处理运动图像。表达式如下,A为线性变换矩阵,b为偏移向量。
\[\vec{T} = A\vec{x} + \vec{b}\]
可以对线性变换进行任意的组合。
\[ \begin{bmatrix} x’ \\ y’ \end{bmatrix} =\underbrace{ \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix}}_{\text{2D平移矩阵}}\]
齐次坐标:为矩阵添加一个维度。
可以看到加了一个维度后,可以看到增加一个维度后,平移这个2D的仿射变换,被表示成了一个3D的线性变换,通过对第三个维度的归一化,即可映射回二维空间。
\[ \begin{bmatrix} x’ \\ y’ \end{bmatrix} =\underbrace{ \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix}}_{\text{2D平移矩阵}}\]
\[
\underbrace{
\begin{bmatrix}
1 & 0 & t_x \\
0 & 1 & t_y \\
0 & 0 & 1
\end{bmatrix}
}_{\text{3D平移矩阵}}
\
\underbrace{
\begin{bmatrix}
x \\
y \\
1
\end{bmatrix}
}_{\text{齐次坐标}}
=
\begin{bmatrix}
(1 \cdot x) + (0 \cdot y) + (t_x \cdot 1) \\
(0 \cdot x) + (1 \cdot y) + (t_y \cdot 1) \\
(0 \cdot x) + (0 \cdot y) + (1 \cdot 1)
\end{bmatrix}
=
\underbrace{
\begin{bmatrix}
x + t_x \\
y + t_y \\
1
\end{bmatrix}
}_{\text{变换后的齐次坐标}}
\]
\[\underbrace{\begin{bmatrix}x\\ y\\ z\end{bmatrix} \quad \mapsto \quad \begin{bmatrix}x/z \\ y/z\end{bmatrix}}_{\text{从3D空间投影回2D平面}}\]
优势:将仿射变换变为线性变换。启用透视投影。
将点绘制到屏幕上:
1.视口变换:将场景缩放到屏幕大小
2.投影变换:对空间透视变形,再正交投影
3.模型视图:将世界帧变换为相机帧
评论(0)
暂无评论