PDA

View Full Version : Animation of a series of images using OpenGL



04-10-2002, 12:54 PM
Hello,
I am new to OpenGL and have been reading the book by Edward Angel on OpenGL graphics. In his accompanying manual, he provides code for a PPM file reader and I have adapted it to show my own data files( in grayscale ). I want to know how I could use OpenGL to call up a series of image files one after the other so that the effect is like that of a movie playing on the output window. I have tried call to glutIdleFunc but all I get is the last image in the series being displayed
Any help would be appreciated. Thanks a lot in advance.

Otonski

Rob The Bloke
04-10-2002, 12:59 PM
goto the nehe website and there is an example of an avi loader. You could adapt that so that it will draw the pixels directly to the framebuffer using glDrawPixels, but that would not be as good as using something more suitable (ie directDraw). Entirely possible though...

nexusone
04-10-2002, 03:37 PM
Sounds like a problem in your code:

Do you have a delay between the images being displayed?
Do you redraw screen for each new image?

Show us the code, without see it can only guess what the problem is.



Originally posted by Otonski:
Hello,
I am new to OpenGL and have been reading the book by Edward Angel on OpenGL graphics. In his accompanying manual, he provides code for a PPM file reader and I have adapted it to show my own data files( in grayscale ). I want to know how I could use OpenGL to call up a series of image files one after the other so that the effect is like that of a movie playing on the output window. I have tried call to glutIdleFunc but all I get is the last image in the series being displayed
Any help would be appreciated. Thanks a lot in advance.

Otonski

04-10-2002, 05:34 PM
Hi all,
This code is taken from Edward Angel's book OpenGL Primer and was for a PPM reader. I've cut out all the unecessary parts of the reader and used it to display a data file which is just a series of numbers from 0 to 255 just like a PPM file. The data file used represents a grid 296 points (width) by 128 bits (height).
I have two questions:
i) How can I display the data files one after the other?
ii) I seem to get a grayscale image with this colormap. How do I change the color values so that I can show in my animation the densest parts as having a red color while the sparse parts have a blue color with green in between.
The code can be found below. Thanks for all your help.


#include <stdio.h>
#include <stdlib.h>

#include <GL/glut.h>

int n;
int m;
GLint counter = 0;


GLuint *image;


void myidle()
{

counter += 1;
if( counter <= 100 )
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;
image = malloc(3*sizeof(GLuint)*nm);
s = 255./k;

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;
}

Rob The Bloke
04-10-2002, 06:27 PM
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", &amp;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(&amp;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;
}

should sort it....

[This message has been edited by Rob The Bloke (edited 04-10-2002).]