2D tutorial on OpenGL

Started by
6 comments, last by OrangyTang 16 years, 1 month ago
Greetings, I have been using SDL for quite a bit now. I really like that library, it's easy to use and has many functions and its sub-libraries are very handy. But I would like to try something more low-level. I thought of OpenGL, and found the NeHe tutorials, but those seem mainly aimed towards 3D applications. I am only interested in 2D at the moment. So I wanted to ask, is there a 2D-based tutorial on OpenGL that you would recommend? Thanks in advance.
Advertisement
OpenGL in itself doesn't handle anything other than graphics for you, you need to create the window, and handle input, yourself through other APIs then OpenGL. Since you already have some familairity with SDL, you should use that with OpenGL, as it's fairly easy to do with only a little hassle involved.

Using SDL with OpenGL

Then I'd look at this here: (It uses OpenGL and SDL for graphics)
Complete 2D Engine Overview
Particularily this section: Core Graphics

And you can always look at NeHe or other sites for how to use certain features.
I just can't seem to grasp OpenGL's ideology... Guess I'll have to take notes, but once I understand the basics, it'll be a piece of cake.

Anyway, here's another question: I have a very simple game in pure SDL. It uses my engine, which is basically a wrapper around the various SDL libraries. That version runs very well, but on one of my slower computers (a 400MHz P2 with ~300 megs'o'ram) it runs a bit... choppy. Will there be a speed increase if I implement OpenGL into the engine (and modify the game to use textures instead of surfaces)?

Thanks again.
Depending on the video card and drivers, you may see a significant speedup (especially if you are using blending).

You'll have to keep the limitations of older hardware (and general 3d hardware) in mind, though: no per pixel manipulations, no readbacks to CPU, and some older hardware may not support 32bit color (pre Riva TNT, and all 3dfx cards prior to 4000).

Last but not least, many drivers were problematic those days, so keep that in mind.

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

OpenGL is a 3D graphics API though it can manage 2D drawing there are others such as SDL which are designed for 2D.

The most difficult part of working with OpenGL is creating the window and rendering context and this can be different between OSes, a good rule of thumb is when reading through the code from OpenGL tutorials if it does not contain A statement beginning with gl or GL_ then it's not a OpenGL call but rather something to do with window set-up or input handling.

A easy to follow tutorial on openGL using glut to skip all the nasty parts such as setting up a window.

When I first encountered OpenGL I started by modifying source code generated by the IDE that copied with all the window set-up.

I also found because the main choice for choosing OpenGL a lot of books and tutorials skim over the window set-up section as it's different between OSes. The best source I found for tutorials on setting up a window on a windows platform was Direct3D (DirectX) as it is windows only they go into more detail on setting up a window.
Quote:The most difficult part of working with OpenGL is creating the window and rendering context
I find this semi true. My personal opinion is that the opengl states and using textures vs surfaces are more difficult coming from a 2d perspective than using any of the libraries such as GLFW, SDL or even GLUT for windowing as these use simple intuitive API calls whereas opengl can be a cruel mistress...

Some minor differences right off the bat that will take some fudging or getting used to coming from a pure 2d perspective:
1. screen coordinates are 0,0 in bottom left instead of upper left (easy fix).
2. texture mapping is not quite as easy (IMHO) as blitting (the benefits outweigh this though)
3. if you thought pixel manipulation was slow in SDL it gets worse in opengl however there are shaders and other workarounds that can make this multiples faster.
4. alpha blending vs colorkey - the different types of blending can be confusing and with scaling and rotation (although much faster fps wise) you will have to make some tweaks to your images and how you load them whereas colorkeys just ignored certain pixels so were never drawn when blitting

I totally made the move to 2d in 3d (via opengl however many concepts translate to any 3d API) and think the benefits far outweigh the pitfalls. I still don't understand vector math very well (you will want to learn at least some of this!) however even doing things in a very unoptimized way so you can feel more like old 2d there are noticeable performance boosts (especially when using alpha blending or rotation) to using a 3d API such as opengl on relatively modern hardware (modern = anything less than 5 or even 6 years old).

Sorry for the long post. Not sure why I replied in the first place. I must be really tired... :)
Evillive2
No use, I can't understand it... probably because my math isn't that good.

I find myself copying code off tutorials without understanding a single line. No, I'll wait until I'm a bit more ready, I'll keep using SDL until then.

Thanks all very much for your replies!
Quote:Original post by evillive2
1. screen coordinates are 0,0 in bottom left instead of upper left (easy fix).

If you pass suitable arguments to glOrtho then you can position the screen origin either bottom left or bottom right depending on what you prefer (or even another corner if you're weird).

This topic is closed to new replies.

Advertisement