GAMES101-现代计算机图形学入门-闫令琪——Lecture 04 Transformation Cont 学习笔记

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
2、Basic 3D Transformations
(1)、Scale
image-20210113193142891
(2)、Translation
image-20210113193247143
(3)、Rotation around x-, y-, or z-axis
image-20210113193343590

在这里,我们可以观察到沿y轴旋转的旋转矩阵中有两项(右上和左下的sinα)的符号被调换了,这是因为以右手螺旋定则来看的话,沿x轴(大拇指指向x轴)是从y转向z,沿z轴(大拇指指向z轴)是从x到y,而沿y轴是从z到x(而不是x到z),所以相应的符号也就反了。

3、3D Rotations

任意一个旋转都可以被分解成由绕x,y,z轴的三个旋转合成的一个旋转,因此

image-20210113194156074
  • So-called Euler angles(这也就是所说的欧拉角)

  • Often used in flight simulators: roll, pitch, yaw

    image-20210113194311163

但是使用欧拉角会产生万向锁的问题

4、Rodrigues’ Rotation Formula

Rotation by angle α around axis n

image-20210113194418822
二、Viewing transformation
(一)、View / Camera transformation
1、Perform View Transformation

Define the camera first

image-20210113195325021
  • 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

如果摄像机和场景中所有的东西一起移动的话,那么整个场景在摄像机中的画面还是相对保持不变。因此约定了将摄像机的位置 e ⃗ \vec{e} e 变换到坐标原点(0,0,0),将向上的方向 t ^ \hat{t} t^变换到Y轴,将摄像机朝向的方向 g ^ \hat{g} g^变换到-Z。让场景中的其他所有物体也随着摄像机的变换而变换。

3、Transform the camera by Mview

image-20210113201847823 image-20210113201906447

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

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

由于该旋转矩阵是正交矩阵,所以该矩阵的逆就是该矩阵的转置矩阵

image-20210113202900989

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 image-20210113203817617
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

对于正交投影,最简单的理解方式就是将所有三维空间的物体挤压到一个二维的平面,也就是将所有物体的z坐标直接去掉,也就实现了从三维到二维平面的映射。

(2)、In general

image-20210113211649220

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

先通过一个位移矩阵将长方体中心移动到原点,再通过一个缩放矩阵将长方体压缩成一个立方体。

2、Perspective projection
image-20210113213156767
  • Parallel lines not parallel; converge to single point
  • Further objects are smaller

How to do perspective projection↓

image-20210113213259819image-20210113213316993

  • 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

通过侧视图,对于视锥上任意一个点(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

此处我们根据齐次坐标的性质,将中间的式子同乘一个z,即得到最右边的式子,这就是原来的点通过变换后得到的新的点的坐标。此时z’我们依然不知道。

我们希望算出来到底是什么样的一个变换矩阵使得这个点的坐标变换成最右边的样子,也就是我们要求出矩阵Mpersp->ortho(4×4)

image-20210113215919970

此时可以先通过刚刚的已知量将这个矩阵的部分写出来:

image-20210113220116063

下面我们来取两个特殊的点。刚刚在一开始已经提到,近平面在挤压之后,任何一个点都不会变,远平面在挤压过程中,Z值不会变,也就是说挤压之后远平面的Z值依然是f(z=f)。

首先我们取近平面上任意一点,由于近平面在挤压之后,任何一个点都不会变,所以此时z就可以用n来替代,得到以下式子:

image-20210113220514740

所以这个矩阵与近平面上的任意一点相乘的结果第三行是n2,而第一、二行经过齐次坐标的变换后,能将x变为nx,y变为ny,与一开始的通用写法相同,此时第三行的n2显然跟前两行的数x、y没关系,所以这个矩阵可以写成:

image-20210113220645752

我们再选取远平面的中心点,由于远平面的中心点在挤压之后也不会发生变化,x、y依然是0,z=f,所以同理可以写出矩阵:

image-20210113222113486

联立刚刚的两个式子:

image-20210113222431348

我们可以解出A和B。

至此,我们把整个变换的矩阵全部填完了。

image-20210113223114113

Do orthographic projection (Mortho) to finish

Mpersp= MorthoMpersp->ortho

补充:如何定义这个视锥?

Sometimes people prefer: vertical field-of-view (fovY) and aspect ratio

image-20210114175611601

通常我们通过两个量来定义这个视锥:

①、垂直可视角度(field-of-view):如上图中红色虚线,摄像机到近平面上下中点所连成的线的夹角,即为垂直可视角度。

②、宽高比( aspect ratio):width/height。

How to convert from fovY and aspect to l, r, b, t?

image-20210114180347598

如何将刚刚定义的垂直可视角度(fovY)和宽高比(aspect)转化为已经定义好的 l, r, b, t?

我们从侧面去看这个视锥,假设最右边垂直的线是近平面,那么到摄像机的距离就是|n|,这时我们可以根据三角函数得出:

image-20210114180639275

宽高比即为左右的宽度与上下的高度的比值:

image-20210114180842264
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值