2
votes

I'm trying to develop a simple 2D fighting game using OpenGL, and I'm stuck on characters animation. The basic idea is to have a quad made of two triangles, and change the texture of this quad every frame or so. I'm using a sprite sheet to store the frames, and now I'm trying to find the most convenient way to cycle through them. The vertices coordinates and texture coordinates are stored in a vertex buffer object in the GPU memory, using the GL_STATIC_DRAW parameter.

The first idea was to load the sprite sheet using an external library such as SOIL in a single OpenGL texture (glGenTextures(1, &texture);), and then change the texture coordinates of the quad passed to the vertex shader every frame, so that if the animation was four frames long, I'd change the texture's S coord to 0.0, 0.25, 0.5 and 0.75 and back to 0.0. But that means that I have to change my VBO quite often, and I thought that it could be quite expensive.

Another idea is to allocate an array of textures using glGenTextures(nFrames, &textureArray[0]); and then store a single frame for each array positionm and use glBindTexture(GL_TEXTURE_2D, textureArray[currFrame]); to apply different textures without having to change my VBO. But since I don't want to split the sprite sheet into several images, I can't think of any way to accomplish this using libraries such as SOIL.

What is the most convenient way to proceed in this case? I didn't provide any code because first I'd like to evaluate several possibilities.

1

1 Answers

1
votes

I think that I would do it the same way as you described first. Change the coordinates in the texture to show the portion you are interested in. I'm not sure how expensive that would be, but changing the VBO (using a shader) should be extremely effective and not expensive at all. Taking the rule about "optimize late" into consideration, it would be extremely easy to redo it at a later time as well. If you've done it in a well designed way that is.

But I do think it's faster to change the S coords of the texture than to swap between several different textures.