Exemplo n.º 1
0
int main( int argc, char *argv[]){
  //set up the images
  Image *src; 
  Image *myImage;

  //set up the modules!!
  Module *basket;

  Module *connector;
  Module *connector1;
  Line l;
  Point pts[2];

  Module *circle;
  Module *hotAirBalloon;
  Module *teamOfBalloons; 
  Module *scene; 

  //pick the colors
  Color colors[6]; 
  Color brown;
  Color blue;
  Color green;
  Color grey; 

  //set up view stuff, and image file stuff
  Matrix vtm, gtm; 
  int rows; 
  int cols; 
  View3D view; 
  DrawState *ds;
  char filename[256];

  int i, j, k, m, n; 

  //Set the colors up
  color_set(&brown, 0.2, 0.1, 0.0);
  color_set(&blue, 0.6, 0.8, 1.0);
  color_set(&green, 0.1, 0.4, 0.0);
  color_set(&grey, 0.3, 0.3, 0.3);
  color_set(&colors[0], 1.0, 0.0, 0.0);//Red
  color_set(&colors[1],1.0, 0.5, 0.0);//orange
  color_set(&colors[2],1.0, 1.0, 0.1);//yellow
  color_set(&colors[3], 0.0, 1.0, 0.0);//green
  color_set(&colors[4], 0.0, 0.0, 1.0);//blue
  color_set(&colors[5], 0.4, 0.0, 0.8);//purple
  
  //if you want to supply a background image you can, otherwise I set a default sky blue background!
  if(argc>1){
    printf("you supplied an image\n");
    myImage = image_read(argv[1]);
    rows = myImage->rows;
    cols = myImage->cols;
    printf("%d %d\n", rows, cols);
  }else{
    printf("We are giving your image a default size!\n");
    rows = 500;
    cols = 500;
    myImage = image_create(rows, cols); 
    for(m = 0; m< rows; m++){
      for(n = 0; n<cols; n++){
	image_setColor(myImage, m, n, blue);
      }
    }
  }

  //Loop over the scene 300 times moving the scene around!! 
  for(k = 0; k<300; k++){
    //Set up the view
    point_set( &(view.vrp), 150, 100, 200, 1.0);
    vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2]);
    vector_set( &(view.vup), 0, 1.0, 0);
    view.d = 20; 
    view.du = 10; 
    view.dv = view.du* (float)rows/cols;
    view.f = 0; 
    view.b = 5; 
    view.screenx = rows; 
    view.screeny = cols; 

    matrix_setView3D( &vtm, &view); 
    matrix_identity( &gtm ); 

    // basket: cube (will need 1 in hot air balloon module)
    basket = module_create(); 
    module_color(basket, &brown);
    module_cube(basket, 1); 

    //connectors: lines need 4
    connector = module_create();
    point_set3D(&pts[0],1,1,1 );
    point_set3D(&pts[1],2,4.2,1);
    line_set(&l, pts[0], pts[1]);
    module_line(connector, &l);

    connector1 = module_create();
    point_set3D(&pts[0], 1,1 , 1);
    point_set3D(&pts[1], 0,4.2 ,1 );
    line_set(&l, pts[0], pts[1]);
    module_line(connector1, &l);

    // balloon: circles nested on top of eachother 
    circle = module_create();
    module_color(circle, &colors[0]);
    module_rotateX(circle, 0, 1);
    module_translate2D(circle, 0, 3);
    module_circle(circle, 1);

    module_color(circle, &colors[1]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 2.5);

    module_color(circle, &colors[2]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 3);

    module_color(circle, &colors[3]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 4);

    module_color(circle, &colors[4]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 5);

    module_color(circle, &colors[5]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 6);

    module_color(circle, &colors[0]);
    module_rotateX(circle, 1,0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 7);

    module_color(circle, &colors[1]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 7);

    module_color(circle, &colors[2]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0,1);
    module_circle(circle, 7);

    module_color(circle, &colors[3]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 7);

    module_color(circle, &colors[4]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 6);

    module_color(circle, &colors[5]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 5);

    module_color(circle, &colors[0]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 1);
    module_circle(circle, 4);

    module_color(circle, &colors[1]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 0.5);
    module_circle(circle, 3);

    module_color(circle, &colors[2]);
    module_rotateX(circle, 1, 0);
    module_translate2D(circle, 0, 0.5);
    module_circle(circle, 2);

    //hotAirBalloon: put the above parts together
    hotAirBalloon = module_create();
    module_module(hotAirBalloon, basket);
    module_module(hotAirBalloon, connector);
    module_translate(hotAirBalloon, -2,0 ,0 );
    module_module(hotAirBalloon, connector1);
    module_translate(hotAirBalloon, 0, 0, -2);
    module_module(hotAirBalloon, connector1);
    module_translate(hotAirBalloon, 2, 0, 0);
    module_module(hotAirBalloon, connector);
    module_module(hotAirBalloon, circle);


    //make a team of balloons
    teamOfBalloons = module_create();
    module_translate(teamOfBalloons, 0,-30,-15);
    module_module(teamOfBalloons, hotAirBalloon);
    module_translate(teamOfBalloons, 0, 0+(k*0.1),15);
    module_module(teamOfBalloons, hotAirBalloon);
    module_translate(teamOfBalloons, 0, 0+(k*0.1),15);
    module_module(teamOfBalloons, hotAirBalloon);


    //make a scene of balloons
    scene = module_create(); 
    module_translate(scene, -40, 10+(k*0.1), 10);
    module_module(scene, teamOfBalloons);
    module_translate(scene, 140, 10, 55);
    module_module(scene, teamOfBalloons);
    module_translate(scene, -50, 10+(k*0.1), -40);
    module_module(scene, teamOfBalloons);
    module_translate(scene, 30, 10+(k*0.2) , -55);
    module_module(scene, teamOfBalloons);

    //set up the image
    src = image_create(rows, cols); 

    //Either draw the background image you supplied or use my default background
    for (i = 0; i<rows; i++){
      for (j = 0; j<cols; j++){
	  blue = image_getColor(myImage, i, j);
	  image_setColor(src,i, j, blue);
      }
    }

    //set up the draw state if you want filled polygons need to use ShadeConstant instead of ShadeFrame
    ds = drawstate_create();
    ds->shade = ShadeFrame; 

    //draw the modules!
    module_draw( scene, &vtm, &gtm, ds, NULL, src); 

    //put the files in the right place with the right name!! 
    sprintf(filename, "/export/home/vedwards/Desktop/Graphics/images/hotAirBalloons3/frame-%04d.ppm", k );
    printf("Writing image\n");
    image_write( src, filename );
  }

  //Clean up, delete all of the modules, images, and drawState
  module_delete(basket);
  module_delete(connector);
  module_delete(connector1);
  module_delete(circle);
  module_delete(hotAirBalloon);
  module_delete(teamOfBalloons);

  free(ds);
  image_free( src );
  image_free( myImage );
  return(0);
}
Exemplo n.º 2
0
/*
 * add a teapot to the module with the specified number of divisions
 */
