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.