Results 1 to 4 of 4

Thread: creating an MPEG from an open GL animation

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2000
    Posts
    2

    creating an MPEG from an open GL animation

    Hi. I have an animation that I'd like to turn into an MPEG. I'm running GL under linux, and it seems that I need to dump each frame of the animation to a separate file (gif or ppm?) to then convert the result into an MPEG. Is there an easy way to save each frame? I'm new to all this, so any help will be appreciated....

    Loren Frank

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jul 2000
    Location
    Nice, France
    Posts
    201

    Re: creating an MPEG from an open GL animation

    To save each frame, just do a glReadPixels of the entire front buffer after every swapbuffer(use GL_UNSIGNED_BYTE for type). This way you store the frame into an array of GLubyte in memory. Then from this array, you can rebuild an image file (uncompressed is easier, so no gif).

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2000
    Posts
    2

    Re: creating an MPEG from an open GL animation

    Thanks for the info. Do you know how to take the array of pixel values and convert it into a gif, ppm, or other graphics format that can be read in to a MPEG encoder?

    Thanks again,

    Loren

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jul 2000
    Location
    Nice, France
    Posts
    201

    Re: creating an MPEG from an open GL animation

    I use the following function to save RGB or RGBA images to targa files.
    I don't know if you can make mpegs directly from TGA files or if you need to convert them first.
    You probably won't need to save the alpha channel, but that can be done with TGA files.

    //---------------------------------------------------------------------------
    // Inputs
    // pixels_data pointer to an array of bytes containing the pixel
    // data of the image to save.
    // file_name Pointer to a string of characters containing the
    // name of the Output file.
    // width Width of the image in pixels.
    // height Height of the image in pixels.
    // has_alpha Parameter definig if alpha values are present in
    // the memory buffer.
    // save_alpha Parameter definig if alpha values must be stored
    // in the targa file.
    //---------------------------------------------------------------------------
    void SaveImageToTargaFile(unsigned char *pixel_data, char *file_name,
    int width, int height,bool has_alpha, bool save_alpha)
    {
    FILE *image_file; // Targa image file's handle
    char file_info[6]; // TGA file's header's second part
    int image_size; // Size of the image in pixels
    int i, j; // Counter variables
    int bytes_per_pixel; // Bytes per pixel in original image
    int bytes_to_store; // Bytes per pixel in the output file
    unsigned char *file_buffer; // temporary buffer
    int pixpos; // Current writting position in the file buffer
    int pixel_data_size; // size of pixel data

    image_size = width * height;

    // Create second part of the header
    i = width % 256;
    j = height % 256;
    file_info[0] = (char)(i);
    file_info[1] = (char)((width - i) / 256);
    file_info[2] = (char)(j);
    file_info[3] = (char)((height - j)/ 256);
    file_info[4] = (char)(24 + (((save_alpha) && (has_alpha)) ? 8 : 0));
    file_info[5] = 0;

    // Preparing to write pixel data into the file buffer
    bytes_per_pixel = has_alpha ? 4 : 3;
    bytes_to_store = file_info[4] / 8;
    pixel_data_size = image_size * bytes_to_store;
    file_buffer = new unsigned char[18 + (image_size * bytes_to_store)];
    // Write first part of the header into the file buffer
    memcpy(file_buffer, TGA_header, 12);
    // Write second part of the header into the file buffer
    memcpy(&file_buffer[12], file_info, 6);
    for(i = 0; i < image_size; i++)
    {
    pixpos = 18 + i * bytes_per_pixel;
    for(j = 0; j < 3; j++)
    {
    file_buffer[pixpos + j] = pixel_data[i * bytes_per_pixel + 2 - j];
    }
    if((has_alpha) && (save_alpha))
    {
    file_buffer[pixpos + 3] = pixel_data[i * bytes_per_pixel + 3];
    }
    }

    // Try to create the output file
    image_file = fopen(file_name, "wb");
    if(image_file == NULL)
    return;

    if(fwrite(file_buffer, 18 + pixel_data_size, 1, image_file) != 1)
    return;

    fclose(image_file);
    // Free memory
    delete [] file_buffer;
    }

Similar Threads

  1. Replies: 9
    Last Post: 02-11-2011, 10:59 AM
  2. Creating a 2D Animation With Movable Objects
    By meinkamfyschweter in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-25-2010, 10:24 PM
  3. Replies: 2
    Last Post: 05-19-2008, 01:57 AM
  4. Structure for MPEG -1/MPEG-2/MPEG-2.5 layer 2
    By jerrymannil in forum OpenMAX
    Replies: 0
    Last Post: 10-25-2007, 11:35 PM
  5. Structure for MPEG -1/MPEG-2/MPEG-2.5 layer 2
    By jerrymannil in forum OpenMAX
    Replies: 0
    Last Post: 12-31-1969, 07:00 PM

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