课程 图形学 Coordinate transformation
about 图形学中从模型坐标系到世界坐标系,从世界坐标系到视图(摄像头)坐标系会经历的各种矩阵变换。同时还具体的讲了视图变换的过程。
属于图形学系列文章。
一些基本概念
Points
Vectors
Lines (point P + a vector (Q−P))
Planes
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
Rotation
in 2D
in 3D
Around the Z axis:
Around the X axis:
任意的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)
Translation
前面三个都是 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: 满足 q=T(p)+r 其中 T 是线性的。
假设 P = (Px, Py, Pz),那么 affine transformation of P 可以表示为:
除了 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
概念
我们常常在原来的矩阵或向量上再加一行,使之变成四维的齐次坐标。
(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:
Scaling:
Rotation around Z:
Z-shear:
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
拆解过程:
- 将 Pf 平移到原点 T(-Pf)
- 绕着z轴旋转 Rz(θ)
- 将 Pf 平移回去 T(Pf)
总结:M = T(Pf)Rz(θ)T(-Pf)
about an arbitrary axis
旋转轴这么定义,要求逆时针旋转θ(这么看来上面 about a fixed point 的情况是这个的特殊情况,旋转的轴是以 Pf 为起点,平行于z轴的向量):
拆解过程(假设P1坐标为 (x, y, z)
):
- 首先使得向量u与z轴重合
- 将P1平移到原点 T(-x, -y, -z)
- 关于x轴旋转使得u在x-z平面 R1
- 关于y轴旋转使得u在z轴上 R2
- 按照要求绕u逆时针旋转θ R3
- 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?