GAMES101笔记 Lecture 03&04 Transformation


为什么要变换?

  • Modeling:相机位置、刚体的旋转、尺度缩放
  • Viewing:从三维到二维的投影变换。

Lecture 03:二维与三维变换

二维线性变换

推导思路:找变化前后相同点之间位置变化的关系

  • 缩放矩阵(Scaling): [ x ′ y ′ ] = [ s x 0 0 s y ] [ x y ] \begin{bmatrix} x^\prime \\ y^\prime\end{bmatrix} =\begin{bmatrix}s_x & 0 \\ 0 & s_y \end{bmatrix} \begin{bmatrix} x \\ y\end{bmatrix} [xy]=[sx00sy][xy]
  • 翻转矩阵(Reflection): [ x ′ y ′ ] = [ − 1 0 0 1 ] [ x y ] \begin{bmatrix} x^\prime \\ y^\prime\end{bmatrix} =\begin{bmatrix}-1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y\end{bmatrix} [xy]=[1001][xy] (按y轴翻转)
  • 切变矩阵(Shear): [ x ′ y ′ ] = [ 1 a 0 1 ] [ x y ] \begin{bmatrix} x^\prime \\ y^\prime\end{bmatrix} =\begin{bmatrix}1 & a \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y\end{bmatrix} [xy]=[10a1][xy]
  • 旋转矩阵(Rotate):默认关于原点旋转,CCW(逆时针)。
    R θ = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] R_\theta =\begin{bmatrix}\cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{bmatrix} Rθ=[cosθsinθsinθcosθ] R − θ = [ cos ⁡ θ sin ⁡ θ − sin ⁡ θ cos ⁡ θ ] = R θ T R_{-\theta} =\begin{bmatrix}\cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{bmatrix} = R_\theta^T Rθ=[cosθsinθsinθcosθ]=RθT

逆变换

  • 将变换矩阵 M M M 造成的效果消除的方法,就是再乘其逆矩阵 M − 1 M^{-1} M1
    由逆变换的定义,有 R − θ = R θ − 1 R_{-\theta} = R_\theta^{-1} Rθ=Rθ1, 则 R θ T = R θ − 1 R_\theta^T = R_\theta^{-1} RθT=Rθ1——旋转矩阵是正交的。

总结:线性变换(linear transforms):
[ x ′ y ′ ] = [ a b c d ] [ x y ] \begin{bmatrix} x^\prime \\ y^\prime\end{bmatrix} =\begin{bmatrix}a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y\end{bmatrix} [xy]=[acbd][xy]


齐次坐标(Homogeneous coordinates)

Q:为什么要引入齐次坐标的概念?

  • 对于二维图形的平移(translation)操作,分开表示很简单: x ′ = x + t x x^\prime = x + t_x x=x+tx y ′ = y + t y y^\prime = y + t_y y=y+ty ,但二维矩阵形式表示 [ x ′ y ′ ] = [ 1 0 0 1 ] [ x y ] + [ t x t y ] \begin{bmatrix} x^\prime \\ y^\prime\end{bmatrix} =\begin{bmatrix}1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y\end{bmatrix} +\begin{bmatrix} t_x \\ t_y\end{bmatrix} [xy]=[1001][xy]+[txty] 和线性变换的形式不符。是否可以用一种统一的形式表示这两种变换?

齐次坐标引入一个额外的维度,对于二维点来说其坐标变为 ( x , y , 1 ) T \begin{pmatrix}x,y,1\end{pmatrix}^T (x,y,1)T,二维向量表示为 ( x , y , 0 ) T \begin{pmatrix}x,y,0\end{pmatrix}^T (x,y,0)T。此时平移操作的矩阵变换形式为:
( x ′ y ′ w ′ ) = ( 1 0 t x 0 1 t y 0 0 1 ) ( x y 1 ) = ( x + t x y + t y 1 ) \begin{pmatrix} x^\prime\\ y^\prime\\ w^\prime\end{pmatrix} =\begin{pmatrix}1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1\end{pmatrix} = \begin{pmatrix} x+t_x \\ y+t_y \\ 1\end{pmatrix} xyw = 100010txty1 xy1 = x+txy+ty1
在齐次坐标表示下, ( x , y , w ) T , w ≠ 0 \begin{pmatrix}x,y,w\end{pmatrix}^T, w\neq0 (x,y,w)T,w=0 视为二维点 ( x / w , y / w , 1 ) T \begin{pmatrix}x/w,y/w,1\end{pmatrix}^T (x/w,y/w,1)T;因此两点相加后得到的点坐标表示的是两点连线的中点。

线性变换和平移变换统一后称为仿射变换(Affine Transformations),以齐次坐标形式表示(注意:这里是先进行线性变换,再平移):
( x ′ y ′ 1 ) = ( a b t x c d t y 0 0 1 ) ( x y 1 ) \begin{pmatrix} x^\prime\\ y^\prime\\ 1\end{pmatrix} =\begin{pmatrix}a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1\end{pmatrix} xy1 = ac0bd0txty1 xy1


变换的组合与分解

变换的组合(Composing Transforms)

