Results 1 to 5 of 5

Thread: Animation of a series of images using OpenGL

  1. #1
    Guest

    Animation of a series of images using OpenGL

    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

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    472

    Re: Animation of a series of images using OpenGL

    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...

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: Animation of a series of images using OpenGL

    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

  4. #4
    Guest

    Re: Animation of a series of images using OpenGL

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

  5. #5
    Senior Member Regular Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    472

    Re: Animation of a series of images using OpenGL

    Code :
    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).]

Similar Threads

  1. 3D image compounded from series of 2D images
    By north in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-18-2009, 03:10 AM
  2. Geforce 8800 series and OpenGL
    By Mars_999 in forum OpenGL: User Software
    Replies: 5
    Last Post: 11-09-2006, 12:51 PM
  3. OpenGL ES and View Arch. in Series 60
    By Fadi Chehimi in forum OpenGL ES
    Replies: 1
    Last Post: 06-15-2005, 12:11 PM
  4. OpenGL for GeForce4 Ti series?
    By Imanewbie in forum OpenGL: User Software
    Replies: 1
    Last Post: 11-22-2002, 10:52 AM
  5. Re: animation of a series of images using OpenGL
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-14-2002, 03:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean