Game101现代计算机图形学入门学习笔记(三)

1、为什么学习变换

动画、游戏、3D、2D中都要用到变换

2、2D变换

缩放矩阵
[ s 0 0 s ] \left[\begin{matrix} s&0\\ 0&s \end{matrix} \right] [s00s]
反射矩阵
[ − 1 0 0 1 ] \left[\begin{matrix} -1&0\\ 0&1 \end{matrix} \right] [1001]
错切矩阵
[ − 1 a 0 1 ] \left[\begin{matrix} -1&a\\ 0&1 \end{matrix} \right] [10a1]
旋转矩阵
[ c o s ( θ ) − s i n ( θ ) s i n ( θ ) c o s ( θ ) ] \left[\begin{matrix} cos(\theta)&-sin(\theta)\\ sin(\theta)&cos(\theta) \end{matrix} \right] [cos(θ)sin(θ)sin(θ)cos(θ)]
以上都是线性变换,都可以用以下式子统一表示:
[ x ′ y ′ ] = [ a b c d ] [ x y ] (1) \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] \tag{1} [xy]=[acbd][xy](1)

3、齐次坐标

1、为什么使用齐次坐标?

  • 平移不能用(1)统一表示
    [ x ′ y ′ ] = [ a b c d ] [ x y ] + [ t x t y ] \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] [xy]=[acbd][xy]+[txty]
    平移不是线性变换

2、解决方法

  • 增加第三项坐标(w坐标)
  • 2维点: ( x , y , 1 ) T (x,y,1)^T (x,y,1)T
  • 2维向量: ( x , y , 0 ) T (x,y,0)^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 ) \left(\begin{matrix} x^{'}\\ y^{'}\\ w^{'} \end{matrix} \right)=\left(\begin{matrix} 1&0&t_{x}\\ 0&1&t_{y}\\ 0&0&1 \end{matrix} \right) \cdot\left(\begin{matrix} x\\ y\\ 1 \end{matrix} \right)+\left(\begin{matrix} x+t_{x}\\ y+t_{y}\\ 1 \end{matrix} \right) xyw=100010txty1xy1+x+txy+ty1
    ( x y w ) \left(\begin{matrix} x\\ y\\ w \end{matrix} \right) xyw是2维点 ( x / w y / w 1 ) ( w ≠ 0 ) \left(\begin{matrix} x/w \\ y/w \\ 1 \end{matrix} \right)(w\not=0) x/wy/w1(w=0)

3、仿射变换

  • 仿射变换=线性变换+平移
    ( x ′ y ′ ) = ( a b c d ) ( x y ) + ( t x t y ) \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) (xy)=(acbd)(xy)+(txty)
  • 使用齐次坐标
    ( x ′ y ′ 1 ) = ( a b t x c d t y 0 0 1 ) ⋅ ( x y 1 ) \left( \begin{matrix} x^{'}\\ y^{'}\\ 1 \end{matrix} \right)=\left(\begin{matrix} a&b&t_x\\ c&d&t_y\\ 0&0&1 \end{matrix} \right) \cdot \left(\begin{matrix} x\\ y\\ 1 \end{matrix} \right) xy1=ac0bd0txty1xy1

4、2维变换

1、缩放

S ( s x , s y ) = ( s x 0 0 0 s y 0 0 0 1 ) S(s_x,s_y)=\left(\begin{matrix} s_x&0&0\\ 0&s_y&0\\ 0&0&1 \end{matrix} \right) S(sx,sy)=sx000sy0001

2、旋转(证明)

R ( α ) = ( c o s ( α ) − s i n ( α ) 0 s i n ( α ) c o s ( α ) 0 0 0 1 ) R(\alpha)=\left(\begin{matrix} cos(\alpha)&-sin(\alpha)&0\\ sin(\alpha)&cos(\alpha)&0\\ 0&0&1 \end{matrix} \right) R(α)=cos(α)sin(α)0sin(α)cos(α)0001

3、平移

T ( t x , t y ) = ( 1 0 t x 0 1 t y 0 0 1 ) T(t_x,t_y)=\left(\begin{matrix} 1&0&t_x\\ 0&1&t_y\\ 0&0&1 \end{matrix} \right) T(tx,ty)=100010txty1

5、逆(反)变换

M − 1 M^{-1} M1同时是M变换在代数上和几何上的逆

4、变换组合

  • 矩阵相乘不具有交换性
    R ( 45 ) ⋅ T ( 1 , 0 ) ≠ T ( 1 , 0 ) ⋅ R ( 45 ) R(45) \cdot T(1,0) \not=T(1,0) \cdot R(45) R(45)T(1,0)=T(1,0)R(45)
  • 矩阵是从右到左应用到矩阵上的
  • T ( 1 , 0 ) ⋅ R ( 45 ) [ x y 1 ] = [ 1 0 t x 0 1 t y 0 0 1 ] [ c o s ( 4 5 ∘ ) − s i n ( 4 5 ∘ ) 0 s i n ( 4 5 ∘ ) c o s ( 4 5 ∘ ) 0 0 0 1 ] [ x y 1 ] T(1,0) \cdot R(45) \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} cos(45^{\circ})&-sin(45^{\circ})&0\\ sin(45^{\circ})&cos(45^{\circ})&0\\ 0&0&1 \end{matrix} \right]\left[ \begin{matrix} x\\ y\\ 1 \end{matrix} \right] T(1,0)R(45)xy1=100010txty1cos(45)sin(45)0sin(45)cos(45)0001xy1

