Hi

I’ve got a quaternion and I need to apply it in QML/Qt to rotate an object to align with the original rotated object. What I have tried so far:

for the reference:

Qt Code:
  1. quaternion = {0.826351,-0.405726,-0.353097,0.166890}
To copy to clipboard, switch view to plain text mode 

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

Qt Code:
  1. public static double getPitch(Quat4d q)
  2. {
  3. return Math.atan2(2*(q.y*q.z + q.w*q.x), q.w*q.w - q.x*q.x -q.y*q.y +q.z*q.z);
  4. }
  5.  
  6. public static double getYaw(Quat4d q)
  7. {
  8. return Math.asin(-2*(q.x*q.z - q.w*q.y));
  9. }
  10.  
  11. public static double getRoll(Quat4d q)
  12. {
  13. return Math.atan2(2*(q.x*q.y + q.w*q.z), q.w*q.w +q.x*q.x - q.y*q.y - q.z*q.z);
  14. }
To copy to clipboard, switch view to plain text mode 

So I try QML:

Qt Code:
  1. Item {
  2. id: rectangle1
  3. width: 300
  4. height: 300
  5. scale: 1
  6. anchors.centerIn:parent;
  7.  
  8.  
  9. property double angleX: -61.87454134010583
  10. property double angleY: -26.624468050434572
  11. property double angleZ: 38.97955600504172
  12. property int originX: width/2
  13. property int originY: height/2
  14.  
  15. }
  16. transform: Rotation {
  17. origin.x: rectangle1.originX;
  18. origin.y: rectangle1.originY;
  19. axis { x: 0; y: 0; z: -1 }
  20. angle: rectangle1.angleZ }
  21.  
  22. Item {
  23. id: rectangle2
  24. width: 300
  25. height: 300
  26. scale: 1
  27. anchors.centerIn:parent;
  28.  
  29. transform: Rotation {
  30. origin.x: rectangle1.originX;
  31. origin.y: rectangle1.originY;
  32. axis { x: 0; y: -1; z: 0 }
  33. angle: rectangle1.angleY }
  34.  
  35. Rectangle {
  36. id: rectangle3
  37. width: 300
  38. height: 300
  39. color: "#ffffff"
  40. scale: 1
  41. opacity: 0.70
  42. anchors.centerIn:parent;
  43.  
  44. focus: true
  45.  
  46. transform: Rotation {
  47. origin.x: rectangle1.originX;
  48. origin.y: rectangle1.originY;
  49. axis { x: -1; y: 0; z: 0 }
  50. angle: rectangle1.angleX }
  51. }
  52. }
  53. }
To copy to clipboard, switch view to plain text mode 

I used 2 Items and 1 Rectangle to get the rotation right on the canvases, but the data 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 4×4 from OpenGL and I try:

Qt Code:
  1. void Dialog::paintEvent(QPaintEvent *e)
  2. {
  3. QPainter painter(this);
  4. painter.fillRect(50,50,100,100,Qt::red);
  5. painter.beginNativePainting();
  6.  
  7. 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};
  8.  
  9. glMatrixMode(GL_MODELVIEW);
  10. glLoadIdentity();
  11. glLoadMatrixd(modelview_matrix);
  12.  
  13. glColor3f(1,0.4,0.4);
  14. glPushMatrix();
  15. glPopMatrix();
  16.  
  17. painter.endNativePainting();
  18. }
To copy to clipboard, switch view to plain text mode 

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

Qt Code:
  1. void Dialog::paintEvent(QPaintEvent *e)
  2. {
  3. QPainter painter(this);
  4. painter.fillRect(50,50,100,100,Qt::red);
  5. painter.beginNativePainting();
  6.  
  7. QMatrix4x4 mat;
  8. mat.setToIdentity();
  9. const QQuaternion *q = new QQuaternion(0.826351,-0.405726,-0.353097,0.166890);
  10. mat.rotate(q->normalized());
  11.  
  12. painter.endNativePainting();
  13. }
To copy to clipboard, switch view to plain text mode 

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/...NativePainting 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

Qt Code:
  1. painter.fillRect(50,50,100,100,Qt::red);
To copy to clipboard, switch view to plain text mode 

declared earlier?

Another question I have is about the QMatrix4×4, 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?