变换

本模块包含一个用于图形计算的Matrix类。目前我们支持:

  • 旋转、平移和缩放矩阵

  • 乘法矩阵

  • 裁剪矩阵(带或不带透视)

  • 3D 触摸的变换矩阵

关于变换矩阵的更多信息,请参阅 OpenGL 矩阵教程

在 1.6.0 版本发生变更: 新增了 Matrix.perspective()Matrix.look_at()Matrix.transpose() 方法。

class kivy.graphics.transformation.Matrix

基类:object

>>> from kivy.graphics.transformation import Matrix
>>> m = Matrix()
>>> print(m)
[[ 1.000000 0.000000 0.000000 0.000000 ]
[ 0.000000 1.000000 0.000000 0.000000 ]
[ 0.000000 0.000000 1.000000 0.000000 ]
[ 0.000000 0.000000 0.000000 1.000000 ]]

[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [ 12 13 14 15]

get(self)

将当前值作为扁平列表检索。

在 1.9.1 版本加入.

identity(self) Matrix

将矩阵重置为单位矩阵(原地操作)。

inverse(self) Matrix

返回该矩阵的逆矩阵,作为一个新的Matrix对象。

look_at(self, double eyex, double eyey, double eyez, double centerx, double centery, double centerz, double upx, double upy, double upz)

返回一个新的lookat矩阵(类似于`gluLookAt <http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml>`_)。

参数:
eyex:浮点数

眼睛X坐标

eyey: 浮点数

眼睛的Y坐标

eyez:浮点数

眼睛的Z坐标

centerx:浮点数

参考点的X坐标位置。

centery:浮点数

参考点的Y坐标位置。

centerz:浮点数

参考点的Z轴位置

upx:浮点数

X 值向上向量。

upy:浮点数

Y 值向上向量。

upz:浮点数

Z 值上向量。

在 1.6.0 版本加入.

multiply(mb, Matrix ma) Matrix

将给定的矩阵与自身(从左侧)相乘,即我们用当前矩阵左乘给定矩阵,并返回结果(非原地操作):

m.multiply(n) -> n * m
参数:
ma:矩阵

要乘以的矩阵

normal_matrix(self) Matrix

计算法线矩阵,该矩阵是用于将法线变换到眼/相机空间的左上角3x3模型视图矩阵的逆转置。

在 1.6.0 版本加入.

perspective(self, double fovy, double aspect, double zNear, double zFar) Matrix

创建一个透视矩阵(原地操作)。

参数:
fovy:浮点数

“视野”角度

aspect:浮点数

宽高比

zNear:浮点数

近裁剪面

zFar:浮点数

远裁剪平面

在 1.6.0 版本加入.

project(self, double objx, double objy, double objz, Matrix model, Matrix proj, double vx, double vy, double vw, double vh)

将三维空间中的点投影到二维视口中。

参数:
objx:浮点数

点的X坐标

objy:浮点数

点的Y坐标

objz:浮点数

Z 坐标

model:矩阵

模型矩阵

proj:矩阵

投影矩阵

vx:浮点数

视口X坐标

vy:浮点数

视口的 y 坐标

vw:浮点数

视口的宽度

vh:浮点数

视口高度

在 1.7.0 版本加入.

rotate(self, double angle, double x, double y, double z) Matrix

绕轴(x, y, z)按角度旋转矩阵(原地操作)。

参数:
angle:浮点数

矩阵旋转的角度。

x:浮点数

点的X坐标

y:浮点数

点的Y坐标位置。

z:浮点数

点的Z坐标位置。

scale(self, double x, double y, double z) Matrix

按每个维度指定的因子缩放当前矩阵(原地操作)。

参数:
x:浮点数

X 轴方向的缩放因子

y:浮点数

Y 轴方向的缩放因子

z:浮点数

沿Z轴的缩放因子

set(self, flat=None, array=None)

以扁平列表格式或如下所示的4x4数组格式,将自定义值插入矩阵中:

m.set(array=[
    [1.0, 0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0, 0.0],
    [0.0, 0.0, 1.0, 0.0],
    [0.0, 0.0, 0.0, 1.0]]
)

在 1.9.0 版本加入.

tolist(self)

以 numpy 格式检索当前矩阵的值。例如,m.tolist() 将返回::

[[1.000000, 0.000000, 0.000000, 0.000000],
[0.000000, 1.000000, 0.000000, 0.000000],
[0.000000, 0.000000, 1.000000, 0.000000],
[0.000000, 0.000000, 0.000000, 1.000000]]

你可以使用这种格式将结果直接插入到numpy中,方式如下:numpy.array(m.tolist())

在 1.9.0 版本加入.

transform_point(self, double x, double y, double z, t=None) tuple

通过矩阵变换该点,并返回变换后的点作为``(x, y, z)``元组。如果该点是一个向量``v``,则返回值为``v2 = matrix * v``。

如果提供了 t,它会将该值与矩阵的最后一列相乘,并返回变换后的 (x, y, z, t)

translate(self, double x, double y, double z) Matrix

翻译矩阵。

参数:
x:浮点数

X轴方向的平移因子

y:浮点数

Y轴方向的平移因子

z:浮点数

沿Z轴的平移因子

transpose(self) Matrix

返回转置矩阵作为一个新的Matrix。

在 1.6.0 版本加入.

view_clip(self, double left, double right, double bottom, double top, double near, double far, int perspective) Matrix

创建一个裁剪矩阵(原地操作)。

参数:
left:浮点数

坐标

right:浮点数

坐标

bottom:浮点数

坐标

top:浮点数

坐标

near:浮点数

坐标

far:浮点数

坐标

perpective:整数

坐标

在 1.6.0 版本发生变更: 启用对透视参数的支持。