Some mild ammendments...
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
int n;
int m;
GLint counter = 0;
/* you actually require 100 images to be loaded here. Store pointers to 100 images therefore.... */
GLuint *imagesArray[100];
/* use a pointer to cycle through the above images */
GLuint *image = NULL;
void myidle()
{
/* increment the counter */
counter ++;
/* get the counter to cycle from 0 to 99 */
counter = counter%100;
/* assign the image pointer */
image = imagesArray[ counter ];
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(n, m, GL_RGB, GL_UNSIGNED_INT, image);
glutSwapBuffers();
}
void myreshape(int h, int w)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLfloat) n, 0.0, (GLfloat) m);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, h, w);
}
int main(int argc, char **argv)
{
FILE *fd;
int k, nm;
int one;
char filename1[20];
int i;
float s;
int red, green, blue;
for( counter = 0; counter <= 100; counter++ ) {
sprintf(filename1,"nldat2/%05d.dat", counter );
if ( !( fd = fopen( filename1, "r") ) )
printf ( "File cannot be opened\n" );
else {
printf( "opened file %05d.dat\n", counter );
n = 296;
m = 128;
k = 255;
nm = n*m;
/* allocate memory for the image, before you had a horrendous memory leak .... */
imagesArray[counter] = malloc(3*sizeof(GLuint)*nm);
s = 255./k;
/* assign the image pointer to the current image */
image = imagesArray[ counter ];
for(i = 0; i < nm; i++)
{
fscanf(fd, "%d\n", &one );
red = one;
blue = one;
green = one;
image[3*i + 3] = red;
image[3*i + 2] = green;
image[3*i + 1] = blue;
}
}
printf( "finished loop %d\n", counter);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(n, m);
glutInitWindowPosition(0, 0);
glutCreateWindow("image");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutIdleFunc(myidle);
glPixelTransferf(GL_RED_SCALE, s);
glPixelTransferf(GL_GREEN_SCALE, s);
glPixelTransferf(GL_BLUE_SCALE, s);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
glClearColor(1.0, 1.0, 1.0, 1.0);
glutMainLoop();
return 0;
}