OpenGL/2D

Recommended Posts

GenuineXP    262
Hello! I know this sort of question has been asked A LOT, but I'm interested in using OpenGL for 2D rendering. More specifically, I want to be able to display animations, not just still images/textures. I'd like to be able to load images whose width and height aren't a power of 2 and use an alpha channel. Are there any good tutorials out there that will show me how to do this? I'm using SDL as well. Thanks! :-)

Share on other sites
superpig    1825
Have you taken a look at NeHe?

(If you tell us what you've already covered we'll be able to give you better replies)

Share on other sites
scgrn    168
Have a look at this thread.

Also, here's a good tutorial on setting up an OpenGL context with SDL.

Share on other sites
To use OpenGL with 2D rendering, just draw textured quads. I'm guessing that the way to animate a texture would be to load the frames of the texture as one gigantic filmstrip -- for a 10x10 texture that has 5 frames, you could expand it to a 50x10 filmstrip -- and then you could alter the texture coordinates according to time. For animation, though, I'd be sure to use OpenGL's capabilities; if you have a turret rotating on top of a 2D space ship, for example, don't create a 360 frame animation for all the possible rotations of the turret -- instead, separate the turret from the space ship, and then rotate the turret using glRotatef.
Also, gluBuild2DMipmaps accepts textures whose width and height aren't powers of two. I guess if its in 2D, you don't really need mipmaps, so it shouldn't be too hard to write some code that automatically chooses, for a given texture, the smallest power-of-two dimensions that can contain that texture, and then pads the texture accordingly.
As for setting up the projection matrix, here's some of my code:
glMatrixMode(GL_PROJECTION);glLoadIdentity();glScalef(2 / (float)width, 2 / (float)height, 0);glRotatef(180, 1, 0, 0);glTranslatef(GLfloat(width / -2), GLfloat(height / -2), 0);

This code sets the projection matrix such that (0, 0) is the top left corner of the screen, and (width, height) is the bottom right. You might be able to acheive the same effect with glOrtho in one line of code -- I'm not sure if my way is the best way.
Hope that helps!

Share on other sites
GenuineXP    262
Thanks for the replies! I'll be sure to check out those links.

I'm only a little familiar with OpenGL right now. I've never set it up using SDL before though. I'll take a look at that turorial; the SDL documentation has an example too, I think.

I have the book "Beginning OpenGL Game Programming", so I have a great reference for OpenGL. I haven't read too far into it yet, but it only briefly mentions setting up an orthographic matrix; the book's more concerned with 3D rendering, not 2D stuff! Texturing may be a problem too because I want to load images that don't necessarily have a width and height that are a power of 2. How do I use OpenGL extensions? Is gluBuild2DMipmaps included in glu.h?

The "filmstrip" idea is exactly what I'm going for. When I used SDL to render I did the same thing. Is just loading one giant texture with all of the frames the best way to go? If so, would I just change the texture coords to produce the animation?

Thanks again for the help. I know this is a common question.

Share on other sites
Cuzz    122
If you don't need rotation and scaling, look into glRasterPosi(x, y) and glDrawPixels(...).

Share on other sites
Ezbez    1164
Just to let you know, the alpha channel can be handled by glBlend(). If you aren't already, look into using SDL and SDL_image. They can both be used together with OpenGL. If you use SDL_image, you can load in .png or .tga files that have alpha channels, making it easy to use transparency.

Share on other sites
Nitrogen    154
Also, most graphics cards these days support non-power of 2 textures.
So you're pretty safe assuming you can use non-power textures, but whereever possible, try to stick to good ol' power of 2.

Share on other sites
GenuineXP    262
Cool, I had no idea that SDL_image could be used with OpenGL. SDL_image returns images in memory as SDL_Surface structures though... is there a way to use an SDL_Surface as an OpenGL texture?

I'm very interested in scaling and rotation; that's the primary reason I want to render with OpenGL and not SDL, so glRasterPoli() and glDrawPixels() probably won't be useful to me.

I guess I could just try using images with disregard for the power of 2 thing. If a graphics card doesn't support textures like this, I could just include alternate images that follow the rules. I have a GeForce 6600 GT graphics card. Would that support non-power of 2 textures?

Share on other sites
scgrn    168
Quote:
 Original post by GenuineXP...SDL_image returns images in memory as SDL_Surface structures though... is there a way to use an SDL_Surface as an OpenGL texture?...

This is shown in the second link I gave you.

Share on other sites
jonahrowley    300
You could use a higher-level library. I'm looking at Clanlib, it will do OpenGL accelerated sprites and such, and from the programmer's point of view, it's just a 2d sprite library (plus a lot, lot more). Don't be afraid to use high-level libs if they fit your needs, it means you can spend more time implementing game features, not engine features!