about 图形学中从模型坐标系到世界坐标系,从世界坐标系到视图(摄像头)坐标系会经历的各种矩阵变换。同时还具体的讲了视图变换的过程。

属于图形学系列文章

一些基本概念

Points

$$ P = (P_{x}, P_{y}, P_{z}) $$

Vectors

$$ \boldsymbol{v}=\boldsymbol{P}-\boldsymbol{Q}=(\underbrace{P_{x}-Q_{x}}_{=v_{x}}, \underbrace{P_{y}-Q_{y}}_{=v_{y}}, \underbrace{P_{z}-Q_{z}}_{=v_{z}}) $$
$$ \begin{array}{l}{\text { Magnitude }|\boldsymbol{v}|=\sqrt{v_{x}^{2}+v_{y}^{2}+v_{z}^{2}}} \\ {\text { Direction (unit vector) } \hat{\boldsymbol{v}}=\boldsymbol{v} /|\boldsymbol{v}|}\end{array} $$

Lines (point $P$ + a vector $(Q - P)$)

$$ \begin{array}{l} {L(\alpha)=P+\alpha(Q-P)=\alpha Q+(1-\alpha) P}\end{array} $$

Planes

$$ \begin{aligned} \text { Three non-collinear points } P, Q, \text { and } R \text { define a plane } \\ S(\alpha)=& \alpha P+(1-\alpha) Q \\ T(\alpha, \beta)=& \beta S(\alpha)+(1-\beta) R \\=& \alpha \beta P+\beta(1-\alpha) Q+(1-\beta) R \\=& P+\beta(1-\alpha)(Q-P)+(1-\beta)(R-P) \\ \end{aligned} $$
$$ \begin{array}{l}{\text { Plane normal: } \boldsymbol{n}=(\boldsymbol{Q}-\boldsymbol{P}) \times(\boldsymbol{R}-\boldsymbol{P})} \\ {\text { Satisfies } \boldsymbol{n} \cdot \boldsymbol{P}=\boldsymbol{n} \cdot \boldsymbol{Q}=\boldsymbol{n} \cdot \boldsymbol{R}}\end{array} $$

Vector Operations: Addition, subtraction, Scaling, Dot (scalar) product, Cross (vector) product

Some properties: Linear dependency, affine sum, Convexity, convex hull

transformation

transformation 的本质是是一个函数,它将一个点映射到其它的点上。

Linear Transformation: 这类变换可以用矩阵乘法表示,满足 Preservation of zero points、Preservation of straight lines、Preservation of parallel lines、Does not necessarily preserve angles 的性质,常见的例子有 rotation, scaling, shearing

(Axis-Aligned) Scaling

$$ \boldsymbol{P}^{\prime}=\underbrace{\left(\begin{array}{ccc}{s_{x}} & {0} & {0} \\ {0} & {s_{y}} & {0} \\ {0} & {0} & {s_{z}}\end{array}\right) }_{=: S\left(S_{x}, S_{y}, S_{z}\right)}\boldsymbol{P} $$

Rotation

in 2D

$$ \left(\begin{array}{l}{x^{\prime}} \\ {y^{\prime}}\end{array}\right)=\underbrace{\left(\begin{array}{cc}{\cos \theta} & {-\sin \theta} \\ {\sin \theta} & {\cos \theta}\end{array}\right)}_{=: R(\theta)}\left(\begin{array}{l}{x} \\ {y}\end{array}\right) $$

in 3D

Around the Z axis:

$$ \left(\begin{array}{l}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}}\end{array}\right)=\underbrace{\left(\begin{array}{ccc}{\cos \theta} & {-\sin \theta} & {0} \\ {\sin \theta} & {\cos \theta} & {0} \\ {0} & {0} & {1}\end{array}\right)}_{=R_{z}(\theta)}\left(\begin{array}{l}{x} \\ {y} \\ {z}\end{array}\right) $$

Around the X axis:

$$ \left(\begin{array}{l}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}}\end{array}\right)=\underbrace{\left(\begin{array}{ccc}{1} & {0} & {0} \\ {0} & {\cos \theta} & {-\sin \theta} \\ {0} & {\sin \theta} & {\cos \theta}\end{array}\right)}_{=R_{x}(\theta)}\left(\begin{array}{l}{x} \\ {y} \\ {z}\end{array}\right) $$

任意的3D旋转可以表示成 Rx(θx)*Ry(θy)*Rz(θz)

Shear

性质:Changes the shape of the object / Generally do not preserve angles

in 2D

定义:Translate one coordinate of a point proportionally to the value of the other coordinate of the same point. 假设原坐标为 (x, y),则:

