Aspect Ratio Stretching in OpenGL

后端 未结 2 734
南笙
南笙 2021-01-03 11:46

I am having some trouble with full screen mode. I can set my window to be 800x600, but when I full screen that resolution, it stretches. I assume this is because of a change

相关标签:
2条回答
  • 2021-01-03 11:53

    SOLUTION: The real problem ended up being that you were misusing the gluOrtho2D function. Instead of using this:

    gluOrtho2D(0.0, width, height * aspect, 0.0);
    

    You needed to switch it to the correct form this:

    gluOrtho2D(0.0, width, 0.0, height);
    

    The latter creates a 2D orthographic projection, that fills the entire width and height of your viewport, so no stretching occurs.

    ORIGINAL ANSWER:

    You need to modify your projection in order to account for the new aspect ratio.

    Make sure you first of all set glViewport to the new window size. After the viewport is set you will need to switch your matrix mode to projection with a call to glMatrixMode and then finally calculate your new aspect ratio with width / height and pass the new aspect ratio to gluPerspective. You can also use straight glFrustum instead of gluPerspective you can find source to gluPerspective to achieve that same effect with glFrustum.

    Something like this:

        float aspectRatio = width / height;
        glMatrixMode(GL_PROJECTION_MATRIX);
        glLoadIdentity();
        gluPerspective(fov, aspectRatio, near, far); 
    
    0 讨论(0)
  • 2021-01-03 12:02

    After resizing the window, you need to adjust your projection matrix to reflect the new aspect ratio. If you're using classic OpenGL, switch the matrix mode to GL_PROJECTION, load the identity matrix, then call glOrtho or gluPerspective with the vertical dimension scaled by the aspect ratio (assuming you want the horizontal spread of the image to be the same as it was with the original window).

    0 讨论(0)
提交回复
热议问题