• 0

OpenGL window resize


Question

Hi guys, I'm a bit stumped. I've been following several online tutorials but havn't managed to find a fix for my problem.

I'm making a platform game, and when the window is resized everything needs to scale with it. At the moment everything scales, but proportionality is not kept.

I understand as it's only a 2d game I should be using GLOrtho instead of GLPerspective.

Here's my attempt:

void reshape(int width, int height){
    if (height == 0) height = 1; //Prevents a divide by zero.
    glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, (GLfloat)width / (GLfloat)height, 0.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
    //gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}

This code is called by :

glutReshapeFunc(reshape);

In the main method.

I am using freeGlut and running linux, IDE is netbeans if any of these details are relevant.

Thanks.

Link to comment
https://www.neowin.net/forum/topic/974220-opengl-window-resize/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

At the moment your code does no scaling and no stretching. OpenGL will render as much of the world as needed to fill the whole window. Proportionality of individual elements is kept because they are not stretched. Proportionality of the window itself is not kept of course.

So you want is to have a fixed aspect ratio and stretch it to fill the window? Then the most simple way is to not call gluOrtho2D in the reshape function. Call it once at program start, and on resize attempt, just adjust the viewport. As the projection matrix willl not change, the resulting image will have to be stretched.

This will lead to pixelisation if the user stretches the window a lot, but that doesn't really matter as you shouldn't be allowing the user to resize the window arbitrarily in the first place. If you want to control the aspect ratio, disable window resizing and offer an option in-game to change the resolution, like 99.999% of games do.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.