x-shear: (x, y + bx)

y-shear: (x + ay, y)

in 3D

定义:Translate all but one coordinate of a point proportionally to the value of that one coordinate. 假设原坐标为 (x, y, z),则:

z-shear: (x + az, y + bz, z)

$$ \left(\begin{array}{l}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}}\end{array}\right)=\left(\begin{array}{c}{x+a z} \\ {y+b z} \\ {z}\end{array}\right)=\left(\begin{array}{ccc}{1} & {0} & {a} \\ {0} & {1} & {b} \\ {0} & {0} & {1}\end{array}\right)\left(\begin{array}{l}{x} \\ {y} \\ {z}\end{array}\right) $$

Translation

$$ \left(\begin{array}{l}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}}\end{array}\right)=\left(\begin{array}{l}{x+t_{x}} \\ {y+t_{y}} \\ {z+t_{z}}\end{array}\right) $$

前面三个都是 linear 的变换,但 translation 是非线性的变换,它 does not preserve zero points -> not a linear transformation -> but is an affine transformation

What’s the meanning of affine transformation ?

linear transformation + translation: 满足 $\boldsymbol{q}=T(\boldsymbol{p})+\boldsymbol{r}$ 其中 $T$ 是线性的。

假设 P = (Px, Py, Pz),那么 affine transformation of P 可以表示为:

$$ \left(\begin{array}{l}{P_{x}^{\prime}} \\ {P_{y}^{\prime}} \\ {P_{z}^{\prime}}\end{array}\right)=\left(\begin{array}{lll}{a_{11}} & {a_{12}} & {a_{13}} \\ {a_{21}} & {a_{22}} & {a_{23}} \\ {a_{31}} & {a_{32}} & {a_{33}}\end{array}\right)\left(\begin{array}{c}{P_{x}} \\ {P_{y}} \\ {P_{z}}\end{array}\right)+\left(\begin{array}{l}{b_{1}} \\ {b_{2}} \\ {b_{3}}\end{array}\right) $$

除了 preserve zero points 外,其它 linear transformation 满足的性质它都满足。

What’s the meanning of rigid transformation ?

preserves angles and lengths、does not deform the object 如旋转和平移

affine transformation 包括的变换更广(包括 rigid transformation),但它的 deforms 有限制,需要 preserves collinearity and ratio of lengths,像缩放或剪切属于这一类

Homogeneous Coordinates

概念

我们常常在原来的矩阵或向量上再加一行,使之变成四维的齐次坐标。

$$ \left(\begin{array}{c}{P_{x}^{\prime}} \\ {P_{y}^{\prime}} \\ {P_{z}^{\prime}} \\ {1}\end{array}\right)=\left(\begin{array}{cccc}{a_{11}} & {a_{12}} & {a_{13}} & {b_{1}} \\ {a_{21}} & {a_{22}} & {a_{23}} & {b_{2}} \\ {a_{31}} & {a_{32}} & {a_{33}} & {b_{3}} \\ {0} & {0} & {0} & {1}\end{array}\right) \left(\begin{array}{c}{P_{x}} \\ {P_{y}} \\ {P_{z} \\ {1}}\end{array}\right) $$

(x, y, z, p) 这中表示方法不仅能将 affine transformation 转换为单纯的矩阵乘法,还可以用来区分向量和点。

Any point on the same vector has the same homogeneous coordinates. 这里是说在四维平面里,同一个向量上的点在三维坐标系里面是相同的点。If (x, y, z, w) is the homogeneous coordinate of a 3D point, then the 3D point is given by (x/w, y/w, z/w)

p = 0 代表向量,带入到平移矩阵,向量没有发生变化,正好符合向量没有位置的概念。

齐次坐标系下的各种变换

Translation:

$$ \left(\begin{array}{c}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}} \\ {1}\end{array}\right)=\left(\begin{array}{c}{x+t_{x}} \\ {y+t_{y}} \\ {z+t_{z}} \\ {1}\end{array}\right)=\left(\begin{array}{ccc}{1} & {0} & {0} & {t_{x}} \\ {0} & {1} & {0} & {t_{y}} \\ {0} & {0} & {1} & {t_{z}} \\ {0} & {0} & {0} & {1}\end{array}\right)\left(\begin{array}{l}{x} \\ {y} \\ {z} \\ {1}\end{array}\right) $$

Scaling:

