Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

3d - Qt Transform matrix

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here

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:

enter image description here

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...