Games101-Cp1-Transformation

最近为了求职重新开始把图形学相关的内容重新系统的学习,先把Games101的内容入门,然后把虎书相关的内容补充。

Transformation

矩阵变换可以对不同坐标系之间进行转换,在这个过程中,主要有MVP三大变换,即模型变换、视口变换、投影变换。即从本地坐标系转换到世界坐标系、世界坐标系转换到观察坐标系、相机的不同投射方式。

矩阵变换-模型变换Modeling

模型变换是最简单基础的变化,把物体所在的本地坐标系转换到世界坐标系,主要包括对物体进行大小、旋转、平移变换。都以矩阵形式表示。

  • 大小变换Scale

\[\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} s & 0 \\ 0 & s \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] \]

等比例缩放

\[\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} s_{x} & 0 \\ 0 & s_{y} \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] \]

不均匀缩放

\[\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} -1 & 0 \\ 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] \]

反射/对称

\[\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} 1 & a \\ 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] \]

切变

  • 旋转变换Rotation

\[\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} cos\theta & -sin\theta \\ sin\theta & cos\theta \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] \]

旋转矩阵 通过特殊点即可推导

旋转矩阵同时是正交矩阵,即\(R_\theta^{T}=R_\theta^{-1}\).

\[R_{-\theta} = \left[ \begin{matrix} cos\theta & sin\theta \\ -sin\theta & cos\theta \end{matrix} \right] =R_\theta^{T} =R_\theta^{-1} \]

  • 平移变换Translate

上面两种变换在二维矩阵中即可完成,并且是线性变换。但平移变换不是线性变换,而是仿射变换(Affine Transform),仿射映射是线性映射和平移变换之和。

\[\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} a & b \\ c & d \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] + \left[ \begin{matrix} t_x \\ t_y \end{matrix} \right] \]

平移

但我们希望在一个矩阵内同时表示线性几何变换和平移变换。于是引入一种统一表达方式——齐次坐标,增加一个维度,这样有利于表达仿射变换的表示。并且只有仿射变换的第三行为\((0, 0, 1)\).
2D点的表达方式是\((x, y, 1)^{T}\),增加的维度不为0取常数时为点,通常为1或w。2D向量的表达方式\((x, y, 0)^{T}\),增加的维度为0时表示为向量。因为向量具有平移不变性,即\((x, y, 0)^{T}=R(x, y, 0)^{T}\).并且满足v+v=v,p-p=v,p+v=p,p+p=p(ps:两点的中点,\((x, y, w)^{T}=(x/w, y/w, 1)^{T}\))。

