# How to use modern OpenGL for 2D games?

I've found a plethora of "modern" OpenGL (3.0+) tutorials for 3D, but I have found next to nothing when looking for information on how to use it for 2D game development. How can I get started using OpenGL for 2D gamedev?

Specifically, I'm interested in getting answers to the following topics:

• How should I set up my various matrices for orthographic projection?
• Are shaders as heavily used in 2D applications as in 3D ones? If so, what is their purpose in the 2D setting?
• How should I handle the massive number of textures obviously required for a 2D game?

I apologize for the relatively broad question, but I've spent a long time searching and I've found very little useful information that applies to modern OpenGL.

• How should I set-up my various matrices for Orthographic projection?

Your goal is 2D, right? So you don't need any projection. Projection is the act of condensing a 3D world to a 2D plane. Set the shader so it defaults the Z coordinate of every vertex to 0.0 and you should be set.

You might use a scale matrix for the X and Y coordinates though. Scaling it so -10.0 to 10.0 becomes -1.0 to 1.0. For references sake, the matrix to do that is:

0.1   0.0   0.0
0.0   0.1   0.0
0.0   0.0   1.0

• Are shaders as heavily used in 2D applications as in 3D ones? If so, what is their purpose in the 2D setting?

They can be. They can still be used for Per-Pixel lighting, giving some fake depth to objects. They can also be used for texture effects, water effects, image-generation... you could even use them to do physics calculations with a bit of trickery.

Another way to put that is: Everything they are used for in 3D. It's just applied differently, and isn't as well documented. Though the people playing 2D games probably aren't going to be getting on your case if you don't have lifelike effects, so they're not required either.

Though you're going to need to know the basics of shaders to render a sprite anyway.

• How should I handle the massive number of textures obviously required for a 2D game?

Just as you do in 3D. Bunches of Texture Objects, PBO's, and a Texture Atlas or a few. Probably a few structures/classes, and headaches as-well. I think that would be called "Asset Management", or something similar.

• How can I get started with 2D gamedev?

There are probably a few tutorials out there. Though, since the 3D version is more common, I would say to learn OpenGL for 3D and then apply what you learn to it's 2D counterpart. 3D and 2D are very similar, 2D just doesn't keep track of the Z coordinate.

• For 2D, you could still use orthographic projection and different Z coordinates to do Z-buffering, otherwise you'd have to do the painter's algorithm yourself and draw in the right order. Sep 18, 2013 at 0:03
• @congusbongus actually, if you're spriting with transparency, you're most likely going to have to do the painter's algorithm yourself... Sep 18, 2013 at 1:50