2D sprite animation: one or more textures?

Started by lorcán, June 06, 2009, 09:12:47

Previous topic - Next topic

lorcán

Hi there.

I wonder what could be the "best" method to handle 2D sprite animations (like walking characters). Each specific animation is stored in a single image, like these 10 frames :



This means the image contains all the frames of a single animation.

Now I wonder if it's better :
1) to split all the n frames in n textures
or
2) to keep the whole image in a single texture and shift texture coordinates in real-time
or anything else... :P

Thanks  ;)

broumbroum

I'm using 2D GL_Texture's for EACH sprite in every Animation. This works very well, raising ~30 FPS with LWJGL. Of course, the WHOLE animation must be loaded in GL cache before to use it.
Then it's possible to make 1000+ sprites in cache and play at 30 FPS. Notice that OpenGL Texture's are automatically cached and lift up in the VRAM as needed by the GL Thread !

Hence it is quite easy. Animation's would handle Texture "int" bindings in an usual array of int[]'s. :D

lorcán

OK

QuoteHence it is quite easy. Animation's would handle Texture "int" bindings in an usual array of int[]'s. :D

Or maybe using a main single collection (hashmap or so) of java objects (each of these objects is a "human" object representation of a texture, and therefore contains the "int" binding of this existing texture in VRAM and a texture name as String...). So the animation would rather handle a table of string names, which would be the key in the main collection of java "texture" objects. I'm afraid that using the int values instead are a bit dangerous (after deleting a texture, and so on). What do you think?



broumbroum

That's right, deleting a texture would require to update the array. For I'm not telling you the exact manner, it is clearly the best way yours.
Let's say a collection of texture would imply 2 key-bindings : one for an unique identifier for the texture and the second (let it be the value of each mapping) be the OpenGL binding.
As the Texture is deleted, the corrsponding mapping must be cleared off.
I'm using for myself hashCode() and openGL bindings in a static HashMap that is updated each time it is needed.

One thing really I'm sure : storing int's--hashCode's are and bindings so they are--is faster than running over String's. BTW String's have unique hashCode's too.
At least you can (watch out you have to re-define hashCode() with correct values--e.g. System.nanoTime()) specify each texture as Object's with specific parameters such as size or color mode.

lorcán

QuoteI'm using for myself hashCode() and openGL bindings in a static HashMap that is updated each time it is needed.

One thing really I'm sure : storing int's--hashCode's are and bindings so they are--is faster than running over String's. BTW String's have unique hashCode's too.
At least you can (watch out you have to re-define hashCode() with correct values--e.g. System.nanoTime()) specify each texture as Object's with specific parameters such as size or color mode.

This interests me! Do you have some code samples? (eg your texture class and other related classes)

BTW aren't you French-speaking? ;)

broumbroum

Je parle en Français, en effet. :)

Voici le post que j'avais mis précédemment : http://lwjgl.org/forum/index.php/topic,2886.msg15921.html#msg15921
(LWJGL est en Anglais donc je continue en Anglais...)
Well I think you're looking for the post where I'm providing my texture loading classes : http://lwjgl.org/forum/index.php/topic,2886.msg15921.html#msg15921

More about JXA API : http://apps.sourceforge.net/phpbb/sf3jswing/ .
Coming next : Full JXA integration in THE GAME applet. ::)

Ciardhubh

Some sprite animations benefit from using a 3D texture, i.e. put frames on top of each other in a single 3D texture and animate by adjusting the R texture coordinate. This has the advantage that you can get linear filtering between frames.

It depends on the type of animation how it will turn out, though. Something like a character animation would look rather bad but effects like explosions, fire, smoke, etc. look very smooth. You can get away with fewer frames or play an animation slower without stuttering.

Edit:
I've been experimenting with this a bit lately. If you want to see how it looks in comparison, try this:
http://ciardhubh.de/download/node/19/3DTextureAnimation.jnlp (Webstart)

The top two quads are textured using an array of individual 2D textures. The lower two are textured using one 3D texture.

lorcán

Thanks Ciardhubh & broumbroum :)

Ciardhubh I don't think I'll use 3D textures in my current game however I'd be interested in your code for experimenting and maybe I'll use 3D textures in a future project :-) I'm quite amazed by the smooth transition between each animation frames. Really nice results! ;)

Ciardhubh

Quote from: lorcán on June 16, 2009, 09:26:30
Thanks Ciardhubh & broumbroum :)

Ciardhubh I don't think I'll use 3D textures in my current game however I'd be interested in your code for experimenting and maybe I'll use 3D textures in a future project :-) I'm quite amazed by the smooth transition between each animation frames. Really nice results! ;)

I felt like writing something :)

Smooth Sprite Animation using 3D Textures in OpenGL:
http://ciardhubh.de/node/19

broumbroum

Thank you ! I was also looking for this.

lorcán

Thank you very much for your interesting article Ciardhubh! ;)