Jump to content

  • Log In with Google      Sign In   
  • Create Account

Maintaining aspect ratio in fullscreen mode

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1   Members   -  Reputation: 168


Posted 28 August 2012 - 09:31 AM

Hey guys.

I'm writing a game that has to take into account fullscreen and windowed modes. I'm basically using a fullscreen borderless window for the fullscreen mode. What I want to do for fullscreen mode is to get the user's desktop resolution, and create a viewport that has the same horizontal resolution as the user's desktop, and still maintain the aspect ratio. And to do this, I'm thinking of adding black borders to both sides of the screen which aren't filled. What I know is I need to use glViewport to set a viewport and glScissor to clip the outside of the viewport. But how to I set this up so that the viewport is centered on the screen? And if I do this, do I have to specify the coordinates in my game according to the offset created by the black bars on the sides? Or will OpenGL automatically map my coordinates inside the viewport's rectangle?

Help is very much appreciated.
Thank you very much. Posted Image

#2   Members   -  Reputation: 2027


Posted 28 August 2012 - 10:05 AM

0,0 is relative to the windows 0,0 and not the desktop. If you didn't know that already, I suggest you just start diving in to opengl. You don't need scissoring for the viewport. If you simply started opengl you would figure that out almost immediately. Not sure what centered on the screen means.

#3   Members   -  Reputation: 141


Posted 28 August 2012 - 12:53 PM

I'm not quite sure of the gist of what you are looking for, but maintaining aspect ratio shouldn't be that difficult. Supposing you have a onResize() method (or similar), that handles the window resize events, you can take the window width and divide it by window height (supposing the height != 0). That should give you the correct aspect ratio in all circumstances.

If you have, let's say, a square 1:1 ratio when windowed, and want to keep it that way and add black borders to the left and right edges, I suggest to render the scene to a texture in any case. Then, just render a square plane with that texture at the center of the world. Whenever the aspect ratio is changed, the "camera" will automatically add the black background for you. :)

#4   Members   -  Reputation: 274


Posted 28 August 2012 - 01:57 PM

Since you want to make your viewport width match your window width you just need to use the window's height and the desired viewport height to calculate what you should provide in the y parameter for your glViewport call:
x, y specify the lower left corner of the viewport.

Your game coordinates should be viewport relative unless you make them so that they aren't.

#5   Members   -  Reputation: 168


Posted 28 August 2012 - 11:12 PM

Hi guys, thanks for the replies!

I finally got what I wanted to do.

if (fullscreen) {
  GLfloat aspect = 4.0f / 3.0f;

  int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  int screenHeight = GetSystemMetrics(SM_CYSCREEN);

  int viewWidth = screenWidth;
  int viewHeight = screenWidth / aspect;

  if (viewHeight > screenHeight) {
   viewHeight = screenHeight;
   viewWidth = screenHeight * aspect;

  int vportX = (screenWidth - viewWidth) / 2;
  int vportY = (screenHeight - viewHeight) / 2;

  glViewport(vportX, vportY, viewWidth, viewHeight);

This results in the viewport's height matching the window's height, and the width will be adjusted to match the aspect ratio.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.