OpenGL数学基础之矩阵

三维空间物体的运动使用变换来表示,包括三种:平移变换(translate),旋转变换(rolate),伸缩变换(scale),变换量可以使用4x4的矩阵(Matrix)表示,变换过程就是对物体的所有顶点都乘以这个矩阵。

矩阵乘法

[ 1 a b c 1 d e f 1 ] [ x y z ] = [ x + a ∗ y + b ∗ z c ∗ x + y + d ∗ z e ∗ x + f ∗ y + z ] \begin{bmatrix} 1 & a & b \\ c & 1 & d \\ e & f & 1 \\ \end{bmatrix} \begin{bmatrix} x \\ y \\ z \end{bmatrix}= \begin{bmatrix} x + a * y + b * z \\ c * x + y + d * z \\ e * x + f * y + z \end{bmatrix} 1cea1fbd1xyz=x+ay+bzcx+y+dzex+fy+z

矩阵乘法是行列向量相乘,所以行跟列必须是一样的矩阵才能相乘。

单位矩阵:
[ 1 0 0 0 1 0 0 0 1 ] \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} 100010001
根据上面的乘法公式可以知道,任何矩阵与单位矩阵相乘等于本身。

平移变换

假如有点(x, y, z),现在将其安向量 ( v 1 , v 2 , v 3 ) (v_1, v_2 ,v_3) (v1,v2,v3)平移,结果就为 ( x + v 1 , y + v 2 , z + v 3 ) (x + v_1, y + v_2, z + v_3) (x+v1,y+v2,z+v3),用矩阵表示如下:

[ 1 0 0 v 1 0 1 0 v 2 0 0 1 v 3 0 0 0 1 ] [ x y z 1 ] = [ x + v 1 y + v 2 z + v 3 1 ] \begin{bmatrix} 1 & 0 & 0 & v_1 \\ 0 & 1 & 0 & v_2 \\ 0 & 0 & 1 & v_3 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} = \begin{bmatrix} x + v_1 \\ y + v_2 \\ z + v_3 \\ 1 \end{bmatrix} 100001000010v1v2v31xyz1=x+v1y+v2z+v31

多出一行的原因是,但xyz都为0的时,矩阵乘法的结果总是为0,无法表示原点的平移;同时,4x4的矩阵可以连乘。

旋转变换

这里写图片描述
如图,圆为单位圆,已知点 ( x 1 , y 1 ) (x_1, y_1) (x1,y1),绕z轴旋转角度b,求该点旋转之后的坐标 ( x 2 , y 2 ) (x_2, y_2) (x2,y2),由图可得:

x 2 = c o s ( a + b ) = c o s ( a ) c o s ( b ) − s i n ( a ) s i n ( b ) = x 1 c o s ( b ) − y 1 s i n ( b ) x_2 = cos(a+b) = cos(a)cos(b) - sin(a)sin(b) = x_1cos(b) - y1sin(b) x2=cos(a+b)=cos(a)cos(b)sin(a)sin(b)=x1cos(b)y1sin(b)
y 2 = s i n ( a + b ) = s i n ( a ) c o s ( b ) + s i n ( b ) c o s ( b ) = y 1 c o s ( b ) + x 1 s i n ( b ) y_2 = sin(a+b) = sin(a)cos(b) + sin(b)cos(b) = y_1cos(b) + x_1sin(b) y2=sin(a+b)=sin(a)cos(b)+sin(b)cos(b)=y1cos(b)+x1sin(b)

所以用矩阵表示点(x, y, z)绕z轴旋转(xy平面内)角度b:

[ c o s ( b ) − s i n ( b ) 0 0 s i n ( b ) c o s ( b ) 0 0 0 0 1 0 0 0 0 1 ] [ x y z 1 ] = [ x c o s ( b ) − y s i n ( b ) x s i n ( b ) + y c o s ( b ) z 1 ] \begin{bmatrix} cos(b) & -sin(b) & 0 & 0\\ sin(b) & cos(b) & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} = \begin{bmatrix} xcos(b) - ysin(b) \\ xsin(b) + ycos(b) \\ z \\ 1 \end{bmatrix} cos(b)sin(b)00sin(b)cos(b)0000100001xyz1=xcos(b)ysin(b)xsin(b)+ycos(b)z1

绕y轴旋转(xz平面内)角度b:

[ c o s ( b ) 0 − s i n ( b ) 0 0 1 0 0 s i n ( b ) 0 c o s ( b ) 0 0 0 0 1 ] [ x y z 1 ] = [ x c o s ( b ) − z s i n ( b ) y x s i n ( b ) + z c o s ( b ) 1 ] \begin{bmatrix} cos(b)& 0 & -sin(b)& 0 \\ 0 & 1 & 0 & 0 \\ sin(b) & 0 & cos(b) & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} = \begin{bmatrix} xcos(b) - zsin(b) \\ y \\ xsin(b) + zcos(b) \\ 1 \end{bmatrix} cos(b)0sin(b)00100sin(b)0cos(b)00001xyz1=xcos(b)zsin(b)yxsin(b)+zcos(b)1

绕x轴旋转(yz平面内)角度b:

[ 1 0 0 0 0 c o s ( b ) − s i n ( b ) 0 0 s i n ( b ) c o s ( b ) 0 0 0 0 1 ] [ x y z 1 ] = [ x y c o s ( b ) − z s i n ( b ) y s i n ( b ) + z c o s ( b ) 1 ] \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & cos(b) & -sin(b) & 0 \\ 0 & sin(b) & cos(b) & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} = \begin{bmatrix} x \\ ycos(b) - zsin(b) \\ ysin(b) + zcos(b) \\ 1 \end{bmatrix} 10000cos(b)sin(b)00sin(b)cos(b)00001xyz1=xycos(b)zsin(b)ysin(b)+zcos(b)1

伸缩变化

任何一个矩阵乘以单位矩阵得到的值不变,如果我们将单位矩阵中的1比分别变为

( s 1 , s 2 , s 3 ) (s_1, s_2, s_3) (s1,s2,s3)

则可以得到:

[ s 1 0 0 0 0 s 2 0 0 0 0 s 3 0 0 0 0 1 ] [ x y z 1 ] = [ x s 1 y s 2 z s 3 1 ] \begin{bmatrix} s_1 & 0 & 0 & 0 \\ 0 & s_2 & 0 & 0 \\ 0 & 0 & s_3 & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} = \begin{bmatrix} xs_1 \\ ys_2 \\ zs_3 \\ 1 \end{bmatrix} s10000s20000s300001xyz1=xs1ys2zs31

这个矩阵变换表示点 ( s 1 , s 2 , s 3 ) (s_1, s_2, s_3) (s1,s2,s3)分别在xyz轴缩放

( s 1 , s 2 , s 3 ) (s_1, s_2, s_3) (s1,s2,s3)

变换组合

因为矩阵的乘法满足结合律:
M 1 ∗ M 2 ∗ . . . ∗ M n ∗ V = ( M 1 ∗ M 2 ∗ . . . ∗ M n ) ∗ V M1 * M2 * ... * Mn * V = (M1 * M2 * ... * Mn) * V M1M2...MnV=(M1M2...Mn)V
所以变换的组合可以将每个变换的分量相乘,最后再乘以这个顶点即可。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值