Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Transform matrix

Tags:

qt

3d

qt-quick

qml

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.

like image 403
lorenzolightsgdwarf Avatar asked Sep 18 '14 17:09

lorenzolightsgdwarf


1 Answers

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.

like image 88
phyatt Avatar answered Oct 15 '22 11:10

phyatt