How make an animated texture? (OpenGL, DevIL, C++)

I’m trying to load a gif in my opengl program.

I have the gif frames separated into separate .png files but I can’t figure out how to switch from one frame to the next.

It will load one frame at a time just fine but I want it to be animated.

Could someone link me to a tutorial to see how to load a gif or show me how to do it?

I’m working with C++, OpenGL, Glut, and DevIL.

I can provide my code if you need to see it to show me how to do this but it will have to wait until later because I’m not at my computer right now.

You can’t load a single animated texture like this.

What you do is load each frame as a separate texture, then animate it as time passes.

For example, if your .gif had 10 frames, you would load create 10 separate textures and load each frame into each texture. Then, if you wished to animate them at 10 fps, you check the amount of time that has passed. If 0 to 0.1 seconds has passed you display frame 0, if 0.1 to 0.2 seconds has passed you display frame 1, and so on.

[QUOTE=mhagain;1282567]You can’t load a single animated texture like this.

What you do is load each frame as a separate texture, then animate it as time passes.

For example, if your .gif had 10 frames, you would load create 10 separate textures and load each frame into each texture. Then, if you wished to animate them at 10 fps, you check the amount of time that has passed. If 0 to 0.1 seconds has passed you display frame 0, if 0.1 to 0.2 seconds has passed you display frame 1, and so on.[/QUOTE]

I already have all the frames sepeate and loaded as .png my issue is getting it to switch the frame.

i dont know a tutorial for animations, but i would try to put it in a special class:


// abstract class ...
class Animation
{
public:

// interface
virtual void AnimateNextFrame(float timestep) = 0;

};


// special class for this kind of animation ...
class Animation_MovingTexture : public Animation
{
public:

virtual void AnimateNextFrame(float timestep)	// in ms
{
static float totaltimepassed = 0;
totaltimepassed += timestep;

int index = ((int)(totaltimepassed / m_msperpicture))  % 10;
glBindTexture(GL_TEXTURE_2D, m_textureIDs[index]);

// render quad ... etc ...

}

private:

GLuint m_textureIDs[10];
float m_msperpicture {500.0f};   // in ms

};

If you are capable of rendering with one texture, you ought to be able to render with a different texture the next frame. So what exactly is your problem: how to tell when to change frames, how to structure your code, or do you just want us to write it for you?

I want to know how to get it to switch frames without using a new if statememt for each frame as that beings to get irritating.

No if you write the code for me line for line it’s not a learning process.

Store the textures in an array.

Ok, but once it’s in an array how do I get it to switch the frame thats loaded. Either my loop makes it crash or it stays on 1 frame.

Ok, this is no longer the fun project I thought it was gonna be. Will someone please show me the code and tell me what each part does so I can learn from that?

OK, let’s pretend this is your array:

GLuint textures[10];

And let’s pretend this is your time variable (integer, milliseconds):

int timePassed;

So to select a different texture each frame, based on time passed:

glBindTexture (GL_TEXTURE_2D, textures[timePassed % 10]);

I don’t know how fast or slow you want this to animate so you may wish to multiply or divide timePassed by some factor before doing the % 10 step.

Seriously now: this was incredibly basic stuff.