Jump to content
  • Advertisement
Sign in to follow this  
Takadimi

OpenGL Rendering 2D game w/ orthographic projection

This topic is 1241 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

So I've been tackling learning OpenGL for a little while now and I've been cobbling together a pong clone just as an exercise and I was wanting to get the opinions of people who are a lot more experience than me about projection in something simple like a 2D game.

 

Starting out I just used the [-1, 1] x [-1, 1] coordinate system with no projection matrices. As a result, I had to keep track of everything in my game (positions, velocity, etc.) with small floating point numbers and it wasn't the most intuitive thing in the world. I looked into switching to orthographic projection and just using the screen size as the bounds. So for an 800 x 600 screen the way I started mapping those values essentially changed to pixel operations.

 

This feels easier to work with and I can rationalize about the numbers more easily, but it seems like there is an obvious flaw: what happens when the screen size changes? Do you just scale every value relative to the screen size? Is this how it's done in most simple 2D games?

 

I know this is bottom of the barrell basics for most everyone here, but I'm still pulling my hair out over things like this and could use some guidance.

 

Thanks to anyone who takes the time out of there day to respond.

Share this post


Link to post
Share on other sites
Advertisement

If you resize the window you should resize the viewport aka glViewport() to fit the new screen size.

Regards the orthographic projection, this depends on the game you are working on.

In a pong clone you may want a fixed orthographic projection which never change - this will automatically scale up/down based on the viewport.

If you also want to preserve the aspect ratio you need to calculate the offsets and sizes yourself and pass this to the glviewport (Letterbox Aspect Ratio).

 

In some other games, you may update your orthographic projection to fit the window size, but these would require you just have to scale your game entirely yourself.

 

Modern games use mostly a perspective matrix and setup a camera to fit the level portion in, even in a 2D scenario because you dont have to deal with aspect ratio / stretching at all.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!