【译】GAMES101-现代计算机图形学入门:Lec03~04_变换

155 阅读9分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 N 天,点击查看活动详情


大家好,我是曹骏。因为本人对计算机图形学、WebGPU等非常感兴趣,因此会陆续发布闫令琪博士的《GAMES101:现代计算机图形学入门》课程译文,希望能帮助到更多的同学理解课程,爱上图形学这一浪漫的学科。

我先给出课程的链接:
www.bilibili.com/video/BV1X7…
www.bilibili.com/video/BV1X7…

1 为什么学习变换 Why study transformation

1.1 模型变换 Modeling

  • 场景展示——描述摄像机的位置移动

  • 机器人跳舞——物体的旋转

  • 皮克斯开场动画台灯压扁字母I——物体的缩放

1.2  视图变换 Viewing

  • 光栅化成像中涉及大量的视图变换

2 二维变换 2D Transformations

3B1B线代的本质(可视化理解线性变换):www.bilibili.com/video/BV1ns…

2.1 缩放变换矩阵 Scale Matrix

  • 不锁定比例的 Non-Uniform

2.2 对称变换矩阵 Reflection Matrix

  • 水平翻转 Horizontal reflection:

2.3 切变变换矩阵 Shear Matrix

  • 水平方向位移在y=0处是0,在y=1处是a,竖直方向位移总是0

2.4 旋转变换矩阵(默认绕原点、逆时针方向) Rotation Matrix

  • 旋转的角度为θ

2.5 线性变换 Linear Transforms

  • 使用相同维度的矩阵相乘,形式为坐标与矩阵相乘得到新的坐标

3 齐次坐标 Homogeneous Coordinates

3.1 为什么学习齐次坐标 Why homogeneous coordinates

  • 平移变换不是线性变换,无法直接用矩阵形式表示

  • 我们不想把平移当作一个特殊的变换 But we don’t want translation to be a special case

  • 是否有一种统一的方式能够表示所有变换,它的代价怎么样? Is there a unified way to represent all transformations?  (and what’s the cost?)

3.2 通过齐次坐标解决问题 Solution: Homogenous Coordinates

3.2.1 添加第三个坐标 Add a third coordinate (w-coordinate)

3.2.2 平移的矩阵表示 Matrix representation of translations

3.2.3 如果平移的是一个向量? What if you translate a vector?

  • 向量具有平移不变性,经过平移变换后还是原向量

3.2.4 齐次坐标的妙处

  • 如果w-coordinate的结果是1或0则为有效操作,这样既能让平移变换形式统一,也保留了向量和点加减运算的可操作性
  • 使用齐次坐标时,(x, y, w)表示平面内一点(x/w, y/w, 1), w≠0
    • vector + vector = vector        0+0=0

两个向量的w都为0,相加后还是为0,所以结果仍然是一个向量

    • point – point = vector            1-1=0

两个向量的w都为1,相减后为0,所以结果是一个向量

    • point + vector = point           1+0=1

原来的point沿着向量vector方向移动一段距离后,产生的新点

    • point + point = ??

point+point得到的结果为两个点连线的中点(将w-coordinate化为1)

3.2.5 仿射变换 Affine Transformations

  • 仿射变换 = 线性变换 + 平移变换  Affine map = linear map + translation

  • 使用齐次坐标 Using homogenous coordinates:

3.2.6 使用齐次坐标的2D变换 2D Transformations

齐次坐标的代价是引入了额外的数字参与计算

4 逆变换 Inverse Transform

  • M-1既是M矩阵的逆,也是矩阵几何意义上的逆

5 组合变换 Composite Transform

5.1 变换的顺序会影响结果! Transform Ordering Matters!

  • 先平移后旋转与先旋转后平移

  • 从矩阵的运算来看,矩阵的乘法不满足交换律 Matrix multiplication is not commutative

  • 注意矩阵的运算顺序是从右到左的 Note that matrices are applied right to left

5.2 组合变换 Composite Transform

  • 对于仿射变换序列A1, A2, A3, ...可以先使用结合律得到一个表示组合变换的单个矩阵,这一点对性能消耗十分重要!

6 分解复杂的变换 Decomposing Complex Transforms

  • 如何以任意一点c为圆心旋转 How to rotate around a given point c?
    • 先把旋转中心点平移到原点 Translate center to origin
    • 旋转 Rotate
    • 平移到原位置 Translate back

7 三维变换 3D Transformations

7.1 使用齐次坐标描述三维空间中的点和向量

  • 再一次使用齐次坐标 Use homogeneous coordinates again:

  • 使用齐次坐标时,(x, y, z, w)表示三维空间内一点(x/w, y/w, z/w), w≠0

  • 使用4×4矩阵来表示仿射变换 Use 4×4 matrices for affine transformation

  • 仿射变换的顺序是先进行线性变换,再平移

7.2 三维空间中的缩放与平移 Scale and Translation

  • 缩放 Scale

  • 平移 Translation

7.3 三维空间中的旋转 3D Rotations

7.3.1 沿x,y,z轴旋转 Rotation around x-, y-, or z-axis

注意:沿y轴旋转时矩阵稍有不同(z叉乘x得到y轴,而不是x叉乘z)
弹幕解释:xyz,yzx,zxy,这三个没有本质区别,懂了这个,前面迎刃而解