5、3维变换

1、齐次坐标

3维点: ( x , y , z , 1 ) T (x,y,z,1)^T (x,y,z,1)T
3维向量: ( x , y , z , 0 ) T (x,y,z,0)^T (x,y,z,0)T

2、仿射变换

( 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( \begin{matrix} x^{'}\\ y^{'}\\ z^{'}\\ 1 \end{matrix} \right)=\left(\begin{matrix} a&b&c&t_x\\ d&e&f&t_y\\ g&h&i&t_z\\ 0&0&0&1 \end{matrix} \right) \cdot \left(\begin{matrix} x\\ y\\ z\\ 1 \end{matrix} \right) xyz1=adg0beh0cfi0txtytz1xyz1

3、3维变换

1、缩放

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 ) S(s_x,s_y,s_z)=\left(\begin{matrix} s_x&0&0&0\\ 0&s_y&0&0\\ 0&0&s_z&0\\ 0&0&0&1 \end{matrix} \right) S(sx,sy,sz)=sx0000sy0000sz00001

2、旋转

旋转矩阵是正交矩阵
R x ( α ) = ( 1 0 0 0 0 c o s ( α ) − s i n ( α ) 0 0 s i n ( α ) c o s ( α ) 0 0 0 0 1 ) R_x(\alpha)=\left(\begin{matrix} 1&0&0&0\\ 0&cos(\alpha)&-sin(\alpha)&0\\ 0&sin(\alpha)&cos(\alpha)&0\\ 0&0&0&1 \end{matrix} \right) Rx(α)=10000cos(α)sin(α)00sin(α)cos(α)00001
R y ( α ) = ( c o s ( α ) 0 s i n ( α ) 0 0 1 0 0 − s i n ( α ) 0 c o s ( α ) 0 0 0 0 1 ) R_y(\alpha)=\left(\begin{matrix} cos(\alpha)&0&sin(\alpha)&0\\ 0&1&0&0\\ -sin(\alpha)&0&cos(\alpha)&0\\ 0&0&0&1 \end{matrix} \right) Ry(α)=cos(α)0sin(α)00100sin(α)0cos(α)00001
因为 z × x z \times x z×x得到 y y y
R z ( α ) = ( c o s ( α ) − s i n ( α ) 0 0 s i n ( α ) c o s ( α ) 0 0 0 0 1 0 0 0 0 1 ) R_z(\alpha)=\left(\begin{matrix} cos(\alpha)&-sin(\alpha)&0&0\\ sin(\alpha)&cos(\alpha)&0&0\\ 0&0&1&0\\ 0&0&0&1 \end{matrix} \right) Rz(α)=cos(α)sin(α)00sin(α)cos(α)0000100001

3、平移

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 ) T(t_x,t_y,t_z)=\left(\begin{matrix} 1&0&0&t_x\\ 0&1&0&t_y\\ 0&0&1&t_z\\ 0&0&0&1 \end{matrix} \right) T(tx,ty,tz)=100001000010txtytz1

4、3维旋转

R ( α , β , γ ) = R x ( α ) R y ( β ) R z ( γ ) R(\alpha,\beta,\gamma)=R_x(\alpha)R_y(\beta)R_z(\gamma) R(α,β,γ)=Rx(α)Ry(β)Rz(γ)
被称为欧拉旋转。
会有万向节死锁(Gimbal Lock)
可以改用四元数(Quaternion)来避免

5、罗德里格斯公式

(证明)

  • 绕任意轴旋转 α \alpha α
    R ( n , α ) = c o s ( α ) I + ( 1 − c o s ( α ) ) n n T + s i n ( α ) ( 0 − n z n y n z 0 − n x − n y n x 0 ) R(n,\alpha)=cos(\alpha)I+(1-cos(\alpha))nn^T+sin(\alpha)\left(\begin{matrix} 0&-n_z&n_y\\ n_z&0&-n_x\\ -n_y&n_x&0\\ \end{matrix} \right) R(n,α)=cos(α)I+(1cos(α))nnT+sin(α)0nznynz0nxnynx0

6、模型变换

利用基础的变换矩阵将世界当中的物体调整至我们想要的地方(旋转,平移,缩放)。

7、视图变换

1、什么是视图变换

得到物体与摄像机的相对位置

  • 如何拍照
  1. 找到一个好地方并安排好人的站位(模型变换 model transformation)
  2. 找到一个好的角度取放置相机(视图变换 view transformation)
  3. 拍照(投影变换 projection transformation)

2、如何表示视图变换

在这里插入图片描述

  • 定义相机
    1、所在位置 e ⃗ \vec{e} e
    2、视线方向 g ^ \widehat{g} g
    3、朝上方向 t ^ \widehat{t} t
    (垂直于视线方向)
    • 如何变换相机
      将相机放到原点,朝上方向为Y轴,视线方向放在-Z

3、数学中的 M v i e w M_{view} Mview

  • e ⃗ \vec{e} e 放到原点
  • g ^ \widehat{g} g 旋转到-Z
  • t ^ \widehat{t} t 旋转到Y
  • 旋转( g ⃗ × t ^ \vec{g} \times \widehat{t} g ×t ) 到X
    M v i e w = R v i e w T v i e w M_{view}=R_{view}T_{view} Mview=RviewTview
  • e ⃗ \vec{e} e 放到原点
    T v i e w = ( 1 0 0 − x e 0 1 0 − y e 0 0 1 − z e 0 0 0 1 ) T_{view}=\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) Tview=100001000010xeyeze1
    因为直接求 R v i e w R_{view} Rview很困难,反过来求从原点旋转到相机的位置 R v i e w − 1 R_{view}^{-1} Rview1
    因为 R v i e w R_{view} Rview旋转矩阵为正交矩阵,所以 R v i e w − 1 = R v i e w T R_{view}^{-1}=R_{view}^{T} Rview1=RviewT
    R v i e w − 1 = ( x g ^ × t ^ x t x − g 0 y g ^ × t ^ y t y − g 0 z g ^ × t ^ z t z − g 0 0 0 0 1 ) R_{view}^{-1}=\left(\begin{matrix} x_{\widehat{g} \times \widehat{t}}& x_{t}&x_{-g}&0\\ y_{\widehat{g} \times \widehat{t}}&y_{t}&y_{-g}&0\\ z_{\widehat{g} \times \widehat{t}}&z_{t}&z_{-g}&0\\ 0&0&0&1 \end{matrix} \right) Rview1=xg ×t yg ×t zg ×t 0xtytzt0xgygzg00001
    所以
    R v i e w = ( x g ^ × t ^ y g ^ × t ^ z g ^ × t ^ 0 x t y t z t 0 x − g y − g z − g 0 0 0 0 1 ) R_{view}=\left(\begin{matrix} x_{\widehat{g} \times \widehat{t}}& y_{\widehat{g} \times \widehat{t}}&z_{\widehat{g} \times \widehat{t}}&0\\ x_{t}&y_{t}&z_{t}&0\\ x_{-g}&y_{-g}&z_{-g}&0\\ 0&0&0&1 \end{matrix} \right) Rview=xg ×t xtxg0yg ×t ytyg0zg ×t ztzg00001

