4

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 in aspect ratio. How can I fix this?

Edit #1

Here's a screen shot of what I see happening.

image

Left: 800x600

Right: 1366x768

Edit #2

My initGraphics function gets called every time I re-size the 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);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
LunchMarble
  • 5,079
  • 9
  • 64
  • 94

2 Answers2

6

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); 
Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • don't forget a `glLoadIdentity()` or the projection matrix might apply the perspectives on top of each other – cobbal Jun 05 '12 at 05:34
  • I am using Ortho, but it's the same process. – LunchMarble Jun 05 '12 at 16:48
  • @Storm Kiernan so with glOrtho I thought you just say glOrtho(0, width, 0, height, near, far) and that will take care of all aspect ratio – Justin Meiners Jun 05 '12 at 19:23
  • @JustinMeiners but why I am I getting stretching? Is that normal? I am new to this, so I don't know exactly what I should be looking for here, but it seems that stretching would be bad. – LunchMarble Jun 05 '12 at 20:30
  • @Storm Kiernan No that is not normal, are you sure glOrtho is getting called when your window is resized? – Justin Meiners Jun 06 '12 at 22:11
  • @JustinMeiners Yes, I've updated my main post to show you the code that gets called every time WM_SIZE gets called. – LunchMarble Jun 07 '12 at 14:55
  • @Storm Kiernan Shouldnt gluOrtho2D(0, width, height * aspect, 0.0); just be gluOrtho2D(0, width, 0, height) I dont think you need to multiply the height by the aspect – Justin Meiners Jun 09 '12 at 22:22
  • @StormKiernan No problem, I have edited the answer to show the correct solution – Justin Meiners Jun 12 '12 at 04:07
3

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).

Drew Hall
  • 28,429
  • 12
  • 61
  • 81