Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Multiple Viewports and resize in QOpenGLWidget
Forum Updated to NodeBB v4.3 + New Features

Multiple Viewports and resize in QOpenGLWidget

Scheduled Pinned Locked Moved Game Development
viewportqopenglwidgetresize
4 Posts 2 Posters 3.1k Views 2 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.
  • A Offline
    A Offline
    ABloom
    wrote on 4 Sept 2015, 14:50 last edited by
    #1

    Hi there,
    I have a problem drawing multiple viewports on a QOpenGLWidget. Everything seems to work fine until i resize the window (after that the second viewport content disappears forever). Testbed: QT Examples --> Qt-5.4 --> Opengl --> cube
    changes to code:

    cpp:

    void MainWidget::resizeGL(int w, int h)
    {
        this->w = w;
        this->h = h;
    
        // Calculate aspect ratio
        qreal aspect = qreal(w) / qreal(h ? h : 1);
    
        // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
        const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
    
        // Reset projection
        projection.setToIdentity();
    
        // Set perspective projection
        projection.perspective(fov, aspect, zNear, zFar);
        cam_projection.perspective(fov, 1.0f, zNear, zFar);
    }
    //! [5]
    
    void MainWidget::paintGL()
    {
        // Clear color and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        texture->bind();
    
    //! [6]
        // Calculate model view transformation
        QMatrix4x4 matrix;
        matrix.translate(0.0, 0.0, -5.0);
        matrix.rotate(rotation);
    
        // Set modelview-projection matrix
        program.setUniformValue("mvp_matrix", projection * matrix);
    //! [6]
    
        // Use texture unit 0 which contains cube.png
        program.setUniformValue("texture", 0);
    
        // Draw cube geometry
        geometries->drawCubeGeometry(&program);
    
        glViewport(0,0,100,100);
    
        // Set modelview-projection matrix
        program.setUniformValue("mvp_matrix", cam_projection * matrix);
        geometries->drawCubeGeometry(&program);
    
        glViewport(0,0,w,h);
    }
    

    add in h:

        // new
        int w,h;
        QMatrix4x4 cam_projection;
    

    Any Help appreciated,

    Regards,
    Andrea

    A 1 Reply Last reply 4 Sept 2015, 16:29
    0
    • A ABloom
      4 Sept 2015, 14:50

      Hi there,
      I have a problem drawing multiple viewports on a QOpenGLWidget. Everything seems to work fine until i resize the window (after that the second viewport content disappears forever). Testbed: QT Examples --> Qt-5.4 --> Opengl --> cube
      changes to code:

      cpp:

      void MainWidget::resizeGL(int w, int h)
      {
          this->w = w;
          this->h = h;
      
          // Calculate aspect ratio
          qreal aspect = qreal(w) / qreal(h ? h : 1);
      
          // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
          const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
      
          // Reset projection
          projection.setToIdentity();
      
          // Set perspective projection
          projection.perspective(fov, aspect, zNear, zFar);
          cam_projection.perspective(fov, 1.0f, zNear, zFar);
      }
      //! [5]
      
      void MainWidget::paintGL()
      {
          // Clear color and depth buffer
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      
          texture->bind();
      
      //! [6]
          // Calculate model view transformation
          QMatrix4x4 matrix;
          matrix.translate(0.0, 0.0, -5.0);
          matrix.rotate(rotation);
      
          // Set modelview-projection matrix
          program.setUniformValue("mvp_matrix", projection * matrix);
      //! [6]
      
          // Use texture unit 0 which contains cube.png
          program.setUniformValue("texture", 0);
      
          // Draw cube geometry
          geometries->drawCubeGeometry(&program);
      
          glViewport(0,0,100,100);
      
          // Set modelview-projection matrix
          program.setUniformValue("mvp_matrix", cam_projection * matrix);
          geometries->drawCubeGeometry(&program);
      
          glViewport(0,0,w,h);
      }
      

      add in h:

          // new
          int w,h;
          QMatrix4x4 cam_projection;
      

      Any Help appreciated,

      Regards,
      Andrea

      A Offline
      A Offline
      ABloom
      wrote on 4 Sept 2015, 16:29 last edited by
      #2

      Update:

      tried with qt 5.5, same behaviour.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 4 Sept 2015, 19:39 last edited by
        #3

        QMatrix4x4::perspective multiplies the existing matrix by another. In the resetGL() method the original projection matrix is set to identity before perspective is applied, but you're not doing that for your cam_projection, so it gets cumulatively more and more distorted on every resize. Simply adding cam_projection.setToIdentity(); before applying perspective fixes the issue.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          ABloom
          wrote on 9 Sept 2015, 10:31 last edited by
          #4

          Thanks Chris. That solves the problem.

          1 Reply Last reply
          0

          1/4

          4 Sept 2015, 14:50

          • Login

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