GAMES101-图形学入门 LEC3: TRANSFORMATION-2D

上节内容回顾:

  • Vectors
    • Basic operations: addition, multiplication
  • Dot Product
    • Forward / backward ( dot product positive / negative )
  • Cross Product
    • Left / right ( cross product outward / inward )
  • Matrics

本节知识概要

  • Transformation!
  • Today
    • Why study transformation
    • 2D transformations: rotation, scale, shear
    • Homogeneous coordinates
    • Composing transforms
    • 3D transformations

Why study transformation

Modeling 模型变换

rotation 旋转
scaling 缩放

Viewing 视图变换

projection 投影

2D transformations 二维变换

  • Representing transformation using matrices
  • Rotation, scale, shear

Scale

Uniform 均匀缩放

在这里插入图片描述
x ′ = s x y ′ = s y x' = s x \\ y' = s y x=sxy=sy
写成矩阵乘法:
[ x ’ y ′ ] = [ s 0 0 s ] [ x y ] \begin{bmatrix} x’ \\ y' \end{bmatrix} = \begin{bmatrix} s &0 \\ 0 & s \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[s00s][xy]

Non-Uniform 非均匀缩放

在这里插入图片描述
x ′ = s x ⋅ x y ′ = s y ⋅ y x' = s_x \cdot x \\ y' = s_y \cdot y x=sxxy=syy
写成矩阵乘法:
[ x ’ y ′ ] = [ s x 0 0 s y ] [ x y ] \begin{bmatrix} x’ \\ y' \end{bmatrix} = \begin{bmatrix} s_x &0 \\ 0 & s_y \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[sx00sy][xy]

Reflection Matrix 反射矩阵

水平反射
在这里插入图片描述
x ′ = − x y ′ = y x' = -x \\ y' = y x=xy=y

写成矩阵乘法:
[ x ′ y ′ ] = [ − 1 0 0 1 ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} -1 &0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[1001][xy]

shear 切变

在这里插入图片描述
写成矩阵乘法:
[ x ′ y ′ ] = [ 1 a 0 1 ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} 1 &a \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[10a1][xy]

rotate 旋转,默认绕(0,0)旋转,默认逆时针方向

rotation matrix 旋转矩阵

在这里插入图片描述

[ x ′ y ′ ] = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} \cos\theta &-\sin\theta \\ \sin\theta& \cos\theta \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[cosθsinθsinθcosθ][xy]

Linear Transformation = Matrix

x ′ = a x + b y y ′ = c x + d y x' = ax+by \\ y' = cx+dy x=ax+byy=cx+dy

[ x ′ y ′ ] = [ a b c d ] [ x y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} a &b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} [xy]=[acbd][xy]

x ′ = M x x' = Mx x=Mx

Homogeneous coordinates 齐次坐标

Translation 平移

平移变换很特殊
在这里插入图片描述
平移变换无法用矩阵乘法表示

[ x ′ y ′ ] = [ a b c d ] [ x y ] + [ t x t y ] \begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} a &b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix} [xy]=[acbd][xy]+[txty]

但是我们并不希望平移变换独立开来,我们希望把平移变换也统一到矩阵乘法里面。
解决办法:齐次坐标(Homogenous coordinates)

其次坐标添加第三个坐标

若表示二维的点: 用(x,y,1)
若表示二维的向量:用(x,y,0)
注:因为向量具有平移不变性,所以向量的w坐标是0.
[ 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{bmatrix} x' \\ y' \\ w' \end{bmatrix} = \begin{bmatrix} 1 &0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} + \begin{bmatrix} x+t_x \\ y+t_y \\ 1 \end{bmatrix} xyw = 100010txty1 xy1 + x+txy+ty1

而且齐次坐标还满足:
向量 + 向量 = 向量
点 - 点 = 向量
点 + 向量 = 点
点 + 点 = ??

要得到问号的答案,需要用到齐次坐标的扩充定义:
[ x y w ] i s   t h e   2 D   p o i n t   [ x / w y / w 1 ] , w ≠ 0 \begin{bmatrix} x\\ y\\ w \end{bmatrix} is\ the\ 2D\ point\ \begin{bmatrix} x/w\\ y/w\\ 1 \end{bmatrix}, w \neq 0 xyw is the 2D point  x/wy/w1 ,w=0
所以 点+点 = 两点中点。

Affine Transformation

Affine map = linear map + translation

[ x ′ y ′ ] = [ a b c d ] ⋅ [ x y ] + [ t x t y ] \begin{bmatrix} x'\\ y' \end{bmatrix}= \begin{bmatrix} a&b\\ c&d \end{bmatrix} \cdot \begin{bmatrix} x\\ y \end{bmatrix}+ \begin{bmatrix} t_x\\ t_y \end{bmatrix} [xy]=[acbd][xy]+[txty]

Using homogenous coordinates:

[ x ′ y ′ 1 ] = [ a b t x c d t y 0 0 1 ] ⋅ [ x y 1 ] \begin{bmatrix} x'\\ y' \\ 1 \end{bmatrix}= \begin{bmatrix} a&b&t_x\\ c&d&t_y\\ 0&0&1 \end{bmatrix} \cdot \begin{bmatrix} x\\ y\\ 1 \end{bmatrix} xy1 = ac0bd0txty1 xy1

使用齐次坐标后的各种变换

Scale

S ( s x , s y ) = [ s x 0 0 0 s y 0 0 0 1 ] S(s_x,s_y) = \begin{bmatrix} s_x&0&0\\ 0&s_y&0\\ 0&0&1 \end{bmatrix} S(sx,sy)= sx000sy0001

Rotation

R ( α ) = [ cos ⁡ α − sin ⁡ α 0 sin ⁡ α cos ⁡ α 0 0 0 1 ] R(\alpha)= \begin{bmatrix} \cos\alpha & -\sin\alpha & 0 \\ \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 1 \end{bmatrix} R(α)= cosαsinα0sinαcosα0001

Translation

T ( t x , t y ) = [ 1 0 t x 0 1 t y 0 0 1 ] T(t_x, t_y) = \begin{bmatrix} 1&0&t_x\\ 0&1&t_y\\ 0&0&1 \end{bmatrix} T(tx,ty)= 100010txty1

Inverse Transform 逆变换

M − 1 M^{-1} M1
M − 1 M^{-1} M1 is th inverse of transform M M M in both a matrix and geometic sense。

复杂的变换由简单的变换组合得到

注意:变换的顺序很重要
矩阵的乘法不满足交换律,所以先旋转再平移和先平移再旋转得到的结果是不一样的。

注意:连续矩阵相乘,是从右到左逐个应用的。

写完之后,可以把左边的矩阵先全部乘起来,得到的矩阵可以表示这个复杂的变换。
在这里插入图片描述

将复杂的变换先拆解

如何以c点为中心进行旋转?

  1. 先平移到原点
  2. 绕原点进行旋转
  3. 再平移回去
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值