PDA

View Full Version : animation



08-24-2002, 06:32 AM
Hello,
I've been working with OpenGL off and on and I needed a program to read my data files and display them on the screen. I do have one that was fixed by someone in this help forum and I've been using that so far.
I now need the code to work on any number of files by reading the images one at a time instead of loading them into an array and then reading them out. I was told by someone that all I needed was space for one image and I could use that to cycle through my data files and display them on the screen. The program does display something but there's a lot of flickering and you can't see anything at all.
If someone could point out what is wrong, I would be very grateful. Here's the code for the animation display.

/* Movie player: plays movies with an indeterminate number of files */

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

#include <GL/glut.h>


GLint counter = 0;
GLuint *image;
GLfloat s;


GLint n = 296;
GLint m = 128;
GLint k = 255;

void myidle()
{

counter++;
fprintf(stderr, "counter is %d\n", counter );
glutPostRedisplay();

}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(n, m, GL_RGB, GL_UNSIGNED_INT, image);
glutSwapBuffers();
}

void bitmap()
{
FILE *fd;
int k, nm;
int one;
char filename1[20];
int i;
int red, green, blue;

fprintf(stderr,"counter is %d\n", counter );
nm = n*m;
sprintf(filename1,"nldat/%05d.dat", counter );
while ( ( fd = fopen( filename1, "r") ) != NULL ) {

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


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)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(n, m);
glutInitWindowPosition(0, 0);
glutCreateWindow("image");
glutReshapeFunc(myreshape);
bitmap();
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;
}

Thanks a lot in advance.

nexusone
08-26-2002, 07:58 AM
To help the flickering, do not keep updating the screen in your idle function until you are ready to load a new file.


example:

myidle()
{
int new_image;

new_image = FALSE;

// If you want the images to load automatically, then maybe a time function.
if( (current_time - past_time) > delay_time); // Where delay_time is the amount of time to pass before loading image.
{
load_new_image();
new_image = TRUE;
past_time = current_time; // reset timmer
}

if(newimage = true) glutPostRedisplay(); We only draw to the screen if the image has changed.
}



[This message has been edited by nexusone (edited 08-26-2002).]

[This message has been edited by nexusone (edited 08-26-2002).]