\[\left[ \begin{matrix} x' \\ y' \\ 1 \end{matrix} \right] = \left[ \begin{matrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ 1 \end{matrix} \right] = \left[ \begin{matrix} x + t_x \\ y + t_y \\ 1 \end{matrix} \right] \]

平移


以此类推我们需要表示三维点和三维向量的时候,需要引入齐次坐标增加到四维。

矩阵变换-视口变换Viewing

定义一个相机由三大部分组成 1.相机位置pos \(e\) 2.相机朝向方向\(\hat{g}\) 3.相机上方向\(\hat{t}\)
当相机与目标之间的相对位置不变时,所投影得到的图像仍然一致。于是假设相机在原点、朝向-Z轴方向和相机上方向为Y轴。为了将相机移到原点对应的视口矩阵,需要对相机先平移后旋转\(M_{view}=R_{view}T_{view}\)。其中相机的旋转矩阵需要从矩阵的逆进行推导,我们能从相机的定义得到相机初始朝向的三个轴\(((\hat{g}\times\hat{t}),\hat{t}, -\hat{g})\),我们需要把相机移动到原点,又因旋转矩阵为正交矩阵。则通过转置便可以得到旋转矩阵。

\[R_{view}^{-1}= \left[ \begin{matrix} 1 & 0 & 0 & x_e\\ 0 & 1 & 0 & y_e\\ 0 & 0 & 1 & z_e\\ 0 & 0 & 0 & 1 \end{matrix} \right] \]

\[R_{view}^{-1}= \left[ \begin{matrix} 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{matrix} \right] \]

\[R_{view}= \left[ \begin{matrix} x_{\hat{g}\times\hat{t}} & y_{\hat{g}\times\hat{t}} & z_{\hat{g}\times\hat{t}} & 0\\ x_{\hat{t}} & y_{\hat{t}} & z_{\hat{t}} & 0\\ x_{-\hat{g}} & y_{-\hat{g}} & z_{-\hat{g}} & 0\\ 0 & 0 & 0 & 1 \end{matrix} \right] \]

矩阵变换-投影变换Projection

投影变换将观察坐标系的点投影到图像上。

正交投影Orthographic Projection

正交投影就是将一个立方体\([l, r]\times[b, t]\times[f, n]\)进行见平移后缩放映射到一个规范正方体Canonical\([-1, 1]^3\).

\[M_{ortho}= \left[ \begin{matrix} \frac{2}{r-l} & 0 & 0 & 0\\ 0 & \frac{2}{t-b} & 0 & 0\\ 0 & 0 & \frac{2}{n-f} & 0\\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} 1 & 0 & 0 & -\frac{r+l}{2}\\ 0 & 1 & 0 & -\frac{t+b}{2}\\ 0 & 0 & 1 & -\frac{n+f}{2}\\ 0 & 0 & 0 & 1 \end{matrix} \right] \]

透视投影Perspective Projection

透视投影与正交投影的不同之处在于原先平行的视线不再平行,符合近大远小的特征。透视矩阵一般分成两步1.将透视投影挤压到正交投影的形式\(M_{Perp->Ortho}\),其中\(n, f\)不变 2.再进行正交投影的步骤\(M_{Ortho}\).则对应透视投影的矩阵为\(M_{Perp}=M_{Ortho}M_{Perp->Ortho}\).

由上图可以推导除第三行对应的投影矩阵

\[M_{Perp->Ortho}= \left[ \begin{matrix} n & 0 & 0 & 0\\ 0 & n & 0 & 0\\ ? & ? & ? & ?\\ 0 & 0 & 1 & 0 \end{matrix} \right] \]

对应剩下z轴的推导,我们可以通过透视投影的原理得到两个结论1.在近平面所有点的z值不变。2.远平面上的点z值也不变。又因z值与x、y值无关,我们假设矩阵第三行\(M_{第三行}=[0 & 0 & A & B]\).

\[M_{Perp->Ortho} \left[ \begin{matrix} x \\ y \\ n \\ 1 \end{matrix} \right] = \left[ \begin{matrix} x \\ y \\ n \\ 1 \end{matrix} \right] = \left[ \begin{matrix} nx \\ ny \\ n^2 \\ n \end{matrix} \right] , \left[ \begin{matrix} 0 & 0 & A & B \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ n \\ 1 \end{matrix} \right] =n^2 \]

近平面推导

\[M_{Perp->Ortho} \left[ \begin{matrix} 0 \\ 0 \\ f \\ 1 \end{matrix} \right] = \left[ \begin{matrix} 0 \\ 0 \\ f^2 \\ f \end{matrix} \right] , \left[ \begin{matrix} 0 & 0 & A & B \end{matrix} \right] \left[ \begin{matrix} 0 \\ 0 \\ f \\ 1 \end{matrix} \right] =f^2 \]

远平面推导:取$(0, 0, f, 1)$

综上,透视投影转正交投影对应的矩阵\(M_{Perp->Ortho}\)如下.

\[M_{Perp->Ortho}= \left[ \begin{matrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & 1 & 0 \\ \end{matrix} \right] \]

最后对应的作业1可能涉及到罗德里格斯公式,就暂时不展开来分析,后续应该在虎书中会提到再补充。
第一节的内容就大致是这些,感谢看到这里,Cheers!

posted @ 2023-03-10 16:39  xkyl  阅读(48)  评论(0编辑  收藏  举报