Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

Writing a Movie File Using C++

Header Files

You need to include "movie.h" to get all the class definitions. This header file will include all the other animp header files in addition to "iostream", "stdio.h", "stdlib.h", "string.h" and a few more. You may not need too many more.

#include "movie.h" #include <math.h>

Object Declarations

You will probably need to declare a movie and a frame object. The test program "mk_movie.cpp" uses pointers.

int main ( int argc, char **argv ) { movie *m; ap_frame *f;

You will need some other variables

int num_frames; int num_spheres; char *file; int x, y, z; int size;

Command Line Parameters

You may wish to get some parameters from the command line

file = (argc > 1) ? argv[1] : (char *)"movie.an"; num_frames = (argc > 2) ? atoi(argv[2]) : 100; num_spheres = (argc > 3) ? atoi(argv[3]) : 100; size = (argc > 4) ? atoi(argv[4]) : 3;

Creating a New Movie Object

You will need to create a new movie object to add colors and frames later. It helps slightly to tell it the number of expected frames, but the software can cope with increasing the array sizes when necessary.

m = new movie ( file );

Adding Colors to the Movie

You can use the floating point ap_color constructor or the integer version. The floating point color components vary from 0.0 to 1.0 and the integer components vary from 0 to 255.

m->add ( ap_color ( 1.0, 0.0, 0.0, 1.0 ) ); // opaque red m->add ( ap_color ( 0.0, 1.0, 0.0, 0.4 ) ); // trans. green m->add ( ap_color ( 0.3, 0.4, 0.9, 0.3 ) ); // trans. light blue m->add ( ap_color ( 0.9, 0.7, 0.3, 0.5 ) ); // hmmm m->add ( ap_color ( 255, 127, 127, 50 ) ); // trans. bright red

Adding Frames and Spheres

The nested loops below create frames and add them to the movie. The outer loop creates a new ap_frame, uses the inner loop to add spheres to the frame and then adds the frame to the movie.

The inner loop uses the sin function to generate coordinates for the spheres it adds to the frame. It makes all the spheres the same size, but it does vary the color by stepping through the colors added earlier.

for ( int nf = 0; nf < num_frames; nf++ ) { f = new ap_frame(num_spheres); for ( int ns = 0; ns < num_spheres; ns++ ) { x = (int)(50 + 50*sin(M_PI*2.0*nf/num_frames) *sin(M_PI*2.0*ns/num_spheres)); y = ns * 100 / num_spheres; z = (int)(50 + 50*sin(M_PI*2.0*nf/num_frames) *sin(M_PI*2.0*ns/num_spheres)); f->add ( sphere(x,y,z,size,ns%m->colors->n) ); } m->add ( f ); }

Writing the Movie File

If the movie object is created using the constructor with the output file name, then the pieces of the movie file are written as they are added to the object. It is still necessary to call the write routine with no parameters to finish the write operation. At this time the write code will position the file to the proper location and write the actual number of frames to the file.

m->write(); }

Generated on Thu Nov 27 08:17:11 2003 for Animp - Animated Particles by doxygen 1.3.2