矩阵的乘法不满足交换率,即矩阵乘法的顺序会对结果产生影响。相应地,变换的顺序对结果也会产生影响。
注意:变换矩阵的生效顺序是从右向左,如先旋转45°再沿x轴正方向平移一个单位:
T ( 1 , 0 ) ⋅ R 45 [ x ′ y ′ 1 ] = [ 1 0 1 0 1 0 0 0 1 ] [ cos ⁡ 4 5 ∘ − sin ⁡ 4 5 ∘ 0 sin ⁡ 4 5 ∘ cos ⁡ 4 5 ∘ 0 0 0 1 ] [ x y 1 ] T_{(1,0)} \cdot R_{45} \begin{bmatrix} x^\prime\\ y^\prime\\ 1\end{bmatrix} =\begin{bmatrix}1 & 0 & 1 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix}\cos 45^\circ & -\sin45^\circ & 0 \\ \sin45^\circ & \cos 45^\circ & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1\end{bmatrix} T(1,0)R45 xy1 = 100010101 cos45sin450sin45cos450001 xy1
对于一系列的变换 A n ( . . . A 2 ( A 1 ( x ) ) ) = A n ⋯ A 2 ⋅ A 1 ⋅ ( x y 1 ) A_n(...A_2(A_1(\bold x))) = \bold A_n \cdots\bold A_2 \cdot \bold A_1 \cdot \begin{pmatrix} x \\ y \\ 1\end{pmatrix} An(...A2(A1(x)))=AnA2A1 xy1 可以先将左侧矩阵合成后得到一个3x3的矩阵,该矩阵即可表示前面所有变换的组合变换。

复杂变换的分解(Decomposing Complex Transforms)

如何将图形以给定点c为圆心旋转?该操作可分解为:1)将图形平移至原点;2)以原点为圆心旋转;3)平移回原来位置。矩阵表示为: T ( c ) ⋅ R ( α ) ⋅ T ( − c ) \bold T(\bold c) \cdot \bold R(\alpha) \cdot \bold T(-\bold c) T(c)R(α)T(c)


三维变换

对于三维点来说齐次坐标为 ( x , y , z , 1 ) T \begin{pmatrix}x,y,z,1\end{pmatrix}^T (x,y,z,1)T,向量表示为 ( x , y , z , 0 ) T \begin{pmatrix}x,y,z,0\end{pmatrix}^T (x,y,z,0)T ( x , y , z , w ) T , w ≠ 0 \begin{pmatrix}x,y,z,w\end{pmatrix}^T, w\neq0 (x,y,z,w)T,w=0 视为三维点 ( x / w , y / w , z / w ) T \begin{pmatrix}x/w,y/w, z/w\end{pmatrix}^T (x/w,y/w,z/w)T

  • 仿射变换为:
    ( x ′ y ′ z ′ 1 ) = ( a b c t x d e f t y g h i t z 0 0 0 1 ) ( x y z 1 ) \begin{pmatrix} x^\prime\\ y^\prime\\ z^\prime\\1\end{pmatrix} =\begin{pmatrix}a & b & c& t_x \\ d & e & f& t_y \\ g & h & i & t_z \\0 & 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ z \\ 1\end{pmatrix} xyz1 = adg0beh0cfi0txtytz1 xyz1

  • 三维右手系的循环对称性: x ⃗ × y ⃗ = z ⃗ \vec x\times \vec y =\vec z x ×y =z y ⃗ × z ⃗ = x ⃗ \vec y\times \vec z =\vec x y ×z =x z ⃗ × x ⃗ = y ⃗ \vec z\times \vec x =\vec y z ×x =y 。注意得到 y ⃗ \vec y y z ⃗ \vec z z x ⃗ \vec x x 叉积的顺序不是xz,因此在三维绕y轴的旋转矩阵中 sin ⁡ α \sin \alpha sinα 正负的位置与绕x轴和绕z轴的不同。

  • Rodrigue’s Rotation Formula:将绕任意轴 n ⃗ \vec n n (默认该轴过原点) 的旋转 α \alpha α分解为绕x、y、z轴的旋转(最后一项矩阵称为反对称矩阵)推导
    R ( n , α ) = cos ⁡ ( α ) I + ( 1 − cos ⁡ ( α ) ) n n T + sin ⁡ ( α ) ( 0 − n z n y n z 0 − n x − n y n x 0 ) \bold R(\bold n, \alpha) = \cos(\alpha) \bold I + (1-\cos(\alpha))\bold n \bold n^T + \sin(\alpha)\begin{pmatrix}0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0 \end{pmatrix} R(n,α)=cos(α)I+(1cos(α))nnT+sin(α) 0nznynz0nxnynx0

  • 四元数:适合在两个旋转变换之间做插值的情形。(对两旋转矩阵的对应元素做插值并不能得到平滑的变化)


Lecture 04:观测(Viewing)变换

视图/相机变换

在计算机中生成一个视图,和现实生活中拍照片是一样的:

  1. 摆放模型的位置——模型变换(Model Transformation)
  2. 找一个好的角度——观测变换(View Transformation)
  3. 茄子!——投影变换 (Projection Transformation)

定义一个相机时,需要三个向量:position e ⃗ \vec e e 、look-at/gaze direction g ^ \hat g g^、up direction t ^ \hat t t^
我们希望在进行后续的操作前,相机的位置在一个方便的位置。约定俗成的位置是位于原点、朝向Y轴、看向Z轴负方向。
考虑把任意状态的相机转化到默认相机的操作:
在这里插入图片描述
场景中所有的物体均跟随相机进行这一操作,就可以保证相机的位置移动对实际的view不会产生影响。


投影变换

正交投影

透视投影

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值