Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[OLD]QML/Qt (C++) Rotation using quaternion



  • Hello
    I've got a quaternion and I need to apply it in QML/Qt. What I have tried so far:

    for the reference: @quaternion = {0.826351,-0.405726,-0.353097,0.166890}@

    and I get Yaw, Pitch, Roll with (java):

       @ public static double getPitch(Quat4d q)
    

    {
    return Math.atan2(2*(q.yq.z + q.wq.x), q.wq.w - q.xq.x -q.yq.y +q.zq.z);
    }

    public static double getYaw(Quat4d q)
    {
    return Math.asin(-2*(q.xq.z - q.wq.y));
    }

    public static double getRoll(Quat4d q)
    {
    return Math.atan2(2*(q.xq.y + q.wq.z), q.wq.w +q.xq.x - q.yq.y - q.zq.z);
    }@

    So I try QML:

    @Item {
    id: rectangle1
    width: 300
    height: 300
    scale: 1
    anchors.centerIn:parent;

        property double angleX: -61.87454134010583
        property double angleY: -26.624468050434572
        property double angleZ: 38.97955600504172
        property int originX: width/2
        property int originY: height/2
    
        }
        transform: Rotation {
            origin.x: rectangle1.originX;
            origin.y: rectangle1.originY;
            axis { x: 0; y: 0; z: -1 }
            angle: rectangle1.angleZ }
    
        Item {
              id: rectangle2
              width: 300
              height: 300
              scale: 1
              anchors.centerIn:parent;
    
              transform: Rotation {
                  origin.x: rectangle1.originX;
                  origin.y: rectangle1.originY;
                  axis { x: 0; y: -1; z: 0 }
                  angle: rectangle1.angleY }
    
              Rectangle {
                    id: rectangle3
                    width: 300
                    height: 300
                    color: "#ffffff"
                    scale: 1
                    opacity: 0.70
                    anchors.centerIn:parent;
    
                    focus: true
    
                    transform: Rotation {
                        origin.x: rectangle1.originX;
                        origin.y: rectangle1.originY;
                        axis { x: -1; y: 0; z: 0 }
                        angle: rectangle1.angleX }
              }
        }
    }@
    

    I used 2 Items and 1 Rectangle to get the rotation right on the canvases, but the sata doesn't seem to fit.
    So I thought maybe I just don't know jack about Quaternions and I'm getting the wrong data out of it, so I have tried Qt and here I am right now - I found the beginNativePainting() in QPainter, but I don't fully understand it, and the example in the documentation doesn't even work.

    So I have a transformation matrix that is 4x4 from OpenGL and I try:

    @void Dialog::paintEvent(QPaintEvent *e)
    {
    QPainter painter(this);
    painter.fillRect(50,50,100,100,Qt::red);
    painter.beginNativePainting();

    double modelview_matrix[16]= {-0.874483, 1.527267, -2.199979, -0.760204, -0.576585, -0.299399, 0.000000, -0.078945, -0.375442, 0.923478, 0.000000, 0.644870, -0.725668, -0.239894, 0.000000, -0.095929};
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glLoadMatrixd(modelview_matrix);
    
    glColor3f(1,0.4,0.4);
    glPushMatrix();
    glPopMatrix();
    
    painter.endNativePainting();
    

    }@

    And I tried using the quaternion itself (which is very desired, small data etc.)

    @void Dialog::paintEvent(QPaintEvent *e)
    {
    QPainter painter(this);
    painter.fillRect(50,50,100,100,Qt::red);
    painter.beginNativePainting();

    QMatrix4x4 mat;
    mat.setToIdentity();
    const QQuaternion *q = new QQuaternion(0.826351,-0.405726,-0.353097,0.166890);
    mat.rotate(q->normalized());
    
    painter.endNativePainting();
    

    }@

    So I guess my question would be how does the QPainter::beginNativePainting() work, how do I make it work, because as I said even the example from http://harmattan-dev.nokia.com/docs/library/html/qt4/qpainter.html#beginNativePainting doesn't seem to work, and in my case nothing happens in both examples. Do I have to draw something inside the begin - end native painting and it takes an effect on that or should it work with the
    @painter.fillRect(50,50,100,100,Qt::red);@

    declared earlier?

    Another question I have is about the QMatrix4x4, I don't fully understand how it Identifies itself with setToIdentity, what does it identify itself with in my example even? Does it identify itself with the QPainter object?


Log in to reply