$$ \left(\begin{array}{c}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}} \\ {1}\end{array}\right)=\left(\begin{array}{c}{S_{x}x} \\ {S_{y}y} \\ {S_{z}z} \\ {1}\end{array}\right)=\left(\begin{array}{ccc}{S_{x}} & {0} & {0} & {0} \\ {0} & {S_{y}} & {0} & {0} \\ {0} & {0} & {S_{z}} & {0} \\ {0} & {0} & {0} & {1}\end{array}\right)\left(\begin{array}{l}{x} \\ {y} \\ {z} \\ {1}\end{array}\right) $$

Rotation around Z:

$$ \left(\begin{array}{c}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}} \\ {1}\end{array}\right)=\left(\begin{array}{c}{x \cos \theta-y \sin \theta} \\ {x \sin \theta+y \cos \theta} \\ {z} \\ {1}\end{array}\right)=\left(\begin{array}{ccc}{\cos \theta} & {-\sin \theta} & {0} & {0} \\ {\sin \theta} & {\cos \theta} & {0} & {0} \\ {0} & {0} & {1} & {0} \\ {0} & {0} & {0} & {1}\end{array}\right)\left(\begin{array}{l}{x} \\ {y} \\ {z} \\ {1}\end{array}\right) $$

Z-shear:

$$ \left(\begin{array}{c}{x^{\prime}} \\ {y^{\prime}} \\ {z^{\prime}} \\ {1}\end{array}\right)=\left(\begin{array}{c}{x+az} \\ {y+bz} \\ {z} \\ {1}\end{array}\right)=\left(\begin{array}{ccc}{1} & {0} & {a} & {0} \\ {0} & {1} & {b} & {0} \\ {0} & {0} & {1} & {0} \\ {0} & {0} & {0} & {1}\end{array}\right)\left(\begin{array}{l}{x} \\ {y} \\ {z} \\ {1}\end{array}\right) $$

Coordinate System

Representation of a 3D point: basis vectors v1, v2, v3 + the origin r

可以表示一个点 / 向量

Change of Coordinates

组合变换

本质上是矩阵的乘法,但是我们需要注意矩阵的乘法不满足交换律。

Scaling About a Point

如果这个点是原点,很简单;不是原点呢?像下图这样:

我们可以把这个变换拆解:首先通过 T(-1,-1) 把中心点挪到原点,接着就是要求的操作 S(2, 2),最后我们再平移回去。这个过程可以表示成:T(1, 1)S(2, 2)T(-1, -1)P

Rotation

这块知识点非常重要!!!

about a fixed point

假设要求是 z-axis rotation of θ about its center Pf

拆解过程:

  1. 将 Pf 平移到原点 T(-Pf)
  2. 绕着z轴旋转 Rz(θ)
  3. 将 Pf 平移回去 T(Pf)

总结:M = T(Pf)Rz(θ)T(-Pf)

about an arbitrary axis

旋转轴这么定义,要求逆时针旋转θ(这么看来上面 about a fixed point 的情况是这个的特殊情况,旋转的轴是以 Pf 为起点,平行于z轴的向量):

拆解过程(假设P1坐标为 (x, y, z)):

  1. 首先使得向量u与z轴重合
    1. 将P1平移到原点 T(-x, -y, -z)
    2. 关于x轴旋转使得u在x-z平面 R1
    3. 关于y轴旋转使得u在z轴上 R2
  2. 按照要求绕u逆时针旋转θ R3
  3. Inverse transformation of R2, R1, Three

其中第1步的第2小步矩阵操作:

其中第1步的第3小步矩阵操作:

其中第2步矩阵操作:

总结:这个矩阵很重要!

Expressing Transformations

矩阵变换:P’ = RTP

global coordinate system

Right to left:a point P is first translated and then rotated

local coordinate system

Left to right:rotation first, then translation, then the point

两者都对,因为矩阵乘法是遵守结合律的。OpenGL 遵守 LOCAL COORDINATE SYSTEM,对于矩阵操作 P' = TRSP,OpenGL 的指令顺序:

glLoadIndentity()
glTranslate(…)
glRotate(…)
glScale(…)
DrawModel()

Loading, Pushing and Popping

维护一个全局矩阵作为全局状态,允许每个状态从相应的栈中 push / pop

glLoadmatrix(myarray)

glPushmatrix()

glPopMatrix()

glPushMatrix();
glTranslatef(…);
glScalef(…);
glPopMatrix();

View Transformation 视图变换

世界坐标系视图(摄像头)坐标系

一般情况下眼睛/摄像头在原点,我们观察的图像在z的负半轴,如下图所示:

但眼睛/摄像头不一定非要在原点,如下图:

那我们该如何转换呢?

What if N and V are not perpendicular?