Aspect Ratio in OpenGL

I'm having problems with full screen mode. I can set my window to 800x600, but when I get this resolution full screen, it stretches. I suppose this is due to a change in aspect ratio. How can i fix this?

Edit # 1

Here is a screenshot of what I see.

image

Left: 800x600

Right: 1366x768

Edit # 2

My initGraphics function is called every time I resize a window (WM_SIZE).

void initGraphics(int width, int height) {
    float aspect = (float)width / (float)height;
    glViewport(0, 0, width, height);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND); //Enable alpha blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, height * aspect, 0.0);
    glMatrixMode(GL_MODELVIEW);
}
+2
source share
2 answers

SOLUTION: The real problem was that you used the gluOrtho2D function incorrectly. Instead of this:

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

You had to switch it to the correct form:

gluOrtho2D(0.0, width, 0.0, height);

, , .

:

, .

, glViewport . glMatrixMode, , , width / height gluPerspective. glFrustum gluPerspective, gluPerspective glFrustum.

- :

    float aspectRatio = width / height;
    glMatrixMode(GL_PROJECTION_MATRIX);
    glLoadIdentity();
    gluPerspective(fov, aspectRatio, near, far); 
+4

, . OpenGL, GL_PROJECTION, , glOrtho gluPerspective , ( , , ).

+2

All Articles