我需要通过 QMatrix4x4 操作 QML 项目,以便应用一些透视变换。基本上,我将类 Transform 定义为使用对象 QMatrix4x4 作为 QML Item 的 transform 字段的参数
class Transform : public QQuickTransform{
Q_OBJECT
Q_PROPERTY(QMatrix4x4 matrix READ matrix WRITE setMatrix NOTIFY matrixChanged)
public:
explicit Transform(QQuickItem *parent = 0);
QMatrix4x4 matrix() const;
void setMatrix(QMatrix4x4 matrix);
virtual void applyTo(QMatrix4x4 *matrix) const;
signals:
void matrixChanged();
private:
QMatrix4x4 m_matrix;
};
在哪里
void Transform::applyTo(QMatrix4x4 *matrix) const {
*matrix *= m_matrix;
matrix->optimize();
}
然而,QML 似乎并没有以经典的方式“定义”透视矩阵。我的测试主要集中在旋转(http://en.wikipedia.org/wiki/Rotation_matrix)上。
假设我在 x:200, y:200 中有一个 QML 项目,我应用了转换
transform: [Transform{matrix:mytra},Rotation { axis { x: 1; y: 0; z: 0 } angle: 90 } ]
其中 mytra 是单位矩阵。方法 applyTo() 接收(旋转)矩阵
1 -0.195312 0 200
0 -0.195312 0 200
0 0 1 0
0 -0.000976562 0 1
但“经典”的应该是
1 0 0 200
0 0 -1 200
0 1 0 0
0 0 0 1
我不明白这个矩阵来自哪里以及如何使我的矩阵适应 qt。
谢谢您的帮助。
最佳答案
Qt 执行的数学运算是正确的,但 Qt 使用的引用框架与您的想法不匹配。
矩阵数学:
因此,您在数学中看到的组件在图像的 x 和 y 方向上添加剪切。
但是 Qt 所做的旋转是关于这些轴之一的。因此,如果您想进行标准的 2D 旋转而不进行剪切,则需要不指定轴或指定 z 轴。
The axis to rotate around. For simple (2D) rotation around a point, you do not need to specify an axis, as the default axis is the z axis
(axis { x: 0; y: 0; z: 1 })
.
http://qt-project.org/doc/qt-5/qml-qtquick-rotation.html
绕 y 轴的旋转如下所示:
希望有帮助。
关于Qt 变换矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919378/