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. QOpenGLWidget content stretching on resize
Forum Updated to NodeBB v4.3 + New Features

QOpenGLWidget content stretching on resize

Scheduled Pinned Locked Moved Unsolved Game Development
2 Posts 2 Posters 1.8k 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.
  • C Offline
    C Offline
    calier
    wrote on last edited by calier
    #1

    Hi !

    I'm having some issues keeping a fixed aspect ratio for the content of my QOpenGLWidget.
    I'm trying to get my "scene" with a fixed aspect ratio adding Horizontal or Vertical "empty" areas next to my image when necessary. (please see example image to illustrate the issue below)
    I got this to work fine with GLFW but for some reasons I can't get it to work in Qt. It almost looks like my layout stretches the QOpenGLWidget no matter what my resizeGL method does.

    0_1518907411200_QOpenGLWidget_issue1.png

    0_1518907427082_QOpenGLWidget_issue2.png

    0_1518907434720_QOpenGLWidget_issue3.png

    Code:

    void GLViewer::resizeGL(int w, int h)
    {
        if (h == 0) h = 1; 
        GLfloat aspectRatio = (GLfloat)w / (GLfloat)h;
    
        glViewport(0, 0, w, h);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity()
    
        if (w >= h) {
           glOrtho(-0.5 * aspectRatio, 0.5 * aspectRatio, -0.5, 0.5, 0.1, 100);
        } else {
          glOrtho(-0.5, 0.5, -0.5 / aspectRatio, 0.5 / aspectRatio, 0.1, 100);
        }
        glMatrixMode(GL_MODELVIEW);
    }
    

    Any help would be greatly appreciated. Thanks !

    1 Reply Last reply
    0
    • johngodJ Offline
      johngodJ Offline
      johngod
      wrote on last edited by
      #2

      Probably is just something that is messed up with with your multiplcation or division on the aspect ratio.
      I'm just too lazy right now to think about it properly, so I just goint to copy paste one example of my that I know it works, so can try to adapt your code.

      if (width() <= height())
              projection.ortho(-(Range),Range,-Range*h/w,Range*h/w,-(Range*2),Range*2);
      else
              projection.ortho(-(Range*w/h),Range*w/h,-Range,Range,-(Range*2),Range*2);
      
      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