• [Games101] Lecture 03-04 Transformation


    Transformation

    2D 线性变换

    • 线性变换:变换能够用矩阵乘法得到

    可以说,Linear Transformation = Matrices (of the same dimension)

    我们将如下所示的简单矩阵乘法定义为对向量 ( x , y ) T (x, y)^{T} (x,y)T 的线性变换。
    [ a 11 a 12 a 21 a 22 ] [ x y ] = [ a 11 x + a 12 y a 21 x + a 22 y ] \left[

    a11a12a21a22" role="presentation" style="position: relative;">a11a12a21a22
    \right]\left[
    xy" role="presentation" style="position: relative;">xy
    \right]=\left[
    a11x+a12ya21x+a22y" role="presentation" style="position: relative;">a11x+a12ya21x+a22y
    \right] [a11a21a12a22][xy]=[a11x+a12ya21x+a22y]

    缩放 (scale)

    缩放变换是一种沿着坐标轴作用的变换,定义如下:
    scale ⁡ ( s x , s y ) = [ s x 0 0 s y ] \operatorname{scale}\left(s_{x}, s_{y}\right)=\left[

    sx00sy" role="presentation" style="position: relative;">sx00sy
    \right] scale(sx,sy)=[sx00sy]
    即除了 ( 0 , 0 ) T (0,0)^{T} (0,0)T 保持不变之外,所有的点变为 ( s x x , s y y ) T \left(s_{x} x, s_{y} y\right)^{T} (sxx,syy)T

    剪切 (shearing)

    shear 变换直观理解就是把物体一边固定,然后拉另外一边,定义如下:
    s h e a r − x ( s ) = [ 1 s 0 1 ] , s h e a r − y ( s ) = [ 1 0 s 1 ] shear-x(s)=\left[

    1s01" role="presentation" style="position: relative;">1s01
    \right], \\shear-y (s)=\left[
    10s1" role="presentation" style="position: relative;">10s1
    \right] shearx(s)=[10s1],sheary(s)=[1s01]

    • 拉向 x x x

    preview

    • 拉向 y y y

    preview

    旋转 (rotation)

    • 在无特殊说明的情况下,默认关于 ( 0 , 0 ) (0,0) (0,0) 点,逆时针方向旋转 θ \theta θ 角度(弧度)的公式如下

    R θ = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] \mathbf{R}_{\theta}=\left[

    cosθsinθsinθcosθ" role="presentation" style="position: relative;">cosθsinθsinθcosθ
    \right] Rθ=[cosθsinθsinθcosθ]

    推导如下

    image-20220807163918587

    齐次坐标

    • 以上的线性变换矩阵不能描述平移变换,为了统一平移变换和线性变换(以上三种变换),引入平移变换

    定义 2D 坐标和 2D向量如下

    • 2D 坐标: ( x , y , 1 ) T (x,y,1)^T (x,y,1)T
    • 2D 向量: ( x , y , 0 ) T (x,y,0)^T (x,y,0)T
      • 由于向量具有平移不变性,第3维的0保护了向量不会因为平移而改变

    齐次坐标下向量和点的操作

    • vector + vector = vector

    • point – point = vector

    • point + vector = point (一个点沿着向量移动)

    • point + point = 两个点的中点

    此外,当第三维为 w ( w ≠ 0 ) w(w\ne 0) w(w=0)时,定义
    ( x y w )  is the  2 D  point  ( x / w y / w 1 ) , w ≠ 0 \left(

    xyw" role="presentation" style="position: relative;">xyw
    \right) \text { is the } 2 \mathrm{D} \text { point }\left(
    x/wy/w1" role="presentation" style="position: relative;">x/wy/w1
    \right), w \neq 0 xyw  is the 2D point  x/wy/w1 ,w=0

    仿射变换

    • 仿射变换使用一个矩阵统一了所有操作
    • 先应用线性变换再应用平移变换
    image-20220807165635765

    仿射变换下 2D 变换的描述

    Scale

    S ( s x , s y ) = ( s x 0 0 0 s y 0 0 0 1 ) \mathbf{S}\left(s_{x}, s_{y}\right)=\left(

    sx000sy0001" role="presentation" style="position: relative;">sx000sy0001
    \right) S(sx,sy)= sx000sy0001

    Rotation

    R ( α ) = ( cos ⁡ α − sin ⁡ α 0 sin ⁡ α cos ⁡ α 0 0 0 1 ) \mathbf{R}(\alpha)=\left(

    cosαsinα0sinαcosα0001" role="presentation" style="position: relative;">cosαsinα0sinαcosα0001
    \right) R(α)= cosαsinα0sinαcosα0001

    Translation

    T ( t x , t y ) = ( 1 0 t x 0 1 t y 0 0 1 ) \mathbf{T}\left(t_{x}, t_{y}\right)=\left(

    10tx01ty001" role="presentation" style="position: relative;">10tx01ty001
    \right) T(tx,ty)= 100010txty1

    逆变换

    • 使用逆矩阵
    • M − 1 \mathbf{M}^{-1} M1 is the inverse of transform M \mathbf{M} M in both a matrix and geometric sense
    image-20220807170002153

    复合变换

    • 复杂变换可由简单变换得到
    • 变换的顺序非常重要,如先旋转再平移和先平移再旋转得到的结果不同
    • 矩阵变换不满足交换律
    • 计算是右结合的
    image-20220807170258067

    3D 线性变换

    再次使用齐次坐标描述

    • 3D point = ( x , y , z , 1 ) T =(x, y, z, 1)^{T} =(x,y,z,1)T
    • 3D vector = ( x , y , z , 0 ) T =(x, y, z, 0)^{T} =(x,y,z,0)T
    • In general, ( x , y , z , w ) ( w ≠ 0 ) (x, y, z, w)(w \ne0) (x,y,z,w)(w=0) is the 3D point: ( x / w , y / w , z / w ) (x / w, y / w, z / w) (x/w,y/w,z/w)
      • e.g. ( 1 , 0 , 0 , 1 ) (1, 0, 0, 1) (1,0,0,1) and ( 2 , 0 , 0 , 2 ) (2, 0, 0, 2) (2,0,0,2) both represent ( 1 , 0 , 0 ) (1, 0, 0) (1,0,0)
    • 先应用线性变换再应用平移变换

    Use 4 × 4 4 \times 4 4×4 matrices for affine transformations
    ( 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 ) \left(

    xyz1" role="presentation" style="position: relative;">xyz1
    \right)=\left(
    abctxdeftyghitz0001" role="presentation" style="position: relative;">abctxdeftyghitz0001
    \right) \cdot\left(
    xyz1" role="presentation" style="position: relative;">xyz1
    \right) xyz1 = adg0beh0cfi0txtytz1 xyz1

    Scale

    S ( s x , s y , s z ) = ( s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ) \mathbf{S}\left(s_{x}, s_{y}, s_{z}\right)=\left(

    sx0000sy0000sz00001" role="presentation" style="position: relative;">sx0000sy0000sz00001
    \right) S(sx,sy,sz)= sx0000sy0000sz00001

    Translation

    T ( t x , t y , t z ) = ( 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ) \mathbf{T}\left(t_{x}, t_{y}, t_{z}\right)=\left(

    100tx010ty001tz0001" role="presentation" style="position: relative;">100tx010ty001tz0001
    \right) T(tx,ty,tz)= 100001000010txtytz1

    ? Rotation

    • around x − , y − x-, y- x,y, or z z z-axis

    • sin ⁡ α \sin \alpha sinα 的正负号由右手定则确定,顺序是 x → z x\to z xz

      image-20220807171648045

    R x ( α ) = ( 1 0 0 0 0 cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 0 1 ) R y ( α ) = ( cos ⁡ α 0 sin ⁡ α 0 0 1 0 0 − sin ⁡ α 0 cos ⁡ α 0 0 0 0 1 ) R z ( α ) = ( cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 0 1 0 0 0 0 1 )

    Rx(α)=(10000cosαsinα00sinαcosα00001)Ry(α)=(cosα0sinα00100sinα0cosα00001)Rz(α)=(cosαsinα00sinαcosα0000100001)" role="presentation" style="position: relative;">Rx(α)=(10000cosαsinα00sinαcosα00001)Ry(α)=(cosα0sinα00100sinα0cosα00001)Rz(α)=(cosαsinα00sinαcosα0000100001)
    Rx(α)Ry(α)Rz(α)= 10000cosαsinα00sinαcosα00001 = cosα0sinα00100sinα0cosα00001 = cosαsinα00sinαcosα0000100001

    • 所有的 3D 变换都可以被描述为在 x , y , z x,y,z x,y,z 轴上的旋转
      • 也叫欧拉角
      • Often used in flight simulators: roll, pitch, yaw

    R x y z ( α , β , γ ) = R x ( α ) R y ( β ) R z ( γ ) \mathbf{R}_{x y z}(\alpha, \beta, \gamma)=\mathbf{R}_{x}(\alpha) \mathbf{R}_{y}(\beta) \mathbf{R}_{z}(\gamma) Rxyz(α,β,γ)=Rx(α)Ry(β)Rz(γ)

    image-20220807172018445

    Rodrigues’ Rotation Formula

    Rotation by angle α \alpha α around axis n n n
    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 ) ⏟ N \mathbf{R}(\mathbf{n}, \alpha)=\cos (\alpha) \mathbf{I}+(1-\cos (\alpha)) \mathbf{n} \mathbf{n}^{T}+\sin (\alpha) \underbrace{\left(

    0nznynz0nxnynx0" role="presentation" style="position: relative;">0nznynz0nxnynx0
    \right)}_{\mathbf{N}} R(n,α)=cos(α)I+(1cos(α))nnT+sin(α)N 0nznynz0nxnynx0

    View / Camera Transformation

    Think about how to take a photo

    • Find a good place and arrange people (model transformation)
    • Find a good “angle” to put the camera (view transformation)
    • Cheese! (projection transformation 将三维空间投影到二维视图上)

    Projection transformation

    image-20220807172726840

    Orthographic projection

    正则投影的步骤

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fx3Pqwmp-1660291810051)(https://cdn.jsdelivr.net/gh/QiuHong-1202/FigureBed/2021/202208071729657.png)]

    正则立方体

    • map a cuboid [ l , r ] × [ b , t ] × [ f , n ] [l, r] \times [b, t] \times [f, n] [l,r]×[b,t]×[f,n] to the “canonical (正则、规范、标准)” cube [ − 1 , 1 ] 3 [-1, 1]^3 [1,1]3
      • 因为是朝着 z z z 轴负方向看,所以坐标小的是更远的 f f f ,坐标大的是更近的 n n n

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZTM6RjtF-1660291810052)(https://cdn.jsdelivr.net/gh/QiuHong-1202/FigureBed/2021/202208071733145.png)]

    正则投影的变换矩阵

    Translate (center to origin) first, then scale (length/width/height to 2)
    M ortho  = [ 2 r − l 0 0 0 0 2 t − b 0 0 0 0 2 n − f 0 0 0 0 1 ] [ 1 0 0 − r + l 2 0 1 0 − t + b 2 0 0 1 − n + f 2 0 0 0 1 ] M_{\text {ortho }}=\left[

    2rl00002tb00002nf00001" role="presentation" style="position: relative;">2rl00002tb00002nf00001
    \right]\left[
    100r+l2010t+b2001n+f20001" role="presentation" style="position: relative;">100r+l2010t+b2001n+f20001
    \right] Mortho = rl20000tb20000nf200001 1000010000102r+l2t+b2n+f1

    Perspective projection

    透视投影的步骤

    • First “squish” the frustum into a cuboid ( n → n , f → f ) ( M p e r s p → o r t h o ) (n \to n, f \to f) (M_{persp\to ortho}) (nn,ff)(Mpersportho) (将远平面挤压为与近平面相同的大小)
      • 挤压规则
        • 近平面永远不变,任何点在近平面上不变
        • 远平面 z z z 值不变,任何远平面上的点 z z z 值不变
        • 远平面的中心不变
    • Do orthographic projection ( M o r t h o M_{ortho} Mortho) (做正则投影)
    image-20220807174933191 image-20220807175137238

    透视投影的变换矩阵推导

    计算机图形学二:视图变换(坐标系转化,正交投影,透视投影,视口变换)

    透视投影的变换矩阵

    M per  = M ortho  M persp  →  ortho  \mathrm{M}_{\text {per }}=\mathrm{M}_{\text {ortho }} \mathrm{M}_{\text {persp } \rightarrow\text { ortho }} Mper =Mortho Mpersp  ortho 

    M per  = [ 2 r − l 0 0 0 0 2 t − b 0 0 0 0 2 n − f 0 0 0 0 1 ] [ 1 0 0 − r + l 2 0 1 0 − t + b 2 0 0 1 − n + f 2 0 0 0 1 ] [ n 0 0 0 0 n 0 0 0 0 n + f − f n 0 0 1 0 ] \mathrm{M}_{\text {per }}=\left[

    2rl00002tb00002nf00001" role="presentation" style="position: relative;">2rl00002tb00002nf00001
    \right]\left[
    100r+l2010t+b2001n+f20001" role="presentation" style="position: relative;">100r+l2010t+b2001n+f20001
    \right] \left[
    n0000n0000n+ffn0010" role="presentation" style="position: relative;">n0000n0000n+ffn0010
    \right] Mper = rl20000tb20000nf200001 1000010000102r+l2t+b2n+f1 n0000n0000n+f100fn0

  • 相关阅读:
    面试--并发多线程基础
    网络安全—小白自学
    项目二--01:基于nginx、keepalived的高可用集群之负载均衡
    【web-攻击用户】(9.7.2)本地隐私攻击:攻击ActiveX控件
    【python】准点跑路人必备小程序~ 不信你用不到
    【分布式能源的选址与定容】基于非支配排序多目标遗传优化算法求解分布式能源的选址与定容(Matlab代码实现)
    Flink 启用与配置检查点 Checkpoint
    python中的预编译正则表达式
    python 基于PHP+MySQL的驾校信息管理系统
    SQL分页查询,SQL的LIMIT语句用法,SQL如何实现分页查询,SpringBoot实现分页查询。
  • 原文地址:https://blog.csdn.net/Muyunuu/article/details/126306351