7.3.2 任意一个旋转能看作几个绕轴旋转的组合 Compose any 3D rotation from Rx, Ry, Rz

  • 三个一组的描述物体旋转的参量叫欧拉角 Euler angles
  • 在飞行模拟器中一般有滚转、俯仰、偏航 Often used in flight simulators: roll, pitch, yaw

  • 罗德里格旋转公式 Rodrigues’ Rotation Formula

  • for Identity Matrix 单位矩阵

  • n向量是围绕旋转的轴

  • 如果要沿任意轴旋转,和二维类似,需要先把旋转的起点移到轴上,然后旋转,最后再移回原位

  • 四元数(Quaternion)的引入主要是为了解决旋转角度间的插值问题,本课暂不涉及

8 观测变换 Viewing transformation

8.1 视图变换 View/Camera transformation

8.1.1 什么是视图变换 What is view transformation?

  • 想象一下如何拍一张照片 Think about how to take a photo
    1. 找个地方安排人(模型变换)Find a good place and arrange people (model transformation)

    2. 找个“角度”放相机(视图变换)Find a good “angle” to put the camera (view transformation)

    3. 茄子!(投影变换)Cheese! (projection transformation)

8.1.2 如何确定视图变换 How to perform view transformation?

  • 定义一个相机
    • 确定位置 Position
    • 确定看向的方向 Look-at / gaze direction
    • 确定向上方向(来确定相机左右倾斜方向)Up direction

8.1.3 标准位置 Key observation

  • 相机和物体的相对位置不变,则画面也不变 If the camera and all objects move together,  the “photo” will be the same

  • 我们通常将相机的位置移动到原点、向上方向为Y轴方向、看向-Z轴方向,物体与相机同时变换we always transform the camera to the origin, up at Y, look at -Z, and transform the objects along with the camera

  • 通过矩阵来对相机进行变换 Transform the camera by

  1. 平移到原点 Translate e to origin

  1. 将g旋转到-Z,t旋转到Y,(g×t)旋转到X Rotate g to -Z, t to Y, (g x t) To X

旋转矩阵是正交矩阵(Orthogonal Matrix) ,这里因其逆矩阵比较好写出来,所以利用了正交矩阵的3逆等于其转置的性质求出了旋转矩阵

8.2 投影变换 Projection transformation

8.2.1正交投影与透视投影 Perspective projection vs. orthographic projection

  • 人眼更接近透视投影,正交投影常用于工业制图
  • “近大远小““一叶障目”“道理我都懂但是鸽子为什么这么大”都指的是透视投影


8.2.2 正交投影 Othographic projection

简单的理解 A simple way of understanding

  1. 相机在原点,朝向-Z方向,上指Y方向 Camera located at origin, looking at -Z, up at Y

  2. 去掉Z轴坐标 Drop Z coordinate

  3. 将得到的矩形平移并缩放到 Translate and scale the resulting rectangle to

正式的理解 In general

  • 我们希望将一个立方体空间映射成一个标准的正方体 We want to map a cuboid to the “canonical cube

  • 在变换时先平移(中心移到原点),再缩放(长宽高变为2) Translate (center to origin) first, then scale (length/width/height to 2)

  • 注意事项
    • 相机看向-Z方向时近的坐标会大于远的坐标 Looking at / along -Z is making near and far not intuitive (n > f)

    • 这也是为什么OpenGL在这一步使用左手坐标系 that’s why OpenGL (a Graphics API) uses left hand coords

8.2.3 透视投影 Perspective projection

怎样做透视投影?

  1. 首先将截头体“挤压”成一个长方体 First “squish” the frustum into a cuboid (n -> n, f -> f)()
  2. 做正交投影 Do orthographic projection (, already known!)

“挤压”变换的矩阵推导过程

  1. 由相似三角形可得到坐标变换前后的关系 Find the relationship between transformed points (x’, y’, z’) and the original points (x, y, z)

  1. 此时利用齐次坐标可以写出含有一个未知数的变换后的坐标 In homogeneous coordinates

  1. 所以“挤压”变换的矩阵有如下关系 So the “squish” (persp to ortho) projection does this

  1. 以上信息我们已经可以填出一部分 Already good enough to figure out part of (接下来只用找出该矩阵第三行上的数到底是多少即可)

  1. 利用下面两种场景推导第三行的数(特殊值法)
    1. 假设真实点(x,y,z)的位置就位于近平面(n所在位置),那么投影的位置和它是同一个,也就是任何在近的平面上的点坐标都不变 Any point on the near plane will not change
    2. 假设真实点(x,y,z)的位置位于远平面,那么在投影中z轴上的点和它的x, y是一样的
  1. 任何在近的平面上的点坐标都不变 Any point on the near plane will not change

与x,y没有关系,所以的第三行一定是(0 0 A B),A,B为未知量 So the third row must be of the form (0 0 A B)

  1. 任何在远处平面上的点Z轴坐标都不变,且远处平面中心点变换前后都为(0, 0, f, 1) Any point’s z on the far plane will not change

第三行为(0 0 A B)时可得到关系式

  1. 解出A和B的值 Solve for A and B

  1. 终于,我们已经知道了的每一项 Finally, every entry in Mpersp->ortho is known!

在挤压的过程中,对于远近平面的中间的某一个点,经过变化后会被推向远处
个人猜想:这个和画画时近大远小中间处被推向远处可能有关(即a<b)

  • 完整的透视投影变换

  • 如何定义视锥近面中的l, r, b, t What’s near plane’s l, r, b, t
    • 垂直可视角度 Vertical Field-of-View (fovY)
    • 宽高比 Aspect ratio

    • 垂直可视角度、宽高比和l, r, b, t的转换关系 How to convert from fovY and aspect to l, r, b, t?