using MFC, scene distorts on resize

I’ve followed francis shanahan’s turorial on setting up opengl with MFC.
My scene stretches to fit the window when it is resized, so it becomes distorted. What gl function can be used to adjust for a window resize?

You need to update aspect ratio with glFrustum() or gluPerspective() because the screen width and height are changed.


Well in the code in the link you’ve provided, the two lines affecting the distortion are:

glViewport(0, 0, cx, cy);  



The first one sets the size of the viewport according to the size of your window - cx sets the width, and cy sets the height.

And then in the second line, the (GLfloat)cx/(GLfloat)cy bit divides the width by the height to give the aspect ratio for the new viewport.

You could set the aspect ratio to a fixed size instead of re-calculating it every time (e.g. the aspect ratio of a 800*600 window is 1.33), but you would then also have to make sure that the cx and cy values you set for the viewport size are also at the same ratio.

Bear in mind, this might also give you a border at the top or the sides of your window, depending on how the user chooses to resize it.

A better way of solving it might be to simply fix the size of your window at a predefined size, so that the user can’t change it.

Or alternatively, you could write some code in your re-size routine, so that whenever the user adjusts the width of the window it also adjusts the height, to keep the window at always the same aspect ratio.

Dan. :wink:

So, if changing the aspect ratio distorts the scene, why would you ever want to change it?

No, that’s not correct.

Changing the aspect ratio does not distort the scene. Changing the aspect ratio does change the visible part of the scene however.

A distortion only results if the aspect ratio in the gluPerspective call is not the same as the real aspect ratio of the window. So for example if you change the window size without updating the projection matrix with the new aspect ratio, you’ll get a distortion.

Why is the viewport updating, but not the aspect ratio?