Lecture 04 Transformation Cont
一、3D transformations
1、General Introduction
Use homogeneous coordinates again:
- 3D point = (x, y, z, 1)T
- 3D vector = (x, y, z, 0)T
In general, (x, y, z, w) (w != 0) is the 3D point:
(x/w, y/w, z/w)
在3D里同理与上一节2D中所推导的方法,用齐次坐标来描述变换。在这里依然需要注意区分3D的点和向量。
Use 4×4 matrices for affine transformations:
![image-20210113192952341](https://i-blog.csdnimg.cn/blog_migrate/7d76a644447635679cd981067f4f3ee7.png)
2、Basic 3D Transformations
(1)、Scale
![image-20210113193142891](https://i-blog.csdnimg.cn/blog_migrate/71bbaa5f212fa116990f19a4fb51334b.png)
(2)、Translation
![image-20210113193247143](https://i-blog.csdnimg.cn/blog_migrate/c895829d6dda593aeeaac7a9ab2216b9.png)
(3)、Rotation around x-, y-, or z-axis
![image-20210113193343590](https://i-blog.csdnimg.cn/blog_migrate/3a12ba6ed6fa40b9e1f82979ca4e652c.png)
在这里,我们可以观察到沿y轴旋转的旋转矩阵中有两项(右上和左下的sinα)的符号被调换了,这是因为以右手螺旋定则来看的话,沿x轴(大拇指指向x轴)是从y转向z,沿z轴(大拇指指向z轴)是从x到y,而沿y轴是从z到x(而不是x到z),所以相应的符号也就反了。
3、3D Rotations
任意一个旋转都可以被分解成由绕x,y,z轴的三个旋转合成的一个旋转,因此
![image-20210113194156074](https://i-blog.csdnimg.cn/blog_migrate/b97e48c53b5109dcf7bd4c36358cbe9d.png)
-
So-called Euler angles(这也就是所说的欧拉角)
-
Often used in flight simulators: roll, pitch, yaw
但是使用欧拉角会产生万向锁的问题
4、Rodrigues’ Rotation Formula
Rotation by angle α around axis n
![image-20210113194418822](https://i-blog.csdnimg.cn/blog_migrate/78986cff70c38112b8f19def0034c7ac.png)
二、Viewing transformation
(一)、View / Camera transformation
1、Perform View Transformation
Define the camera first
![image-20210113195325021](https://i-blog.csdnimg.cn/blog_migrate/f76742454b90d579fe413533fd63dd22.png)
-
Position e ⃗ \vec{e} e
-
Look-at / gaze direction g ^ \hat{g} g^
-
Up direction t ^ \hat{t} t^
在此处我们要定义三个量,来确定唯一的摄像机变换。首先要确定摄像机在空间中所在的位置 e ⃗ \vec{e} e=(x,y,z),然后确定摄像机朝向的方向 g ^ \hat{g} g^。当确定了这两个信息以后,我们可以发现摄像机仍然可以沿着 g ^ \hat{g} g^所在的轴(左右)旋转(在建模软件中做摄像机动画时Z轴没锁),因此我们还需要确定一个向上的方向 t ^ \hat{t} t^,来锁住旋转轴 g ^ \hat{g} g^。这样就确定了唯一的一个摄像机信息。
2、Key observation
- If the camera and all objects move together, the “photo” will be the same
![image-20210113200258064](https://i-blog.csdnimg.cn/blog_migrate/6e559246909ddd3bcc8814fbfc84e7fe.png)
如果摄像机和场景中所有的东西一起移动的话,那么整个场景在摄像机中的画面还是相对保持不变。因此约定了将摄像机的位置 e ⃗ \vec{e} e变换到坐标原点(0,0,0),将向上的方向 t ^ \hat{t} t^变换到Y轴,将摄像机朝向的方向 g ^ \hat{g} g^变换到-Z。让场景中的其他所有物体也随着摄像机的变换而变换。
3、Transform the camera by Mview
Steps:
- Translates e ⃗ \vec{e} e to origin
- Rotates g ^ \hat{g} g^ to -Z
- Rotates t ^ \hat{t} t^ to Y
- Rotates ( g ^ \hat{g} g^ x t ^ \hat{t} t^) to X
Mview in math:Mview = RviewTview
Translate e ⃗ \vec{e} e to origin:
![image-20210113202131343](https://i-blog.csdnimg.cn/blog_migrate/0c488603c29b8c67d0679fa02daf1e71.png)
Rotate g ^ \hat{g} g^ to -Z, t ^ \hat{t} t^ to Y, ( g ^ \hat{g} g^ x t ^ \hat{t} t^) to X
在写旋转矩阵的时候,从未知的 g ^ \hat{g} g^、 t ^ \hat{t} t^和 g ^ \hat{g} g^x t ^ \hat{t} t^变换到-Z、Y、X可能很困难,但是从X、Y、Z变换到 g ^ \hat{g} g^ x t ^ \hat{t} t^、 t ^ \hat{t} t^、 g ^ \hat{g} g^就显得容易了很多
Consider its inverse rotation: X to ( g ^ \hat{g} g^ x t ^ \hat{t} t^), Y to t ^ \hat{t} t^ ,Z to - g ^ \hat{g} g^
因此,我们只需要求出Rview 的逆矩阵,再求逆,即可得到Mview
![image-20210113202746684](https://i-blog.csdnimg.cn/blog_migrate/53b64871026db28e6509a99ea57e2d0d.png)
由于该旋转矩阵是正交矩阵,所以该矩阵的逆就是该矩阵的转置矩阵
![image-20210113202900989](https://i-blog.csdnimg.cn/blog_migrate/45cec59627bb72daaf2134111e392dd9.png)
Summary
- Transform objects together with the camera
- Until camera’s at the origin, up at Y, look at -Z
- Also known as ModelView Transformation
But why do we need this?
For projection transformation!
(二)、Projection transformation
Projection in Computer Graphics(3D to 2D)
Perspective projection vs. orthographic projection
![image-20210113203726435](https://i-blog.csdnimg.cn/blog_migrate/50386c965fc7e93a96a8d34f00e04642.png)
![image-20210113203817617](https://i-blog.csdnimg.cn/blog_migrate/3455c1e54fb0bd05106a0c6a7d8b2626.png)
1、Orthographic projection
(1)、A simple way of understanding
- Camera located at origin, looking at -Z, up at Y
- Drop Z coordinate
- Translate and scale the resulting rectangle to [-1, 1]2
![image-20210113211418774](https://i-blog.csdnimg.cn/blog_migrate/d349389ba99ebf817e69fef6df762e63.png)
对于正交投影,最简单的理解方式就是将所有三维空间的物体挤压到一个二维的平面,也就是将所有物体的z坐标直接去掉,也就实现了从三维到二维平面的映射。
(2)、In general
![image-20210113211649220](https://i-blog.csdnimg.cn/blog_migrate/e03415b84d99584724b3db6338f3dfac.png)
We want to map a cuboid [l, r] x [b, t] x [f, n] to the “canonical (正则、规范、标准)” cube [-1, 1]3
在这里,不管在空间中给一个什么样的长方体,都可以映射成一个中心在原点的立方体。
在X轴上,我们定义左和右(l和r),在Y轴上,我们定义下和上(b和t),在Z轴上,我们定义远和近(f和n)。通过这六个值,我们即可定义一个长方体。
Translate (center to origin) first, then scale (length/width/height to 2)
![image-20210113212325941](https://i-blog.csdnimg.cn/blog_migrate/70e376079f43a8ff2d378bc52e976cfb.png)
先通过一个位移矩阵将长方体中心移动到原点,再通过一个缩放矩阵将长方体压缩成一个立方体。
2、Perspective projection
![image-20210113213156767](https://i-blog.csdnimg.cn/blog_migrate/81991af22c2c1151f37fb7c61ce367e2.png)
- Parallel lines not parallel; converge to single point
- Further objects are smaller
How to do perspective projection↓
-
First “squish” the frustum into a cuboid (n -> n, f -> f) (Mpersp->ortho)
-
Do orthographic projection (Mortho,already known!)
我们要通过变换将左侧这个视锥“挤”成右边这个长方体,再做一次正交投影,问题就解决了。在上面我们已经知道正交投影怎么做了,所以现在所有的问题集中在如何将这个视锥“挤”成右边这个长方体。
- Any point on the near plane will not change
- Any point’s z on the far plane will not change
在此我们定义几个问题。首先近平面在挤压之后,任何一个点都不会变,远平面在挤压过程中,Z值不会变,也就是说挤压之后远平面的Z值依然是f;远平面的中心点在挤压之后也不会发生变化,x、y依然是0,z=f。
Recall the key idea: Find the relationship between transformed points (x’, y’, z’) and the original points (x, y, z)
![image-20210113214247918](https://i-blog.csdnimg.cn/blog_migrate/9d82726110c4dfdd9cffb48d067da503.png)
通过侧视图,对于视锥上任意一个点(x,y,z)和近平面(x’,y‘,z’),我们会发现两个相似三角形,近平面上的点的z‘=n,根据相似三角形对应边成比例我们可以求出 y ′ = n z y y'=\frac{n}{z}y y′=zny、 x ′ = n z x x'=\frac{n}{z}x x′=znx。
那么我们将刚刚的变换过程写成齐次坐标的形式:
![image-20210113214506684](https://i-blog.csdnimg.cn/blog_migrate/6ae9b8e5de01fe445c4f77a3a34451b4.png)
此处我们根据齐次坐标的性质,将中间的式子同乘一个z,即得到最右边的式子,这就是原来的点通过变换后得到的新的点的坐标。此时z’我们依然不知道。
我们希望算出来到底是什么样的一个变换矩阵使得这个点的坐标变换成最右边的样子,也就是我们要求出矩阵Mpersp->ortho(4×4)
![image-20210113215919970](https://i-blog.csdnimg.cn/blog_migrate/3a6607d43197645ed0d1436fb76b10a2.png)
此时可以先通过刚刚的已知量将这个矩阵的部分写出来:
![image-20210113220116063](https://i-blog.csdnimg.cn/blog_migrate/6b81a910a1f6ba396b71e96bbd5ed510.png)
下面我们来取两个特殊的点。刚刚在一开始已经提到,近平面在挤压之后,任何一个点都不会变,远平面在挤压过程中,Z值不会变,也就是说挤压之后远平面的Z值依然是f(z=f)。
首先我们取近平面上任意一点,由于近平面在挤压之后,任何一个点都不会变,所以此时z就可以用n来替代,得到以下式子:
![image-20210113220514740](https://i-blog.csdnimg.cn/blog_migrate/2c5cd6d57e428b2b80daadc9c6689f18.png)
所以这个矩阵与近平面上的任意一点相乘的结果第三行是n2,而第一、二行经过齐次坐标的变换后,能将x变为nx,y变为ny,与一开始的通用写法相同,此时第三行的n2显然跟前两行的数x、y没关系,所以这个矩阵可以写成:
![image-20210113220645752](https://i-blog.csdnimg.cn/blog_migrate/1f53a6436f02a43d8ef1cef7d73f8515.png)
我们再选取远平面的中心点,由于远平面的中心点在挤压之后也不会发生变化,x、y依然是0,z=f,所以同理可以写出矩阵:
![image-20210113222113486](https://i-blog.csdnimg.cn/blog_migrate/d8fdc812fbe0961d251f439ba499d18a.png)
联立刚刚的两个式子:
![image-20210113222431348](https://i-blog.csdnimg.cn/blog_migrate/cb1f16f35c7c98a402918573f16a429e.png)
我们可以解出A和B。
至此,我们把整个变换的矩阵全部填完了。
Do orthographic projection (Mortho) to finish
Mpersp= MorthoMpersp->ortho
补充:如何定义这个视锥?
Sometimes people prefer: vertical field-of-view (fovY) and aspect ratio
![image-20210114175611601](https://i-blog.csdnimg.cn/blog_migrate/1d287c3cbcf5de3a6f6b4308be3ae28e.png)
通常我们通过两个量来定义这个视锥:
①、垂直可视角度(field-of-view):如上图中红色虚线,摄像机到近平面上下中点所连成的线的夹角,即为垂直可视角度。
②、宽高比( aspect ratio):width/height。
How to convert from fovY and aspect to l, r, b, t?
![image-20210114180347598](https://i-blog.csdnimg.cn/blog_migrate/3f6c1f670a5048cdd37381bdf42019ba.png)
如何将刚刚定义的垂直可视角度(fovY)和宽高比(aspect)转化为已经定义好的 l, r, b, t?
我们从侧面去看这个视锥,假设最右边垂直的线是近平面,那么到摄像机的距离就是|n|,这时我们可以根据三角函数得出:
![image-20210114180639275](https://i-blog.csdnimg.cn/blog_migrate/ec762c28b02303145e44c957d8469a03.png)
宽高比即为左右的宽度与上下的高度的比值:
![image-20210114180842264](https://i-blog.csdnimg.cn/blog_migrate/e0a5363e80eeaa8285c28169732fe448.png)