计算机图形学学习笔记(向量、矩阵、变换)

计算机图形学学习笔记(向量、矩阵、变换)



前言

向量(Vector)运算、矩阵(Matrix)运算、和变换矩阵 (Transformation Marices) 在计算机图形学中非常重要,一切物体的缩放,旋转,位移,都可以通过变换矩阵作用得到。同时在投影 (projection) 变换的时候也有很多应用,本文将会介绍一些向量运算、矩阵运算和一些简单的变换矩阵


一、向量运算

1.向量点乘

几何: a ⃗ ⋅ b ⃗ \vec{a}\cdot\vec{b} a b = ∣ ∣ a ⃗ ∣ ∣ ||\vec{a}|| a * ∣ ∣ b ⃗ ∣ ∣ ||\vec{b}|| b * cos ⁡ θ \cos\theta cosθ
代数: a ⃗ ⋅ b ⃗ = ( x a y a ) ⋅ ( x b y b ) = x a x b + y a y b \vec{a}\cdot\vec{b} = \left(\begin{matrix}x_a\\y_a\\\end{matrix}\right)\cdot\left(\begin{matrix}x_b\\y_b\\\end{matrix}\right)=x_ax_b+y_ay_b a b =(xaya)(xbyb)=xaxb+yayb

∣ ∣ a ⃗ ∣ ∣ ||\vec{a}|| a a ⃗ \vec{a} a 向量的模
cos ⁡ θ \cos\theta cosθ a ⃗ \vec{a} a b ⃗ \vec{b} b 向量的夹角
向量点乘符合交换、结合、分配律

2.向量叉乘

几何: a ⃗ × b ⃗ \vec{a}\times\vec{b} a ×b = − ( b ⃗ × a ⃗ ) -(\vec{b}\times\vec{a}) (b ×a )
∣ ∣ a ⃗ × b ⃗ ∣ ∣ ||\vec{a}\times\vec{b}|| a ×b = ∣ ∣ a ⃗ ∣ ∣ ||\vec{a}|| a * ∣ ∣ b ⃗ ∣ ∣ ||\vec{b}|| b * sin ⁡ θ \sin\theta sinθ
叉乘获得垂直于 a ⃗ \vec{a} a b ⃗ \vec{b} b 的向量方向(右手螺旋定则,四指旋转方向为:如果 a ⃗ × b ⃗ \vec{a}\times\vec{b} a ×b 就从 a ⃗ \vec{a} a 旋转到 b ⃗ \vec{b} b 大拇指方向为叉乘向量的方向)
注意:GAMES101教学使用右手系,所以使用右手螺旋定则,Unity和UE引擎都使用左手系所以应变为左手定则
代数: a ⃗ × b ⃗ = ( x a y a z a ) × ( x b y b z b ) = ( y a z b − y b z a z a x b − x a z b x a y b − y a x b ) \vec{a}\times\vec{b} = \left(\begin{matrix}x_a\\y_a\\z_a\end{matrix}\right)\times \left(\begin{matrix}x_b\\y_b\\z_b\end{matrix}\right) = \left(\begin{matrix}y_az_b-y_bz_a\\z_ax_b-x_az_b\\x_ay_b-y_ax_b\end{matrix}\right) a ×b =xayaza×xbybzb=yazbybzazaxbxazbxaybyaxb
叉乘符合结合、分配律,但是不符合交换律(方向会反)
向量叉乘的应用:判断左右、判断内外

a ⃗ × b ⃗ \vec{a}\times\vec{b} a ×b 得正: a ⃗ \vec{a} a b ⃗ \vec{b} b 右侧

在这里插入图片描述 A B ⃗ × A P ⃗ \vec{AB}\times\vec{AP} AB ×AP B C ⃗ × B P ⃗ \vec{BC}\times\vec{BP} BC ×BP C A ⃗ × C P ⃗ \vec{CA}\times\vec{CP} CA ×CP 方向相同则P在三角形ABC内

二、矩阵运算

1.矩阵相乘

一个M行N列的矩阵与一个N行P列的矩阵相乘获得一个M行P列矩阵
A(M × \times × N) × \times × B(N × \times × P) = C(M × \times × P)
C矩阵i行j列的元素是A矩阵第i行的行向量与B矩阵第j列的列向量的点乘
性质:没有交换律,有结合、分配律
一些公式:
( A B ) T = B T A T (AB)^T=B^TA^T ABT=BTAT
( A B ) − 1 = B − 1 A − 1 (AB)^{-1}=B^{-1}A^{-1} AB1=B1A1
向量叉乘用矩阵乘法形式表达(知道即可):
a ⃗ × b ⃗ = A × b ⃗ = ( 0 − z a y a z a 0 − x a − y a x a 0 ) ( x b y b z b ) \vec{a}\times\vec{b}=A\times\vec{b}= \left(\begin{matrix}0&-z_a&y_a\\z_a&0&-x_a\\-y_a&x_a&0\end{matrix}\right)\left(\begin{matrix}x_b\\y_b\\z_b\end{matrix}\right) a ×b =A×b =0zayaza0xayaxa0xbybzb

三、变换(Transform)

1.2D变换

3.1.1缩放(Scale)

