33. OpenGL Animating Textures (Version 2.0)

Introduction

Some of you might be wondering about animating your textures in OpenGL. Unfortunately, OpenGL does not natively support texture animation. You cannot directly load GIFs or movie files and render them on shapes. However, there are workarounds, and this tutorial will guide you through one of the simplest methods.

In this method, we create a series of image files representing each frame of the animation. These frames are loaded as individual textures and displayed sequentially to simulate animation. Let’s dive in!

Setting Up the Animation Frames

We start by loading each frame of the animation into a texture array. Here’s how it’s done:

  1. GLuint texture[9]; // Array to hold textures  
  2. texture[0] = LoadTexture("textures/1.raw", 256, 256);  
  3. texture[1] = LoadTexture("textures/2.raw", 256, 256);  
  4. texture[2] = LoadTexture("textures/3.raw", 256, 256);  
  5. texture[3] = LoadTexture("textures/4.raw", 256, 256);  
  6. texture[4] = LoadTexture("textures/5.raw", 256, 256);  
  7. texture[5] = LoadTexture("textures/6.raw", 256, 256);  
  8. texture[6] = LoadTexture("textures/7.raw", 256, 256);  
  9. texture[7] = LoadTexture("textures/8.raw", 256, 256);  
  10. texture[8] = LoadTexture("textures/9.raw", 256, 256);  

Each frame is loaded as a unique texture using the `LoadTexture` function, which takes the file path, width, and height of the texture as arguments.

Displaying the Animation

To display the animation, we bind the texture corresponding to the current frame and draw it on a quad. Here’s the key section of the rendering code:

  1. glBindTexture(GL_TEXTURE_2D, texture[(int)frame]); // Bind the current frame  
  2. glBegin(GL_QUADS);  
  3. glTexCoord2d(0, 0); glVertex3f(-1, 1, 0);  
  4. glTexCoord2d(1, 0); glVertex3f(1, 1, 0);  
  5. glTexCoord2d(1, 1); glVertex3f(1, -1, 0);  
  6. glTexCoord2d(0, 1); glVertex3f(-1, -1, 0);  
  7. glEnd();  

Here, `frame` is a variable that determines which frame of the animation to display. Each texture is mapped onto a quad using texture coordinates.

Animating the Frames

To simulate animation, we increment the `frame` variable each time the display function is called. When `frame` exceeds the maximum frame number, it is reset to the first frame, creating a seamless loop.

  1. frame += 0.2; // Advance the frame  
  2. if (frame > 8) {  
  3.     frame = 0; // Loop back to the first frame  
  4. }  

The increment value `0.2` determines the animation speed. Smaller values slow down the animation, while larger values speed it up.

Tutorial Code

Here’s the complete program for animating textures in OpenGL:

  1. #include <gl gl.h="">  
  2. #include <gl glut.h="">  
  3. #include <cstdlib> // For malloc and free  
  4.   
  5. GLuint texture[9]; // Array to hold texture frames  
  6. double frame = 0;  // Current animation frame  
  7.   
  8. // Function to load textures  
  9. GLuint LoadTexture(const char *filename, int width, int height) {  
  10.     GLuint texture;  
  11.     unsigned char *data;  
  12.     FILE *file;  
  13.   
  14.     file = fopen(filename, "rb");  
  15.     if (file == NULL) return 0;  
  16.   
  17.     data = (unsigned char *)malloc(width * height * 3);  
  18.     fread(data, width * height * 3, 1, file);  
  19.     fclose(file);  
  20.   
  21.     glGenTextures(1, &texture);  
  22.     glBindTexture(GL_TEXTURE_2D, texture);  
  23.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  
  24.   
  25.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);  
  26.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);  
  27.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);  
  28.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);  
  29.   
  30.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);  
  31.     free(data);  
  32.   
  33.     return texture;  
  34. }  
  35.   
  36. // Render a textured quad  
  37. void cube(void) {  
  38.     glBindTexture(GL_TEXTURE_2D, texture[(int)frame]);  
  39.     glBegin(GL_QUADS);  
  40.     glTexCoord2d(0, 0); glVertex3f(-1, 1, 0);  
  41.     glTexCoord2d(1, 0); glVertex3f(1, 1, 0);  
  42.     glTexCoord2d(1, 1); glVertex3f(1, -1, 0);  
  43.     glTexCoord2d(0, 1); glVertex3f(-1, -1, 0);  
  44.     glEnd();  
  45. }  
  46.   
  47. // Display function  
  48. void display(void) {  
  49.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  50.     glLoadIdentity();  
  51.     glEnable(GL_TEXTURE_2D);  
  52.     glTranslatef(0, 0, -5);  
  53.   
  54.     cube();  
  55.   
  56.     glutSwapBuffers();  
  57.   
  58.     frame += 0.2;  
  59.     if (frame > 8) {  
  60.         frame = 0;  
  61.     }  
  62. }  
  63.   
  64. // Initialize textures and settings  
  65. void init(void) {  
  66.     texture[0] = LoadTexture("textures/1.raw", 256, 256);  
  67.     texture[1] = LoadTexture("textures/2.raw", 256, 256);  
  68.     texture[2] = LoadTexture("textures/3.raw", 256, 256);  
  69.     texture[3] = LoadTexture("textures/4.raw", 256, 256);  
  70.     texture[4] = LoadTexture("textures/5.raw", 256, 256);  
  71.     texture[5] = LoadTexture("textures/6.raw", 256, 256);  
  72.     texture[6] = LoadTexture("textures/7.raw", 256, 256);  
  73.     texture[7] = LoadTexture("textures/8.raw", 256, 256);  
  74.     texture[8] = LoadTexture("textures/9.raw", 256, 256);  
  75. }  
  76.   
  77. // Reshape function  
  78. void reshape(int w, int h) {  
  79.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);  
  80.     glMatrixMode(GL_PROJECTION);  
  81.     glLoadIdentity();  
  82.     gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);  
  83.     glMatrixMode(GL_MODELVIEW);  
  84. }  
  85.   
  86. // Main function  
  87. int main(int argc, char **argv) {  
  88.     glutInit(&argc, argv);  
  89.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);  
  90.     glutInitWindowSize(500, 500);  
  91.     glutCreateWindow("Texture Animation");  
  92.   
  93.     init();  
  94.   
  95.     glutDisplayFunc(display);  
  96.     glutIdleFunc(display);  
  97.     glutReshapeFunc(reshape);  
  98.     glutMainLoop();  
  99.   
  100.     return 0;  
  101. }  
  102. </cstdlib></gl></gl>  

If you have any questions, feel free to email me at swiftless@gmail.com. Happy coding!

Download Textures(.RAW files in a Zip file)

Download Textures(.BMP files in a Zip file)

  • March 25, 2010
  • 9