Qt Transform matrix

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

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.

回答1:

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 Transform matrix
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!