I have an issue when my window resizes.

I have a simple MVP shader like this

gl_Position = projection * view * model * vec4(in_Position, 1.0);

to render a map in my game scene. The map is extremely simple that it simply offers a lane's vertex. And what I need to do is drawing it inside the window.

So I set up my MVP matrix individually, the "view" and "model" matrix remains the same because all I want to do is drawing a static map. "perspective" is the only matrix need to change since I want to have a zoom feature that I can zoom in/out by changing fovy. "perspective" is updated by calling

glm::perspective(glm::radians(fovy), (float)width/height, near, far) 

and my view matrix is calculated by

glm::lookAt(camera_pos, camera_pos + camera_front, camera_up)

However, when I resize the viewport, I find everything changed. I mean by default, the resolution gonna drop when my window goes larger if I didn't do anything(like updating the glViewPort()). But the behavior I wanna achieve is like

"I could see more part of the map when I made my window size larger".

Therefore I believe that I should keep my viewport resolution same (glViewPort(0, 0, screen_width, screen_height)). And updating perspective matrix by changing the ratio when new window's width and height comes in. Then the final step is to update the camera's position to update view matrix. However I stuck here for a while coz I don't know how to adjust it. I believe this is a common issue but I cannot even find a source talking about it.

Do I think it the wrong way? Do I need to change Camera's position to update my view matrix to achieve the resize behavior?

Platform: Ubuntu 16.04, OpenGL3.3 , C++14

  • \$\begingroup\$ OpenGL is not software. You should include what GPU you have in your platform. This is actually more relevant than your OS because, depending on your hardware manufacturer, you may have a driver bug. \$\endgroup\$ Nov 20, 2018 at 7:00

1 Answer 1


Do I think it the wrong way? Do I need to change Camera's position to update my view matrix to achieve the resize behavior?

You are correct. The purpose of the perspective matrix is to convert 3D camera space coordinates to 2D screen coordinates.

Take a look at this graphic of a viewing frustum: http://www.real3dtutorials.com/images/img00006.jpg

The frustum represents the viewable area (what you see on the computer screen) in world space where the eye is your camera. Every parallel plane between the near and far plane represents your computer screen, so you can picture the size of the world that is on the screen by imagining a cross section that roughly intersects your world / objects .

The frustum can be fully defined by the near and far planes, the field of view, and the camera position, so you have a few options to make more of your world visible, but only one is really viable.

If you were to adjust the fov, then you would certainly see more, but having a high fov will make the world distorted. The fov determines the degree at which objects shrink in the distance, so you could see more, but it needs to be similar to the field of view of the human eye in order to look realistic and not overly distorted.

Now look at the frustum again and imagine a plane inbetween the near and far plane that roughly intersects your world. You can move the near and far planes in either direction, but the size of the plane that crosses your world remains the same, so its position on the screen would be the same.

So the only real option is to move your camera back. By doing this, the entire frustum moves back, and the plane that roughly intersects your world is at a higher distance from the camera i.e. larger.

I know it isn't the clearest explaination, but let me know if you have any questions.


I should add that the viewport has nothing to do with size of the frustum (visible area); the viewport just maps the 2D information from the shader (which is in normalized device coordinates from [-1,1]) to pixels on the screen... Think of the viewport like the scale and offset of a 2D image on a page--the image scales and moves, but the content is the same.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .