Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Class QMatrix4x4
Qt 6.11 is out! See what's new in the release blog

Class QMatrix4x4

Scheduled Pinned Locked Moved Unsolved Qt 6
29 Posts 3 Posters 18.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by SGaist
    #2

    Hi,

    Unless mistaking, you should build a QMatrix4x4 out of your model view matrix before multiplying it with your own matrix.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Erija
      wrote on last edited by
      #3

      Hello again, I tried that but It didn't work.
      Is there any other solution ?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        What did you do ?
        What did not work ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Erija
          wrote on last edited by Erija
          #5
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            My suggestion was to create a QMatrix4x4 out of your GLfloat array.

            Something like

            QMatrix4x4 modelview(m);
            prod = matrix * modelview;
            

            Check the warning off the constructor.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • E Offline
              E Offline
              Erija
              wrote on last edited by Erija
              #7

              @SGaist okay now I get no error but there is another little problem.
              I'm trying to rotate my object using QQuaternion class: so here is what I did:
              First I defined my quaternion:
              GLWidget::GLWidget(QWidget *parent):QOpenGLWidget(parent)

              {

               quaternion= QQuaternion(w=1.0, x=0.0, y=0.0, z=0.0);
               quaternion.setScalar(w);
               quaternion.setX(x);
               quaternion.setY(y);
               quaternion.setZ(z);
              
              
              connect(&timer, SIGNAL(timeout()), this, SLOT(update()));
              
              timer.start(16);
              

              }

              then :

              void GLWidget::paintGL()
              {

              glClear(GL_COLOR_BUFFER_BIT);
              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
              glEnable(GL_BLEND);
              glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
              
              
              glMatrixMode(GL_MODELVIEW);
              glLoadIdentity();
              
              // camera transformation
              gluLookAt(6.0, 6.0, 6.0, 3.0,3.5,0.0, 0.0,1.0,0.0);
              glTranslatef(1.1, 2.1, -3.1);
              

              QMatrix4x4 matrix, prod;

                 GLfloat m[16];
                 //GLfloat matrix[16];
                 glGetFloatv (GL_MODELVIEW_MATRIX, m);
                 QMatrix4x4 modelview(m);
                 prod = matrix * modelview;
              
              
                 prod.rotate(quaternion);
              

              then I drew the object I want to rotate which is a parallelepiped.

              My mainwindow looks like this: ov.PNG
              where the 4 first values are w,x,y,z.

              When I change these values, the object is supposed to rotate but it does not.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Did you apply the newly calculated matrix ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  Erija
                  wrote on last edited by Erija
                  #9

                  @SGaist yes I applied it using the rotate function :
                  GLfloat m[16];
                  //GLfloat matrix[16];
                  glGetFloatv (GL_MODELVIEW_MATRIX, m);
                  QMatrix4x4 modelview(m);
                  modelview.rotate(quaternion);
                  update();

                  Well the rotation will be applied on the modelview matrix and logically the object should rotate when I change the values of w,x,y,z but that doesn't happen.

                  SGaistS 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Did you apply the newly calculated matrix ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      Erija
                      wrote on last edited by
                      #11

                      @SGaist Well I think I did; I used that function [ modelview.rotate(quaternion)].
                      Isn't it right ?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        I think there is a misunderstanding here.

                        You are modifying the local object that you created out of the data returned by glGetFloatv. Changing that as no influence whatsoever on the model view matrix actually used.

                        You have to load it back.

                        It's like when you want to modify a font on a widget. You retrieve the font, modify it and they apply it on the widget.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        E 1 Reply Last reply
                        0
                        • 2 Offline
                          2 Offline
                          23ing
                          wrote on last edited by
                          #13
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            Erija
                            wrote on last edited by
                            #14

                            @SGaist I'm sorry but what do you mean by saying that I'm modifying the local object created out of the data returned by glGetFloatv ?

                            1 Reply Last reply
                            0
                            • E Erija

                              @SGaist yes I applied it using the rotate function :
                              GLfloat m[16];
                              //GLfloat matrix[16];
                              glGetFloatv (GL_MODELVIEW_MATRIX, m);
                              QMatrix4x4 modelview(m);
                              modelview.rotate(quaternion);
                              update();

                              Well the rotation will be applied on the modelview matrix and logically the object should rotate when I change the values of w,x,y,z but that doesn't happen.

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @Erija said in Class QMatrix4x4:

                                    GLfloat m[16];
                                    glGetFloatv (GL_MODELVIEW_MATRIX, m);
                                    QMatrix4x4 modelview(m);
                                   modelview.rotate(quaternion);
                                  update();
                              

                              You rotate "modelview" which is built out of "m" that you populated with glGetFloatv.
                              That variable is decoupled from the OpenGL pipeline.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                Erija
                                wrote on last edited by
                                #16

                                @SGaist Modelview is built out of m and m contains the values of the modelview matrix.
                                In this line : QMatrix4x4 modelview(m); creates a 4x4 matrix from "m" so modelview(m) is the same as the modelview matrix. Am I right ??

                                By decoupled, you mean that OpenGL doesn't consider it as The modelview matrix, I mean the one used at the beginning for applying transformation such as rotation, translation and scaling ?

                                1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  I think there is a misunderstanding here.

                                  You are modifying the local object that you created out of the data returned by glGetFloatv. Changing that as no influence whatsoever on the model view matrix actually used.

                                  You have to load it back.

                                  It's like when you want to modify a font on a widget. You retrieve the font, modify it and they apply it on the widget.

                                  E Offline
                                  E Offline
                                  Erija
                                  wrote on last edited by
                                  #17

                                  @SGaist Yesterday you told me that I should load it back ? does it mean that I should use glLoadMatrix ?

                                  SGaistS 1 Reply Last reply
                                  0
                                  • E Erija

                                    @SGaist Yesterday you told me that I should load it back ? does it mean that I should use glLoadMatrix ?

                                    SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @Erija said in Class QMatrix4x4:

                                    @SGaist Yesterday you told me that I should load it back ? does it mean that I should use glLoadMatrix ?

                                    Yes, the way the OpenGL pipeline works is closer to my QFont example. The data you fetch from it is a copy of what is used not a reference to it.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • E Offline
                                      E Offline
                                      Erija
                                      wrote on last edited by
                                      #19

                                      I see but I tried to load the modelview matrix and the object didn't rotate.

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #20

                                        Can you show your code ?

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • E Offline
                                          E Offline
                                          Erija
                                          wrote on last edited by
                                          #21

                                          @SGaist Okay, Here is my code:

                                          GLWidget::GLWidget(QWidget *parent):QOpenGLWidget(parent)

                                          {
                                          quaternion = QQuaternion( w, x, y, z); // construction of the quaternion
                                          w=1.0;
                                          x=0.0;
                                          y=0.0;
                                          z=0.0;

                                          connect(&timer, SIGNAL(timeout()), this, SLOT(update()));
                                          
                                          timer.start(16);
                                          

                                          }
                                          //================================================================================================

                                          void GLWidget::initializeGL()
                                          {

                                          glClearColor(0.0f,0.0f,0.0f,1.0f); //background
                                          

                                          }

                                          void GLWidget::paintGL()
                                          {

                                          glClear(GL_COLOR_BUFFER_BIT);
                                          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                                          glEnable(GL_BLEND);
                                          glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                                          

                                          glMatrixMode(GL_MODELVIEW); // Modelview matrix used to apply transformations on the object
                                          glLoadIdentity();

                                          // camera transformation
                                          gluLookAt(6.0, 6.0, 6.0, 3.0,3.5,0.0, 0.0,1.0,0.0);
                                          glTranslatef(1.1, 2.1, -3.1); // trnaslate the elements of the scene to the center
                                                                        // of the screen
                                          

                                          quaternion.normalize();

                                          GLfloat m[16];

                                          glGetFloatv (GL_MODELVIEW_MATRIX, m);
                                          QMatrix4x4 modelview(m);
                                          modelview.rotate(quaternion); // applying the rotation: rotate function calculates
                                          //the rotation matrix that corresponds to the quaternion
                                          // then multiplies it by the modelview matrix

                                          glLoadMatrixf(modelview.constData());

                                          glEnable(GL_DEPTH_TEST);
                                          glEnable(GL_CULL_FACE);

                                             glBegin(GL_QUADS);
                                          

                                          [ Then I drew the object]

                                          void GLWidget::resizeGL(int wi, int h)
                                          {
                                          glViewport(0,0,wi,h);
                                          glMatrixMode(GL_PROJECTION);
                                          glLoadIdentity();
                                          gluPerspective( 45.0, (float)wi/h, 0.01, 100.0);

                                          update();
                                          

                                          }

                                          Logically the glLoadMatrix will replace the current matrix which is the old modelview matrix with the new modelview matrix after applying the rotation but that doesn't happen and when I run my code, the object disappears.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved