OpenGL Animated Sprite Question

Apprentice
Posts: 6
Joined: 2009.05
Post: #1
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!
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #2
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.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2009.05
Post: #3
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?
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #4
Sure you can ... all you need to do is to partition your sprites within a single image.

http://en.wikipedia.org/wiki/Texture_atlas
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #5
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.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2009.05
Post: #6
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.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #7
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Modfied OpenGL Sprite Rendering tonyb 3 2,928 Jul 26, 2009 07:30 AM
Last Post: tonyb
  For 2D (sprite) games, do I use OpenGL or something else? lindsay 24 11,809 May 9, 2009 12:06 PM
Last Post: Weltevrede
  OpenGL ES sprite alpha Holmes 27 14,692 Mar 16, 2009 01:06 PM
Last Post: Holmes
  OpenGl Es Sprite render jjslay 43 25,503 Nov 1, 2008 04:24 AM
Last Post: wonza