iDevGames Forums
OpenGL Animated Sprite Question - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: OpenGL Animated Sprite Question (/thread-1355.html)



OpenGL Animated Sprite Question - Aboqa - May 2, 2009 01:41 PM

Hello,

im relatively new to opengl in general and i wanted to ask what would be the best way to animate a sprite on the iphone.

Short description of my project:
2 big bg animated sprites (2 frames, same size 1024x512).
and aprox 6 animated sprites (50 frames, mostly each frame 128x128, but possiblity for dynamic sized frames).

Based on some tutorials i have the following options for animating the small sprites:

1. Display Lists with one big texture 1024x1024 (storage for aprox 100 frames)
I saw this only used in NeHe Tutorial for rendering text from textured font.
So would this be the right choice? [Edit: Forget it ;( OpenGL ES doesn't support Display Lists]

2. Same as 1 but no displaylist (changing textcoordinates)
Is it possible to select the frame in the texture or would the whole texture be resized?

3. One Texture with varying size for each frame.
Does this give me a negative performance impact?

Did I miss a possibility? Which possiblity would you recommend?

Thanks!


OpenGL Animated Sprite Question - warmi - May 4, 2009 07:46 AM

Yeah, you can do that.

Your texture coordinates are no different than selecting a subimage for display from a large image.

If possible use a single texture , biding textures during every frame can become expensive.


OpenGL Animated Sprite Question - Aboqa - May 5, 2009 03:24 PM

What exactly do you mean by saying binding texture in every frame can be expensive.

I only have max 10 animated sprites all of them different so i can't use the same texture multiple times. I guess thats what you meant (Maybe effective if you are cloning hundereds of same textured enemys?).

Anyway here is my Extension Code to the Texture2D class.
Code:
- (void) drawAtPoint:(CGPoint)point doInvert:(BOOL)invert ind:(int)ind cx:(int)cx
{
    GLfloat        coordinates[] = { ind*(_maxS/cx), _maxT,
        (ind+1)*(_maxS/cx), _maxT,
        ind*(_maxS/cx), 0,
        (ind+1)*(_maxS/cx), 0 };
    
    GLfloat        width = ((GLfloat)_width * _maxS) / cx,
    height = (GLfloat)_height * _maxT;
    GLfloat        vertices[] = {
        -width + point.x,    -height + point.y,
        width + point.x,    -height + point.y,
        -width + point.x,    height + point.y,
    width + point.x,    height + point.y};
    
    glBindTexture(GL_TEXTURE_2D, _name);
    if(invert) glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND ); // Color invert
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    if(invert) glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); // Reset invert
}
ind = index of frame
cx = count of frames in x axis

Any improvement suggestions?


OpenGL Animated Sprite Question - warmi - May 5, 2009 08:12 PM

Sure you can ... all you need to do is to partition your sprites within a single image.

http://en.wikipedia.org/wiki/Texture_atlas


OpenGL Animated Sprite Question - AnotherJake - May 5, 2009 08:38 PM

Yes, Aboqa, warmi is talking about your option #2: "no displaylist (changing textcoordinates)"

You can't use display lists on iPhone, so changing your tex coords is the best option.

Your Texture2D modifications look fine on a glance, but it's not obvious to me why you'd change the GL_TEXTURE_ENV_MODE to BLEND; MODULATE should be sufficient unless you're doing something tricky. Your general idea otherwise should work well. This is the best way to animate sprites.


OpenGL Animated Sprite Question - Aboqa - May 6, 2009 08:12 AM

I don't change it everytime its an effect and inverts the color of the texture...

And I can't put all my images into one big texture cause the resolution would be crappy and scaling would have to be done. (Also not very effective when compressing using PVR Format)

OK Thanks guys.


OpenGL Animated Sprite Question - AnotherJake - May 6, 2009 11:17 AM

Aboqa Wrote:I don't change it everytime its an effect and inverts the color of the texture...

Ah, I see.

You don't need to put *all* of your images in one texture; just put as many as you can in as few textures as possible. I've found that it's not a terribly big deal unless you have lots and lots of sprites. The idea is, as already mentioned, minimizing texture binds to help performance. It is certainly not required though. Do whatever works for you, and later if you need more performance you can consider consolidating sprites as needed.

You can still use a texture atlas with PVRTC too, although I've found that the artifacts are horrible with alpha edges. Great for non-alpha though.