View Full Version : Tweaking the projection matrix if viewport resizes

10-11-2008, 09:23 AM

usually, if the window size changes, you do two things to adjust the OpenGL rendering:

- Call glViewport() with the new client area size,
- set up a new projection matrix, e.g. using gluPerspective().

With this standard approach, the display of the rendering changes differently depending on whether you change the width or height of the window. Download e.g.


and start it in non-fullscreen mode, and shrink the window vertically. You'll notice that the rendered cube will be displayed smaller in order to still fit into the viewport. On the other side, if you shrink the width, the cube does not change its screen space size, and if the width is small enough, the cube will be cropped.

My question is, how do I need to change the matrix produced by gluPerspective() in order to get the same behavior for shrinking the window width as for shrinking the window height?

By accident, I found out that changing the -1 in the 3rd column, 4th row of the matrix as shown here


to S where

if (A<1.0) S=1.0/A;

seems to do the trick, but I have no explanation why this works. So my second question is, if the above is the right thing to do, why does it work?


10-13-2008, 09:41 AM
as far as i go, i update only the ratio. if using normal resolution like 640x480, 800x600, 1024x768, (not widescreen) ... they all produce a ratio of 4.0/3.0 = 1.3333.
since windowed mode can be whatever i guess just setting new ratio is enough otherwise the image is somewhat distorted (unproportional).

float aspect_ratio = new_width / new_height // in float.

10-13-2008, 01:25 PM
_NK47, I don't think it answers the question. This is an interesting question, but I can't find the answer for now...

10-15-2008, 07:36 AM
My question is, how do I need to change the matrix produced by gluPerspective() in order to get the same behavior for shrinking the window width as for shrinking the window height?

For projections as far as i understand the question, you just need correct aspect ratio of width and height so the image looks proportional. changing the viewport on window resize is enough then.
otheriwise i dont understand the problem.
maybe eyebex can explain it in more detail.

10-15-2008, 01:09 PM
_NK47: Have you downloaded the NeHe demo and played with it? I guess this is the best way to understand my question.

The problem is not that the image does not look proportional, that's all working fine. I just don't like that the projection behaves differently depending on whether you change the width or height. This is not (directly) related to the aspect ratio, which I'm calculating correctly.

10-17-2008, 01:39 AM
Yes i checked the sample and understand the problem but don't really know how to get it. my first guess would be not updating the glViewport??

Rosario Leonardi
11-05-2008, 09:55 AM
In the prospective matrix you have

1/(r*tan(fovY/2) 0 0 0
0 1/(tan(fovY/2) 0 0
0 0 (zN+zF)/zN-zF) 2*(zN*zF)/(zN-zF)
0 0 -1 2*(zN*zF)/(zN-zF)

zN is the near clip plane
nF is the far clip plane
fovY is the field of view in the Y axis
r is the screen ratio
and r*fovY is the field of view in the X axis (I'll call this fovX)

When you change the width of the window you change R so with a larger window you have a larger fovX. Larger window, larger fov, the image is the same size. :)
If you change the height you still changing the R.. but you are not changing the fovY.. you change fovX.. O_O You have an higher windows but the same fov, so your image look zoomed.

To avoid this you have to compute the new fovY as newWindowsHeight/oldWindowHeight.

sorry... I means
fovY = fovY*newWindowsHeight/oldWindowHeight

01-25-2009, 08:10 AM
Thanks Rosario, you pointed me into the right direction. Here's the code I finally came up with:

void onResize(int width,int height) {
static int const init_width=width;
static double const init_fov=math::Constd::PI()*0.25;

double fov;

if (width>=init_width) {
// Scale the FOV down in the same ration the width increased.
else {
// Scale the FOV so that for a zero window width it becomes PI.

m_camera.setProjection(math::Mat4d::Factory::Persp ectiveProjection(width,height,fov));