PDA

View Full Version : using MFC, scene distorts on resize



11-11-2005, 05:41 AM
I've followed francis shanahan's turorial on setting up opengl with MFC. http://www.francisshanahan.com/detail.aspx?cid=137
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?

songho
11-11-2005, 06:09 AM
You need to update aspect ratio with glFrustum() or gluPerspective() because the screen width and height are changed.

==song==

11-11-2005, 06:23 AM
Well in the code in the link you've provided, the two lines affecting the distortion are:

glViewport(0, 0, cx, cy);

and

gluPerspective(60.0f,(GLfloat)cx/(GLfloat)cy,0.1f,1000.0f);

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

11-12-2005, 07:09 PM
So, if changing the aspect ratio distorts the scene, why would you ever want to change it?

Overmind
11-13-2005, 02:32 AM
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.

11-13-2005, 06:28 AM
Why is the viewport updating, but not the aspect ratio?