2d Game techniques

lfalin
Unregistered
 
Post: #1
I'm looking for a site/tutorial/book to learn the best approach for 2D game programming in openGL. I know some basic openGL, however I'm not sure the best approaches for animating 2D sprite type graphics.

My game will have a pretty static background, with some dynamic information (scoreboard type stuff). There will be some sprites rendered on top of that I want the user to be able to manipulate via the mouse.

I know how picking works in openGL and was thinking to go that route for the manipulation, however I'm not sure of the best method for the sprite rendering, should I use textures or something else?

Thanks,
Lee
Quote this message in a reply
Member
Posts: 111
Joined: 2002.06
Post: #2
Textures would be the best way to draw sprites. Your sprite images need an alpha channel because OpenGL uses the alpha channel to avoid drawing transparent pixels.

I have articles and source code examples for OpenGL 2D programming at my website, which you can reach by clicking the link in my signature.

Mark Szymczyk
http://www.meandmark.com
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #3
Yeah, blazing fast 2D sprites are extremely easy to do with OpenGL using textured quads. Eventually you'll probably wonder if there is a faster way to draw the quads, but there really isn't. Just do the old standard glTexCoord2f, glVertex2f between glBegin(GL_QUADS) and glEnd. Using OpenGL for the picking should be just fine for mouse selection, although it is possible to do it faster yourself in 2d by calculating hit rectangles (I wouldn't bother though). For the textures, I prefer to mosaic a bunch of them on one image, say 1024x1024, which makes it compatible with video cards that can't deal with rectangle textures, like the Rage128 -- as if anyone really tries to make that card compatible anymore (eight years old?). Otherwise, you can load each sprite in as a single rectangle texture, and use GL_TEXTURE_RECTANGLE_EXT, instead of GL_TEXTURE_2D.

[edit] I should add that there are many restrictions to using rectangle textures though, such as: they can't be mip-mapped (not a problem for 2D stuff), and you can't do texture coordinate repeats (which might be an issue for 2D stuff depending on what you wanna do), among other restrictions. [/edit]
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
AnotherJake Wrote:For the textures, I prefer to mosaic a bunch of them on one image, say 1024x1024, which makes it compatible with video cards that can't deal with rectangle textures, like the Rage128 -- as if anyone really tries to make that card compatible anymore (eight years old?). Otherwise, you can load each sprite in as a single rectangle texture, and use GL_TEXTURE_RECTANGLE_EXT, instead of GL_TEXTURE_2D.

Apple sold machines with Rage 128 cards until mid-2001. In any case, loading many small textures into a bigger one allows faster rendering because that way you avoid texture switching for each sprite.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #5
PowerMacX Wrote:Apple sold machines with Rage 128 cards until mid-2001.
I was thinking in terms of when it came out, but that's a good point. When do we give up on the good ol' Rage? I say it can be safely considered obsolete.

Quote:In any case, loading many small textures into a bigger one allows faster rendering because that way you avoid texture switching for each sprite.
I wonder how much that affects performance in a practical sense. I do know that one major advantage to rectangle textures on the Mac is that uploads of the texture data are done via DMA (at least on an AGP system). Of course that might only be a major factor if you plan on doing lots of updates for something like software rendering, but still...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
AnotherJake Wrote:I do know that one major advantage to rectangle textures on the Mac is that uploads of the texture data are done via DMA (at least on an AGP system).

Maybe Arekkusu can chip in here, but I'm pretty sure that DMAing is independent of texture target...
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #7
OneSadCookie Wrote:Maybe Arekkusu can chip in here, but I'm pretty sure that DMAing is independent of texture target...
Yeah, it would be pretty nice to get some confirmation on this, because I'm pretty confident that GL_TEXTURE_RECTANGLE_EXT is (or at least *was*) the only AGP DMA path. Wouldn't be the first time I was smoking crack though... Rolleyes
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #8
Most hardware prefers to keep textures swizzled in VRAM for cache efficiency. Thus, even if the upload over the bus is DMA'd, there may still be a conversion step. Rectangle textures retain their linear layout, so if upload speed is a concern, use rectangle textures.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #9
So then it sounds like DMAing may indeed be independent of the texture target after all, but rectangle textures are essentially guaranteed the DMA upload speed benefit since they won't lose it by getting swizzled once they get to the card?
Quote this message in a reply
lfalin
Unregistered
 
Post: #10
Thanks everyone for the replies!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Screen-space techniques mattz 4 4,196 Nov 6, 2008 08:55 PM
Last Post: Bachus
  2D game techniques in OpenGL Wowbagger 7 8,878 Aug 5, 2006 05:06 PM
Last Post: akb825
  billboarding techniques ghettotek 2 3,437 Feb 16, 2003 10:28 PM
Last Post: OneSadCookie
  3D Animation techniques argument Louis! 12 5,489 Aug 9, 2002 10:26 AM
Last Post: monteboyd