void module_teapot(Module *m, int divisions, int solid){
	int i, j;
	Point teapot[127] = 
	{
		{{  0.2000,  0.0000, 2.70000, 1.0 }}, {{  0.2000, -0.1120, 2.70000, 1.0 }},
		{{  0.1120, -0.2000, 2.70000, 1.0 }}, {{  0.0000, -0.2000, 2.70000, 1.0 }},
		{{  1.3375,  0.0000, 2.53125, 1.0 }}, {{  1.3375, -0.7490, 2.53125, 1.0 }},
		{{  0.7490, -1.3375, 2.53125, 1.0 }}, {{  0.0000, -1.3375, 2.53125, 1.0 }},
		{{  1.4375,  0.0000, 2.53125, 1.0 }}, {{  1.4375, -0.8050, 2.53125, 1.0 }},
		{{  0.8050, -1.4375, 2.53125, 1.0 }}, {{  0.0000, -1.4375, 2.53125, 1.0 }},
		{{  1.5000,  0.0000, 2.40000, 1.0 }}, {{  1.5000, -0.8400, 2.40000, 1.0 }},
		{{  0.8400, -1.5000, 2.40000, 1.0 }}, {{  0.0000, -1.5000, 2.40000, 1.0 }},
		{{  1.7500,  0.0000, 1.87500, 1.0 }}, {{  1.7500, -0.9800, 1.87500, 1.0 }},
		{{  0.9800, -1.7500, 1.87500, 1.0 }}, {{  0.0000, -1.7500, 1.87500, 1.0 }},
		{{  2.0000,  0.0000, 1.35000, 1.0 }}, {{  2.0000, -1.1200, 1.35000, 1.0 }},
		{{  1.1200, -2.0000, 1.35000, 1.0 }}, {{  0.0000, -2.0000, 1.35000, 1.0 }},
		{{  2.0000,  0.0000, 0.90000, 1.0 }}, {{  2.0000, -1.1200, 0.90000, 1.0 }},
		{{  1.1200, -2.0000, 0.90000, 1.0 }}, {{  0.0000, -2.0000, 0.90000, 1.0 }},
		{{ -2.0000,  0.0000, 0.90000, 1.0 }}, {{  2.0000,  0.0000, 0.45000, 1.0 }},
		{{  2.0000, -1.1200, 0.45000, 1.0 }}, {{  1.1200, -2.0000, 0.45000, 1.0 }},
		{{  0.0000, -2.0000, 0.45000, 1.0 }}, {{  1.5000,  0.0000, 0.22500, 1.0 }},
		{{  1.5000, -0.8400, 0.22500, 1.0 }}, {{  0.8400, -1.5000, 0.22500, 1.0 }},
		{{  0.0000, -1.5000, 0.22500, 1.0 }}, {{  1.5000,  0.0000, 0.15000, 1.0 }},
		{{  1.5000, -0.8400, 0.15000, 1.0 }}, {{  0.8400, -1.5000, 0.15000, 1.0 }},
		{{  0.0000, -1.5000, 0.15000, 1.0 }}, {{ -1.6000,  0.0000, 2.02500, 1.0 }},
		{{ -1.6000, -0.3000, 2.02500, 1.0 }}, {{ -1.5000, -0.3000, 2.25000, 1.0 }},
		{{ -1.5000,  0.0000, 2.25000, 1.0 }}, {{ -2.3000,  0.0000, 2.02500, 1.0 }},
		{{ -2.3000, -0.3000, 2.02500, 1.0 }}, {{ -2.5000, -0.3000, 2.25000, 1.0 }},
		{{ -2.5000,  0.0000, 2.25000, 1.0 }}, {{ -2.7000,  0.0000, 2.02500, 1.0 }},
		{{ -2.7000, -0.3000, 2.02500, 1.0 }}, {{ -3.0000, -0.3000, 2.25000, 1.0 }},
		{{ -3.0000,  0.0000, 2.25000, 1.0 }}, {{ -2.7000,  0.0000, 1.80000, 1.0 }},
		{{ -2.7000, -0.3000, 1.80000, 1.0 }}, {{ -3.0000, -0.3000, 1.80000, 1.0 }},
		{{ -3.0000,  0.0000, 1.80000, 1.0 }}, {{ -2.7000,  0.0000, 1.57500, 1.0 }},
		{{ -2.7000, -0.3000, 1.57500, 1.0 }}, {{ -3.0000, -0.3000, 1.35000, 1.0 }},
		{{ -3.0000,  0.0000, 1.35000, 1.0 }}, {{ -2.5000,  0.0000, 1.12500, 1.0 }},
		{{ -2.5000, -0.3000, 1.12500, 1.0 }}, {{ -2.6500, -0.3000, 0.93750, 1.0 }},
		{{ -2.6500,  0.0000, 0.93750, 1.0 }}, {{ -2.0000, -0.3000, 0.90000, 1.0 }},
		{{ -1.9000, -0.3000, 0.60000, 1.0 }}, {{ -1.9000,  0.0000, 0.60000, 1.0 }},
		{{  1.7000,  0.0000, 1.42500, 1.0 }}, {{  1.7000, -0.6600, 1.42500, 1.0 }},
		{{  1.7000, -0.6600, 0.60000, 1.0 }}, {{  1.7000,  0.0000, 0.60000, 1.0 }},
		{{  2.6000,  0.0000, 1.42500, 1.0 }}, {{  2.6000, -0.6600, 1.42500, 1.0 }},
		{{  3.1000, -0.6600, 0.82500, 1.0 }}, {{  3.1000,  0.0000, 0.82500, 1.0 }},
		{{  2.3000,  0.0000, 2.10000, 1.0 }}, {{  2.3000, -0.2500, 2.10000, 1.0 }},
		{{  2.4000, -0.2500, 2.02500, 1.0 }}, {{  2.4000,  0.0000, 2.02500, 1.0 }},
		{{  2.7000,  0.0000, 2.40000, 1.0 }}, {{  2.7000, -0.2500, 2.40000, 1.0 }},
		{{  3.3000, -0.2500, 2.40000, 1.0 }}, {{  3.3000,  0.0000, 2.40000, 1.0 }},
		{{  2.8000,  0.0000, 2.47500, 1.0 }}, {{  2.8000, -0.2500, 2.47500, 1.0 }},
		{{  3.5250, -0.2500, 2.49375, 1.0 }}, {{  3.5250,  0.0000, 2.49375, 1.0 }},
		{{  2.9000,  0.0000, 2.47500, 1.0 }}, {{  2.9000, -0.1500, 2.47500, 1.0 }},
		{{  3.4500, -0.1500, 2.51250, 1.0 }}, {{  3.4500,  0.0000, 2.51250, 1.0 }},
		{{  2.8000,  0.0000, 2.40000, 1.0 }}, {{  2.8000, -0.1500, 2.40000, 1.0 }},
		{{  3.2000, -0.1500, 2.40000, 1.0 }}, {{  3.2000,  0.0000, 2.40000, 1.0 }},
		{{  0.0000,  0.0000, 3.15000, 1.0 }}, {{  0.8000,  0.0000, 3.15000, 1.0 }},
		{{  0.8000, -0.4500, 3.15000, 1.0 }}, {{  0.4500, -0.8000, 3.15000, 1.0 }},
		{{  0.0000, -0.8000, 3.15000, 1.0 }}, {{  0.0000,  0.0000, 2.85000, 1.0 }},
		{{  1.4000,  0.0000, 2.40000, 1.0 }}, {{  1.4000, -0.7840, 2.40000, 1.0 }},
		{{  0.7840, -1.4000, 2.40000, 1.0 }}, {{  0.0000, -1.4000, 2.40000, 1.0 }},
		{{  0.4000,  0.0000, 2.55000, 1.0 }}, {{  0.4000, -0.2240, 2.55000, 1.0 }},
		{{  0.2240, -0.4000, 2.55000, 1.0 }}, {{  0.0000, -0.4000, 2.55000, 1.0 }},
		{{  1.3000,  0.0000, 2.55000, 1.0 }}, {{  1.3000, -0.7280, 2.55000, 1.0 }},
		{{  0.7280, -1.3000, 2.55000, 1.0 }}, {{  0.0000, -1.3000, 2.55000, 1.0 }},
		{{  1.3000,  0.0000, 2.40000, 1.0 }}, {{  1.3000, -0.7280, 2.40000, 1.0 }},
		{{  0.7280, -1.3000, 2.40000, 1.0 }}, {{  0.0000, -1.3000, 2.40000, 1.0 }},
		{{  0.0000,  0.0000, 0.00000, 1.0 }}, {{  1.4250, -0.7980, 0.00000, 1.0 }},
		{{  1.5000,  0.0000, 0.07500, 1.0 }}, {{  1.4250,  0.0000, 0.00000, 1.0 }},
		{{  0.7980, -1.4250, 0.00000, 1.0 }}, {{  0.0000, -1.5000, 0.07500, 1.0 }},
		{{  0.0000, -1.4250, 0.00000, 1.0 }}, {{  1.5000, -0.8400, 0.07500, 1.0 }},
		{{  0.8400, -1.5000, 0.07500, 1.0 }}
	};
	
	int rim[16] = 
		{ 102, 103, 104, 105,   4,   5,   6,   7,
		    8,   9,  10,  11,  12,  13,  14,  15 };
	int body[2][16] = {
		{  12,  13,  14,  15,  16,  17,  18,  19,
		   20,  21,  22,  23,  24,  25,  26,  27 },
		{  24,  25,  26,  27,  29,  30,  31,  32,
		   33,  34,  35,  36,  37,  38,  39,  40 } };
	int lid[2][16] = {
		{  96,  96,  96,  96,  97,  98,  99, 100,
		  101, 101, 101, 101,   0,   1,   2,   3 },
		{   0,   1,   2,   3, 106, 107, 108, 109,
		  110, 111, 112, 113, 114, 115, 116, 117 } };
	int handle[2][16] = {
		{  41,  42,  43,  44,  45,  46,  47,  48,
		   49,  50,  51,  52,  53,  54,  55,  56 },
		{  53,  54,  55,  56,  57,  58,  59,  60,
		   61,  62,  63,  64,  28,  65,  66,  67 } };
	int spout[2][16] = {
		{  68,  69,  70,  71,  72,  73,  74,  75,
		   76,  77,  78,  79,  80,  81,  82,  83 },
		{  80,  81,  82,  83,  84,  85,  86,  87,
		   88,  89,  90,  91,  92,  93,  94,  95 } };
	int bottom[16] = 
		{ 118, 118, 118, 118, 124, 122, 119, 121,
		  123, 126, 125, 120,  40,  39,  38,  37 };
		  
	BezierSurface rimSurf, body1Surf, body2Surf, lid1Surf, lid2Surf, 
				handle1Surf, handle2Surf, spout1Surf, spout2Surf, bottomSurf;
				
	// init all surfaces
	rimSurf.zBuffer = body1Surf.zBuffer = body2Surf.zBuffer = lid1Surf.zBuffer =
	lid2Surf.zBuffer = handle1Surf.zBuffer = handle2Surf.zBuffer = 
	spout1Surf.zBuffer = spout2Surf.zBuffer = bottomSurf.zBuffer = 1;
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			rimSurf.c[i][j] = 		teapot[rim[j+4*i]];
			body1Surf.c[i][j] = 	teapot[body[0][j+4*i]];
			body2Surf.c[i][j] = 	teapot[body[1][j+4*i]];
			lid1Surf.c[i][j] = 		teapot[lid[0][j+4*i]];
			lid2Surf.c[i][j] = 		teapot[lid[1][j+4*i]];
			handle1Surf.c[i][j] = 	teapot[handle[0][j+4*i]];
			handle2Surf.c[i][j] = 	teapot[handle[1][j+4*i]];
			spout1Surf.c[i][j] = 	teapot[spout[0][j+4*i]];
			spout2Surf.c[i][j] = 	teapot[spout[1][j+4*i]];
			bottomSurf.c[i][j] = 	teapot[bottom[j+4*i]];
		}
	}
	
	// add surfaces to module so y is up
	module_rotateX(m, cos(1.5*M_PI), sin(1.5*M_PI));
	for(i=0;i<4;i++){
		module_rotateY(m, cos(0.5*M_PI*i), sin(0.5*M_PI*i));
		module_bezierSurface(m, &rimSurf, divisions, solid);
		module_bezierSurface(m, &body1Surf, divisions, solid);
		module_bezierSurface(m, &body2Surf, divisions, solid);
		module_bezierSurface(m, &lid1Surf, divisions, solid);
		module_bezierSurface(m, &lid2Surf, divisions, solid);
		module_bezierSurface(m, &bottomSurf, divisions, solid);
	}
	module_bezierSurface(m, &handle1Surf, divisions, solid);
	module_bezierSurface(m, &handle2Surf, divisions, solid);
	module_bezierSurface(m, &spout1Surf, divisions, solid);
	module_bezierSurface(m, &spout2Surf, divisions, solid);
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			handle1Surf.c[i][j].val[1] = -1*handle1Surf.c[i][j].val[1];
			handle2Surf.c[i][j].val[1] = -1*handle2Surf.c[i][j].val[1];
			spout1Surf.c[i][j].val[1] = -1*spout1Surf.c[i][j].val[1];
			spout2Surf.c[i][j].val[1] = -1*spout2Surf.c[i][j].val[1];
		}
	}
	module_bezierSurface(m, &handle1Surf, divisions, solid);
	module_bezierSurface(m, &handle2Surf, divisions, solid);
	module_bezierSurface(m, &spout1Surf, divisions, solid);
	module_bezierSurface(m, &spout2Surf, divisions, solid);
}