LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: Quick example - how to make a resizable window  (Read 1613 times)

Evan407

  • Newbie
  • *
  • Offline Offline
  • Posts: 40
Quick example - how to make a resizable window
« on: April 14, 2016, 11:12:22 »

You probably know this code that makes the window resizable
Code: [Select]
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE,GLFW.GLFW_TRUE);// the window will be resizablebut that doesn't do the whole job because opengl is still using the old window size.

Here is how you can make opengl work with resizing the window.
Make a callback like so
Code: [Select]
  /* for resizing window */
  private static GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){
@Override
public void invoke(long window, int width, int height){
  GL11.glViewport(0,0,width,height);
  //update any other window vars you might have (aspect ratio, MVP matrices, etc)
}
  };

register the callback during init code

Code: [Select]
GLFW.glfwSetFramebufferSizeCallback(windowHandle, resizeWindow);
And that's it! Now you can resize the window properly.
Logged

kappa

  • Administrator
  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 1305
Re: Quick example - how to make a resizable window
« Reply #1 on: April 14, 2016, 11:54:14 »

Do keep in mind that on some platforms the window size is not always the same as the framebuffer size (such as retina macs).
Logged

Kai

  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 900
Re: Quick example - how to make a resizable window
« Reply #2 on: April 14, 2016, 13:53:07 »

It is pretty obvious that you have to reset the viewport when your window resizes.
Nothing new about that. And using the framebuffer resize event with its width/height is also fine under Retina.
Logged