Jump to content
  • Advertisement
Sign in to follow this  
acidleaf

OpenGL Maintaining aspect ratio in fullscreen mode

This topic is 2211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. smile.png

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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:
http://www.opengl.org/sdk/docs/man/xhtml/glViewport.xml
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!