PDA

View Full Version : animating textures



bitwise
02-06-2005, 07:19 AM
I've got glCopyTexSubImage2D working in a "sprite" class I'm writing to make OpenGL 2D games. On SDL I can load an entire texture and only show a portion of it - that's how animation comes up. So if I have a bitmap that shows 3 balls (a blue one, a red one and a green one), I could draw an animation of a ball that changes color to the screen (just by choosing which part of the bitmap I want to draw), and I could use that color-changing ball as a normal ball in the game.

Ok, so that's only the concept of a "sprite" that not necessarily 3D programming folks would know about. I'm trying to do exactly that in OpenGL. I'm currently using glCopyTexSubImage2D as I said, but I just read that they cannot be included in display lists. I don't know the reason of this limitation, but I know from reading that display lists are great and they speed things up greatly.

I want my class to be include-able in display lists, but I also need a quick way to select a portion of a texture to draw, to be able to process animations. Is there any other way I can choose a part of a texture to be drawn that can be used in display lists?

dvm
02-06-2005, 07:33 AM
Have you thought about putting your texture on a quad or something and then animating the quad?

ZbuffeR
02-06-2005, 08:17 AM
You don't need to copy.

Contrary to what you think, animated textures are often used in 3d for explosions, fire, smoke, water, etc.

There are 2 ways, I recommend starting with the first one :

- cleaner but can be slower :
.. store each sprite in a different texture
.. use glBindTexture() with the correct ID to select the anim step wanted. This one fits in the display list.

- messy but can deliver more performance if you need it :
.. use only one large texture with all the animations steps in it, side by side, like in the good old ways.
.. to select a particular step of animation, just modify the texture coordinates with glTexCoord2f() to get only the part of the texture you want.
.. beware, you can have problems with filtering between sprites.

I hope it helps.

bitwise
02-06-2005, 10:57 AM
Originally posted by moucard:
Have you thought about putting your texture on a quad or something and then animating the quad?That's exactly what I do :)
I'm not using pixel routines, I'm all about GL_QUADS.


Originally posted by ZbuffeR:
- messy but can deliver more performance if you need it :
.. use only one large texture with all the animations steps in it, side by side, like in the good old ways.
.. to select a particular step of animation, just modify the texture coordinates with glTexCoord2f() to get only the part of the texture you want.
.. beware, you can have problems with filtering between sprites.Yes, I'd prefer the old way.
I actually thought about messing with glTexCoord2f (as I already use it), but you see, I only know how to use it in the range 0.0 - 1.0. I know I can scale my coordinates to fit inside this range, but I'm not sure I can trust the precision of a float. In my animations, I cannot have a texture single pixel displaced, or the whole animation wiggles.

I can see you know how sprites are done. In a 2-frames animation, if the second one is not drawn exactly where the first one was, the whole thing wiggles. That's the kind of insurance I need, and it would be very good if I could pass the proper coordinates (instead of scaled between 0 and 1) to glTexCoord2f.

Is there a way to do with such reliability?