[Solved] Stretching the screen when resizing?

I’m using LWJGL 3 for a 2d hacking simulator game. I want the screen to just be stretched upon resizing the window. How can this be done? I’ve looked at several posts on various forums and stackoverflow, but what everyone is saying seems to be outdated. I’m using version 3.1.2 Stable if that matters.

By “stretching the screen” you likely mean: Upon changing the aspect ratio of the viewport/window, you do not want the rendered objects to be stretched but keep their respective length ratios.
Or in short: No matter how you resize your window, a rendered unit square should always remain a unit square and not a stretched rectangle.

If so, then you can achieve this by changing the GL_PROJECTION matrix like so:


GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
float aspect = (float)windowWidth/windowHeight;
GL11.glScalef(1/aspect, 1, -1);
/* Do any other coordiante system transformations... */

or if you want to specify the coordinate system bounds (left, right, bottom, top), you can use glOrtho which gives the same result:


GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
float aspect = (float)windowWidth/windowHeight;
GL11.glOrtho(-aspect, aspect, -1, 1, -1, 1);

Also interesting to note is that all of the above is also equivalent to this:


GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
float aspect = (float)windowWidth/windowHeight;
GL11.glScalef(1/aspect, 1, 1);
GL11.glOrtho(-1, 1, -1, 1, -1, 1); // <- or any other bounds

However, now you can specify the visible bounds as if you had a square viewport and then still react to changing aspect ratios.

Now, if you do not want to use OpenGL’s matrix stack and instead use shaders, there are other Java matrix libraries to build matrices for sending to a shader. But in essence, they all do the same as the above.

If by “stretching the screen” you mean that the viewport should always fill the window’s client area, then do:


GL11.glViewport(0, 0, windowWidth, windowHeight);

Thanks for responding!

I think I figured out what I needed to do.