49
\$\begingroup\$

I have been teaching myself the OpenGL library for a while now, and want to start making a game. However, for an easier introduction, I want to start with something 2D, such as a top down Pokemon-style game.

Is this a good plan, or is OpenGL made specifically for 3D?

\$\endgroup\$
2
  • 4
    \$\begingroup\$ Related: gamedev.stackexchange.com/questions/616/… and gamedev.stackexchange.com/questions/13180/… \$\endgroup\$
    – user1430
    Jul 15, 2013 at 16:30
  • \$\begingroup\$ I think that one benefit that using OpenGL for 2D has, is that you get the benefit of shaders. These could be useful for achieving special effects. Another benefit, is that if you already know OpenGL, you will already be familiar with the API. \$\endgroup\$
    – Lysol
    Feb 24, 2014 at 6:06

6 Answers 6

48
\$\begingroup\$

OpenGL is quite appropriate for 2D games. Although it is generally used for 3D, the same functionality can be used for 2D games. That is to say, anything you can do with 3D OpenGL will be applicable with "2D" OpenGL.

Some further information can be found at this location.

2D OpenGL is achieved in the same manner that 3D OpenGL is. 2D OpenGL is only the application of a technique, that is, rendering the scene on a flat plane, then using and orthographic projection instead of a perspective projection, which, depending on your setup, could distort the scene.

An example:

Ortho vs perspective

(a) is a scene in an orthographic projection. (b) is the same scene, using a perspective projection. In a 2D game, this can cause mis-positioning of sprites if their depth is altered (which, depending on how you go about development, may be the case). Using a perspective projection also makes aligning things in screen coordinates more difficult.

In addition, OpenGL carries many, many, many benefits with it. Primarily, hardware acceleration is a huge plus. There's also a much finer degree of control over what the graphics card does and how it goes about it, allowing for case-specific optimizations

\$\endgroup\$
5
  • \$\begingroup\$ Thankyou very much! I just wanted confirmation that it would be OK to do this. \$\endgroup\$ Jul 15, 2013 at 16:37
  • 4
    \$\begingroup\$ If you have the aptitude and interest to use OpenGL for 2D, it will bring you plenty of rewards that only OpenGL can give. Blistering performance, cross-platform compatibility... Think of it this way. Almost all other choices for 2D would be built on top of OpenGL anyway. It's a great opportunity to cut out some "middlemen" \$\endgroup\$
    – Steven Lu
    Jul 15, 2013 at 23:03
  • 2
    \$\begingroup\$ @StevenLu and not to mention, being slightly more of a real programmer \$\endgroup\$
    – Outurnate
    Jul 15, 2013 at 23:05
  • \$\begingroup\$ Great read! Thanks for linking that. Yup: coding your 2D with straight OpenGL is just more badass so that is why you should do it. \$\endgroup\$
    – Steven Lu
    Jul 15, 2013 at 23:33
  • 1
    \$\begingroup\$ You should provide more information to this answer rather than just a link. Maybe explain why GL works for 2D (orthographic views) and what advantages it brings (hardware acceleration, shader effects). \$\endgroup\$ Jul 16, 2013 at 21:23
23
\$\begingroup\$

OpenGL as the name states is a graphics library. Although it is developed with 3d in mind, there are no hardcoded assumptions in the library that you'll be using 3d. Moreover, thanks to it's 3d capabilities, several things that would be hard to do in a strictly 2d approach are trivial. Examples:

  • Z-sorting is simplified by using 3d coordinates the depth buffer (watch out for transparency though)
  • sprite rotation and scaling is basically for free as you're using textured quads that are independent of the resolution
  • if you're willing to restrict yourself to OpenGL 2.0 capable graphics cards you can take advantage of shaders, which will simplify writing special effects a lot (blurs, particle effects, image manipulation, color substitution) -- not to mention that it will be a lot faster than done CPU-side

This is only a few advantages that come from the top of my head, I guess there are several others.

\$\endgroup\$
2
  • 4
    \$\begingroup\$ Please do restrict yourself to OpenGL 2.0 (or greater) capable graphics cards, OpenGL 2.0 was released in 2004, even that is pretty outdated. \$\endgroup\$
    – Slicedpan
    Jul 16, 2013 at 8:32
  • 1
    \$\begingroup\$ @Slicedpan, unfortunately some laptops/netbooks still come with Intel cards that have GL 1.4 max support -- personally I decided to ditch them, but some people (especially when making simple games) might want to support them. \$\endgroup\$ Jul 16, 2013 at 13:59