在这里插入图片描述 ( x y ) × ( s 0 0 s ) = ( x 1 y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}s&0\\0&s\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right) (xy)×(s00s)=(x1y1)
在这里插入图片描述 ( x y ) × ( s x 0 0 s y ) = ( x 1 y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}s_x&0\\0&s_y\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right) (xy)×(sx00sy)=(x1y1)
水平翻转:
在这里插入图片描述 ( x y ) × ( − 1 0 0 − 1 ) = ( x 1 y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}-1&0\\0&-1\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right) (xy)×(1001)=(x1y1)
切变:
在这里插入图片描述 ( x y ) × ( − 1 a 0 − 1 ) = ( x 1 y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}-1&a\\0&-1\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right) (xy)×(10a1)=(x1y1)
理解:A点移动了a,B点移动了a/2,所以 x 1 = x + a y x_1=x+ay x1=x+ay

3.1.2旋转(Rotate)

在这里插入图片描述 ( x y ) × ( cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ) = ( x 1 y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right) (xy)×(cosθsinθsinθcosθ)=(x1y1)
R − θ = ( cos ⁡ θ sin ⁡ θ − sin ⁡ θ cos ⁡ θ ) = R θ T R_{-\theta}=\left(\begin{matrix}\cos\theta&\sin\theta\\-\sin\theta&\cos\theta\\\end{matrix}\right)=R_{\theta}^T Rθ=(cosθsinθsinθcosθ)=RθT
R − θ = R θ − 1 ( 定 义 可 得 ) R_{-\theta}=R_{\theta}^{-1}(定义可得) Rθ=Rθ1()

3.1.3线性变换(Linear Transforms)

线性变换=矩阵
( x y ) × ( a b c d ) = ( x 1 y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)\times\left(\begin{matrix}a&b\\c&d\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right) (xy)×(acbd)=(x1y1)
x 1 = a x + b y x_1 =ax+by x1=ax+by
y 1 = c x + d y y_1 =cx+dy y1=cx+dy
以上介绍的变换都是线性变换

3.1.4齐次坐标(Homogeneous coordinates)

平移:
在这里插入图片描述 ( x y ) + ( t x t y ) = ( x 1 y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)+\left(\begin{matrix}t_x\\t_y\\\end{matrix}\right)=\left(\begin{matrix}x_1\\y_1\\\end{matrix}\right) (xy)+(txty)=(x1y1)
x 1 = x + t x x_1 =x+t_x x1=x+tx
y 1 = y + t y y_1 =y+t_y y1=y+ty
平移不能用传统的变换矩阵表示,所以平移不是线性变换
平移——>矩阵表示——>引入齐次坐标,增加一个新的维度(w-coordinate)即
点: ( x y ) − > ( x y 1 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)->\left(\begin{matrix}x\\y\\1\end{matrix}\right) (xy)>xy1
向量:为什么向量是(x,y,0)?因为向量平移之后还是原向量 ( x y ) − > ( x y 0 ) \left(\begin{matrix}x\\y\\\end{matrix}\right)->\left(\begin{matrix}x\\y\\0\end{matrix}\right) (xy)>xy0
表示平移:
( x 1 y 1 w 1 ) = ( 1 0 t x 0 1 t y 0 0 1 ) ( x y 1 ) = ( x + t x y + t y 1 ) \left(\begin{matrix}x_1\\y_1\\w_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) x1y1w1=100010txty1xy1=x+txy+ty1
简而言之,齐次坐标就是用N+1维来代表N维坐标。我们可以在一个2D笛卡尔坐标末尾加上一个额外的变量w来形成2D齐次坐标,因此,一个点(X,Y)在齐次坐标里面变成了(x,y,w),并且一定有:
X = x/w
Y = y/w
也就是说我们把齐次坐标转化为笛卡尔坐标的方法是前面n-1个坐标分量分别除以最后一个分量即可。
在这里插入图片描述
在转换的过程中,会发现(1, 2, 3), (2, 4, 6) 和(4, 8, 12)对应同一个笛卡尔坐标点 (1/3, 2/3),任何标量的乘积,例如(1a, 2a, 3a) 对应 笛卡尔空间里面的(1/3, 2/3) 。因此,这些点是“齐次的”,因为他们代表了笛卡尔坐标系里面的同一个点。换句话说,齐次坐标有规模不变性

3.1.5仿射变换(Affine transform)

仿射变换 = 先线性变换 + 后平移
缩放、旋转、平移变换都属于仿射变换
使用齐次坐标:
( x 1 y 1 w 1 ) = ( a b t x c d t y 0 0 1 ) ( x y 1 ) \left(\begin{matrix}x_1\\y_1\\w_1\end{matrix}\right)=\left(\begin{matrix}a&b&t_x\\c&d&t_y\\0&0&1\end{matrix}\right)\left(\begin{matrix}x\\y\\1\end{matrix}\right) x1y1w1=ac0bd0txty1xy1

3.1.6逆变换(Inverse transform)

在这里插入图片描述

3.1.7组合变换

在这里插入图片描述
为什么变换结果不同?
因为矩阵乘法不符合交换律
T ( 1 , 0 ) R 45 ( x y 1 ) ≠ R 45 T ( 1 , 0 ) ( x y 1 ) T_{(1,0)}R_{45}\left(\begin{matrix}x\\y\\1\end{matrix}\right)\neq R_{45}T_{(1,0)}\left(\begin{matrix}x\\y\\1\end{matrix}\right) T(1,0)R45xy1=R45T(1,0)xy1

2.3D变换(同2D不过多赘述)

参考

GAMES101-现代计算机图形学入门-闫令琪视频课程

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值