4、总结

1、将物体与相机一起变换
2、让相机位于原点,朝上方位为Y轴,视线方向为-Z轴
3、也被称为模型视口变换
4、为什么需要该变换?
为了投影变换

8、投影变换

1、计算机图形学中的投影变换

将三维物体投影到二维中
在这里插入图片描述

2、正交投影

坐标的相对位置不变,光线是平行的,将物体变换到 [ − 1 , 1 ] 3 [-1,1]^3 [1,1]3的立方体中(即坐标范围为[-1,1])。

在这里插入图片描述
M o r t h o = ( 2 r − l 0 0 − r + l r − l 0 2 t − b 0 − r + b r − b 0 0 2 n − f − n + f n − f 0 0 0 1 ) M_{ortho}=\left(\begin{matrix} \frac{2}{r-l}&0&0&-\frac{r+l}{r-l}\\ 0&\frac{2}{t-b}&0&-\frac{r+b}{r-b}\\ 0&0&\frac{2}{n-f}&-\frac{n+f}{n-f}\\ 0&0&0&1 \end{matrix} \right) Mortho=rl20000tb20000nf20rlr+lrbr+bnfn+f1

3、透视投影

类似人眼看到的真实世界,近大远小。平行线在远处会相交于一点。(如何求出下列矩阵)
M p r e s → o r t h o = ( n 0 0 0 0 n 0 0 0 0 n + f − n f 0 0 1 0 ) M_{pres \rightarrow ortho}=\left(\begin{matrix} n&0&0&0\\ 0&n&0&0\\ 0&0&n+f&-nf\\ 0&0&1&0 \end{matrix} \right) Mpresortho=n0000n0000n+f100nf0
M p r e s = M o r t h o M p r e s → o r t h o M_{pres}=M_{ ortho}M_{pres \rightarrow ortho} Mpres=MorthoMpresortho
M p r e s = ( 2 n r − l 0 l + r l − r 0 0 2 n t − b b + t b − t 0 0 0 n + f n − f − n f n − f 0 0 1 0 ) M_{pres}=\left(\begin{matrix} \frac{2n}{r-l}&0&\frac{l+r}{l-r}&0\\ 0&\frac{2n}{t-b}&\frac{b+t}{b-t}&0\\ 0&0&\frac{n+f}{n-f}&-\frac{nf}{n-f}\\ 0&0&1&0 \end{matrix} \right) Mpres=rl2n0000tb2n00lrl+rbtb+tnfn+f100nfnf0

  • 下图灰色平面为近平面。
    Vertical Field of View(fovY) 是垂直可视角度
    Aspect ratio是宽高比
    在这里插入图片描述
    -将fovYaspect转换成l,r,n,t
    在这里插入图片描述

