// OUTPUT FRAME // reads the OpenGL pixel buffer and outputs to a file // keeps and increments an internal counter each time // 1. Call 'OutputFrameInitialize()' routine with width and height of // buffer and base filename that can include the path, e.g. // "/Users/rp/Frames/frame" // initialization will allocate a buffer and save a pointer to the // filename string and initialize the file number to be used during output // if there is an error, a message will be printed and a '0' will be returned. // 2. Successive calls to 'outputFrame()' will write out .ppm files // with '000', '001', '002', etc appended; number will be 3 digits and // start at the number passed in during initialization, e.g., // /Users/rp/Frames/frame001.ppm // /Users/rp/Frames/frame002.ppm // /Users/rp/Frames/frame003.ppm // if there is an error, a message will be printed and a '0' will be returned. // 3. call 'OutputFrameCleanUp()': the CleanUp routine will deallocate // the buffer // some other program (e.g. Final Cut on the Mac) will have to process // the .ppm files and convert them to an animation format. The .ppm may // have to be converted to some other format, e.g. .jpg, before processing #include "outputFrame.h" // ------------------------------------------------------------------- // STANDARD INCLUDES #include #include #include #include // ------------------------------------------------------------------- // GLOBAL DATA static char*frameBuffer = NULL; static intframeNo = 0; static int frameWidth,frameHeight; static char *frameName; // -------------------------------------------------------------------------- // OUTPUT FRAME INITIALIZE // records frame data, allocates buffer, and sets frame number to 0 // -------------------------------------------------------------------------- int outputFrameInitialize(int w,int h,char *name,int num) { frameWidth = w; frameHeight = h; frameName = name; frameNo = num; if (frameBuffer != NULL) free(frameBuffer); if ( (frameBuffer = malloc(frameWidth*frameHeight*4)) == NULL) { printf("** outputFrame error: could not allcoate frame buffer\n"); return 0; } else { return 1; } } // -------------------------------------------------------------------------- // OUTPUT FRAME // reads the OpenGL pixel buffer // outputs a 3 digit numbered frame in ppm format // -------------------------------------------------------------------------- int outputFrame() { FILE*frameFile; charfilename[80]; inti,j; int errno; if (frameBuffer != NULL) { glReadPixels(0,0,frameWidth,frameHeight,GL_RGBA,GL_UNSIGNED_BYTE,frameBuffer); sprintf(filename,"%s%03d.ppm",frameName,frameNo); printf("output to file %s\n",filename); if ( (frameFile = fopen(filename,"w")) == NULL) { printf("** outputFrame error: fopen of frame file failed\n"); exit(1); } else { if ( (errno = fprintf(frameFile,"P3\n")) < 0) { printf("** outputFrame error: print to file error (%d)\n",errno); } fprintf(frameFile,"%d %d\n",frameWidth,frameHeight); fprintf(frameFile,"255\n"); for (j=frameHeight-1; j>=0; j--) { for (i=0; i