QOpenGLWidget content stretching on resize
-
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.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 !
-
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);