9、将标准立方体投影到屏幕

MVP
Model transformation (placing objects)
View transformation (placing camera)
Projection transformation

MVP之后我们需要把标准立方体投影到屏幕上去。

什么是屏幕

像素的二维数组,大小是固定的。
是一种典型的光栅成像设备。

Raster在德语中就是screen的意思。 Rasterize表示drawing onto the screen

屏幕空间

本课程的定义:
原点位于左下角
每个像素坐标值由(x,y)表示,x,y为整数
像素中心点是 ( x + 0.5 , y + 0.5 ) (x+0.5,y+0.5) (x+0.5,y+0.5)
屏幕覆盖范围是 ( 0 , 0 )   ( w i d t h , h e i g h t ) (0,0) ~ (width, height) (0,0) (width,height)

视口变换

[ − 1 , 1 ] 2 [-1,1]^2 [11]2空间变成 [ 0 , w i d t h ] × [ 0 , h e i g h t ] [0,width]\times[0,height] [0,width]×[0,height]
1、拉伸成屏幕一样的aspect ratio
2、平移到中心位置
视口变换矩阵:
M v i e w p o r t = ( w i d t h 2 0 0 w i d t h 2 0 h e i g h t 2 0 h e i g h t 2 0 0 1 0 0 0 0 1 ) M_{viewport}=\left(\begin{matrix} \frac{width}{2}&0&0&\frac{width}{2}\\ 0&\frac{height}{2}&0&\frac{height}{2}\\ 0&0&1&0\\ 0&0&0&1 \end{matrix} \right) Mviewport=2width00002height0000102width2height01
M = M v i e w p o r t ​ M p e r M v i e w M m o d e l M=M_{viewport}​M_{per}M_{view}M_{model} M=MviewportMperMviewMmodel

10、参考和引用

来自bilibili:GAMES101-现代计算机图形学入门-闫令琪
来自变换(二维与三维).pdf
来自变换(模型、视图、投影).pdf
来自罗德里格斯公式证明.pdf
来自CSDN:计算机图形学一:基础变换矩阵总结(缩放,旋转,位移)
来自CSDN:计算机图形学二:视图变换(坐标系转化,正交投影,透视投影,视口变换)
来自CSDN:Game101课程笔记_lecture03_transformation变换
来自知乎:【GAMES101-现代计算机图形学课程笔记】Lecture 05 光栅化(三角形)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值