iDevGames Forums
OpenGL Sprite Structure - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: OpenGL Sprite Structure (/thread-3978.html)

OpenGL Sprite Structure - Stalin55 - Aug 18, 2006 03:16 PM

I am working on a 2d isometric OpenGL(with which I'm fairly inexperienced in) engine using Carbon C, which renders tiles and sprites as textured quads. My trouble is, I can't seem to find any practical way to implement a sprite structure. With my old 2d engine I simply had a sprite sheet, containing all of the sprites for the respective character, which I would copy to a single GWorld. With OpenGL, it appears to me that I will have to load each individual frame as a seperate texture, which seems very impractical and time consuming. Is there any practical way to go about this?

OpenGL Sprite Structure - aarku - Aug 18, 2006 03:39 PM

As you are submitting vertex data to OpenGL you are also submitting texture coordinate information. You can simply change the texture coordinates so that your sprite (quad) is only displaying part of your texture. If 0..1 is your entire texture, you can do some arithmetic to figure out what the coordinates of a certain segment are.


OpenGL Sprite Structure - AnotherJake - Aug 18, 2006 03:40 PM

Stalin55: You can do essentially the same thing with OpenGL. You can put many sprites in one image and load it as a single texture. It'll probably be most logical to use something like 512x512 or 1024x1024 for your image size. Once the texture is loaded then all you have to do is do glBindTexture to the textureName (or ID, as I call it) of that group of sprites. Then when you draw your quads you use coordinates that correspond with the particular portion of the texture you want to use, which would be the coordinates for the rectangle surrounding a particular sprite. I'll assume you'll be using GL_TEXTURE_2D... For simple tutorials with OpenGL it might seem like you'd always use coordinates like 0.0f, 0.0f for the lower left of the texture and 1.0f, 0.0f for the lower right of the texture. For your purposes, you can define those anywhere in-between 0.0f and 1.0f (or even outside of 0-1 if you wanted to for some reason). Something like 0.0f, 0.5f would only be referring to half of the image. You can use any numbers you want to zoom in on any particular area of a texture. This is hard to explain in words. Consult the Red Book for examples, or experiment a little yourself.

[edit] darn it, aarku just barely posted while I was previewing. Oh well, I'll leave it for the sake of repetiveness.