Qt 变换矩阵 [英] Qt Transform matrix

查看:64
本文介绍了Qt 变换矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 QMatrix4x4 操作 QML 项,以便应用一些透视变换.基本上,我定义了类 Transform 以使用对象 QMatrix4x4 作为 QML 项的转换字段的参数

class Transform : public QQuickTransform{Q_OBJECTQ_PROPERTY(QMatrix4x4 矩阵读取矩阵写入 setMatrix NOTIFY matrixChanged)民众:显式转换(QQuickItem *parent = 0);QMatrix4x4 matrix() const;void setMatrix(QMatrix4x4 矩阵);virtual void applyTo(QMatrix4x4 *matrix) const;信号:无效矩阵改变();私人的:QMatrix4x4 m_matrix;};

哪里

void Transform::applyTo(QMatrix4x4 *matrix) const {*矩阵 *= m_matrix;矩阵->优化();}

然而,似乎 QML 并没有以经典的方式定义"透视矩阵.我的测试主要集中在旋转上(

绕 y 轴的旋转如下所示:

希望有所帮助.

I need to manipulate QML items through QMatrix4x4, in order to apply some perspective transformations. Basically, I defined the class Transform to use an object QMatrix4x4 as argument for the transform field of the QML Item

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;

};

where

void Transform::applyTo(QMatrix4x4 *matrix) const {
      *matrix *= m_matrix;
       matrix->optimize();
}

However, it seems that QML doesn't "define" the perspective matrix in the classical way. I've focused my tests mainly on the rotations (http://en.wikipedia.org/wiki/Rotation_matrix). Let's say I have a QML Item in x:200, y:200 and I apply the transform

transform: [Transform{matrix:mytra},Rotation {  axis { x: 1; y: 0; z: 0 } angle: 90 } ]

where mytra is the identity matrix. The method applyTo() receives the (rotation) matrix

     1    -0.195312         0       200         
     0    -0.195312         0       200         
     0            0         1         0         
     0 -0.000976562         0         1  

but the "classical" one should be

     1    0         0       200         
     0    0        -1       200         
     0    1         0         0         
     0    0         0         1  

I don't understand where this matrix comes from and how to adapt my matrix to qt. Thanks for the help.

解决方案

The math Qt is performing is correct, but the frame of reference that Qt is using doesn't match up with what you are thinking.

Matrix Math:

So the components you are seeing in the math are adding shear in the x and y directions of image.

But the rotation that Qt does is about one of those axis. So if you want to do a standard 2D rotation, without shearing, you need to either not specify an axis or you specify the z axis.

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

The rotation about the y axis looks like this:

Hope that helps.

这篇关于Qt 变换矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