OpenGL/2D

Started by
9 comments, last by jonahrowley 18 years ago
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! :-)
Advertisement
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)

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Have a look at this thread.

Also, here's a good tutorial on setting up an OpenGL context with SDL.
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!
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.
If you don't need rotation and scaling, look into glRasterPosi(x, y) and glDrawPixels(...).
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.
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.
Delphi C++ OpenGL Development: www.nitrogen.za.org
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?
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.

This topic is closed to new replies.

Advertisement