17
\$\begingroup\$

While OpenGL is a 3D API, you can still use it to make 2D games. You can fake a 2D environment in 3D through various techniques, such as contraining data to a plane.

However, there is limited support in the actual API for doing this in an obvious fashion (there's no "turn on 2D mode" function, for example). The ways in which you'd simulate it (which you can find in many tutorials online) may be difficult to fully understand without some understanding of the 3D pipeline that is actually being used under the hood.

It sounds like you have been looking at OpenGL for a while and so probably have a decent grasp of those fundamentals. However, if you feel shaky on them you may want to start with your game by using a 2D library that is built on top of OpenGL, such as cocos2d. It will give you some experience with what 2D APIs generally should look like and still gets you the benefits of a hardware-accelerated 3D back-end, if you feel like you aren't quite ready to tackle the whole thing on your own.

\$\endgroup\$
1
5
\$\begingroup\$

Many 2d graphic libraries like SDL or SFML make use of OpenGL hardware acceleration. This gives you the performance benefit of OpenGL, but abstracts the nasty low-level details behind a much simpler API.

\$\endgroup\$
0
\$\begingroup\$

I now have a lot of experience in OpenGl programming, and I can tell you it is a pain. In the past when I started programming I was very productive in simple environments like BlitzBasic. Then I started to go into the next step with learning C++ and OpenGl. And when you do not have a good teacher or people to ask, you will permanently run into the black screen. The biggest problem is the state hell of OpenGl. There are tons of switches that you can swap to change the state of opengl. And some switches depend on other switches. for example glGet(GL_TEXTURE_BINDING_2D) depends on glGet(GL_ACTIVE_TEXTURE). And the documentation which state depends on which other state is not the best. Many operations like texture loading requires you to change this state which very often leads to the breakage of other code that requires the old state. And then there is this problem that OpenGl as implementation dependant behavior, so that your program runs on your machine but on the other machine all you see is black. All you can do here is guessing what is wrong in your 10000 lines of code. So if you like frustration then yes use OpenGl for 2D things. To all other people, don't do it if you don't need it. You can get productive in OpenGl, but it will cost you a lot of your time and nerves. Propably more than you want to spend.

\$\endgroup\$
-1
\$\begingroup\$

Unity3D uses OpenGL, and I use it for 2D and 2.5D platformers. Unity3D is essentially a 3D Gaming Engine, but is also is used for 2D and 2.5D games as well. You can have x, y, z planes and only limit the movement or rotation to 2 of those planes, creating a 2D atmosphere in essentially a 3D world.

\$\endgroup\$
7
  • \$\begingroup\$ Great, my first downvote. Glad to see gamedev has haters too. \$\endgroup\$ Jul 16, 2013 at 18:57
  • 1
    \$\begingroup\$ I guess illustrating a point through real world examples is frowned upon. Very professional. \$\endgroup\$ Jul 16, 2013 at 18:58
  • 7
    \$\begingroup\$ Amit, downvotes do not indicate "haters". Downvotes are simply a way for the community to convey their opinion of questions and answers. It's not personal to you, so try not to take it that way. I agree that the downvoter should have commented on the reason for the downvote, since you don't know how to improve the answer without feedback. I'll tell you why I think it was downvoted. Just because one individual uses OpenGL for 2D games, does not mean on a whole that it's appropriate. You also need to give reasons as to why it's appropriate. \$\endgroup\$
    – House
    Jul 16, 2013 at 20:19
  • 2
    \$\begingroup\$ Yeah, not sure why people are downvoting; the "uses OpenGL" bit I felt should be clarified with "uses OpenGL on platforms that require it" (it uses Direct3D or console-specific APIs everywhere but iOS/Anrdoid/OSX/Linux), but that's more a clarification than a correction. \$\endgroup\$ Jul 16, 2013 at 21:21
  • 1
    \$\begingroup\$ I didn't downvote myself, but another reason this answer is inappropriate is because you are using an OpenGL based engine, not raw OpenGL, which changes things quite lot. \$\endgroup\$ Jul 17, 2013 at 17:38

You must log in to answer this question.

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