Resizing window results in wrong aspect ratio


I’m having issues when resizing my window with glfwSetWindowSize(). When I create my window context with glfwCreateWindow(width, height, “MyWindow”, NULL, NULL) everything looks fine, but after calling glfwSetWindowSize(newWidth, newHeight) the window aspect ratio looks wrong, the image looks stretched. I’ve also tried using glfwSetWindowAspectRatio(newWidth, newHeight) after calling glfwSetWindowSize(newWidth, newHeight) but this seems to have no effect.

If instead of resizing the window context I delete it with glfwDestroyWindow() and then re-create it with glfwCreateWindow(newWidth, newHeight) the window looks correct, but this approach is far too slow, since the OpenGL state has to be re-initialized after calling glfwCreateWindow(), and the window is resized frequently during rendering.

For example, the following works but is far to slow…

void OnWindowResize() {
    m_window = glfwCreateWindow(newWidth, newHeight, "MyWindow", NULL, NULL);

This runs much faster, but the image aspect ratio looks wrong…

void OnWindowResize() {
    glfwSetWindowSize(m_window, newWidth, newHeight);
    glfwSetWindowAspectRatio(m_window, newWidth, newHeight);

Does anyone know what I am doing wrong here? How do I resize the window without messing up the aspect ratio? Thanks for the help!

I’m using GLFW version 3.2.1 on Windows 10 with a GeForce GTS 960M driver 417.35


Are you updating your projection transform and viewport to reflect the new window size?


Yes. I call glm::ortho() to set up the projection transform and glm::lookAt() the set up the view transform after the glfwSetWindowSize() call. The code path is identical after the OnWindowResize() sample above. I’m not sure what you mean by ‘update the viewport’. Is there a OpenGL or GLFW call I am missing after the resize?


The viewport is set with glViewport. While it is set correctly by OpenGL at context creation, it is up to you to keep it up-to-date after that.


That fixed it thanks!

Part of my confusion was from a note in the documentation. I noticed that the GLFW documentation, under glfwSetWindowSize() says…

“Do not pass the window size to glViewport or other pixel-based OpenGL calls. The window size is in screen coordinates, not pixels. Use the framebuffer size which is in pixels, for pixel-based calls.”

So your suggestion worked, but I’m still confused by this note in the documentation. Am I supposed to get the input values to glViewport from the framebuffer or convert the screen coordinate to pixel coordinates somehow?



I suggest using glfwGetFramebufferSize or glfwSetFramebufferSizeCallback to get the correct values.