Пример #1
0
int main(int argc, char *argv[]) {
  Image *src;
  const int rows = 600;
  const int cols = 800;
  const int Resolution = 50;
	Color white;
  Color Grey;
  Color dkGrey;
  Color Red;
  Color Blue;
  Point unitCircle[Resolution];
  Point unitSquare[4];
  Point pt[Resolution];
  Point ptt[Resolution];
  int i, j, index = 0;
  Matrix VTM, GTM, LTM;
  Polygon *ship[50];
  Color shipColor[50];
  double theta = 0.0;
  double phaserAngle = 0.0;
  int firePhase = 0;

  color_set(&Grey, 180/255.0, 180/255.0, 183/255.0);
  color_set(&dkGrey, 140/255.0, 140/255.0, 143/255.0);
  color_set(&Red, 250/255.0, 40/255.0, 40/255.0);
  color_set(&Blue, 30/255.0, 20/255.0, 250/255.0);

  if(argc > 1) {
    theta = atoi(argv[1]);
  }
  printf("Drawing ship with orientation %.2f degrees\n", theta);

  if(argc > 2) {
    phaserAngle = atoi(argv[2]);
    firePhase = 1;

    printf("Drawing phaser with angle %.2f degrees\n", phaserAngle);
  }

  srand(42);

  src = image_create(rows, cols);
	color_set(&white, 1.0, 1.0, 1.0);
	for(i=0; i<rows; i++){
		for(j=0; j<cols; j++){
			if((rand()%50) == 13){
				image_setColor(src, i, j, white);
			}
		}
	}

  // initialize the three matrices
  matrix_identity(&VTM);
  matrix_identity(&GTM);
  matrix_identity(&LTM);

  // Fix world coordinates as normal (x, y) 

  // give the view window an origin at -180m, -150m
  // size is a 4x3 ratio
  // VTM = T(0, rows-1)S(cols/vx, rows/vy)T(180, 150)
  matrix_translate2D(&VTM, 120, 100);
  matrix_scale2D(&VTM, cols/(4*60), -rows/(3*60));
  matrix_translate2D(&VTM, 0, rows-1);
  printf("VTM\n");
  matrix_print(&VTM, stdout);

  // make a space ship oriented along the positive X axis
  // use the LTM to move simple primitives into place
  // use the GTM to rotate the ship
  // use the VTM to change the view

  // make a list of points that form the unit circle
  for(i=0;i<Resolution;i++) {
    point_set2D(&(unitCircle[i]), 
    cos( i * 2.0 * M_PI / (float)Resolution), 
    sin( i * 2.0 * M_PI / (float)Resolution));
  }
  // set up the unit square
  point_set2D(&(unitSquare[0]), 0, 0);
  point_set2D(&(unitSquare[1]), 1, 0);
  point_set2D(&(unitSquare[2]), 1, 1);
  point_set2D(&(unitSquare[3]), 0, 1);

  // build a set of polygons that form the ship in model space
  // put the origin of the model between the engines

  // outline for the main disk
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 31, 31);
  // move it 20m along the X-axis
  matrix_translate2D(&LTM, 60, 0);
  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = Red;

  printf("Post-LTM\n");
  polygon_print(ship[0], stdout);

  // main disk
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 30, 30);
  // move it 20m along the X-axis
  matrix_translate2D(&LTM, 60, 0);
  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = Grey;

  // central bridge disk
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 10, 10);
  // move it 20m along the X-axis
  matrix_translate2D(&LTM, 60, 0);
  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = dkGrey;

  // make the body disk elongated along the X axis
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 30, 12);
  matrix_translate2D(&LTM, 2.5, 0);

  // transform the circle points using LTM
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(Resolution, pt);
  shipColor[index++] = Grey;
  
  // make a trapezoidal strut out of the unit square
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -0.5, 0.0);
  matrix_scale2D(&LTM, 10, 10);
  matrix_shear2D(&LTM, .2, 0.0);

  for(i=0;i<4;i++) {
    matrix_xformPoint(&LTM, &(unitSquare[i]), &(pt[i]));
  }

  // move the strut out from the origin along the Y axis
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, 0, 12);

  for(i=0;i<4;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  matrix_print(&LTM, stdout);
  ship[index] = polygon_createp(4, ptt);
  shipColor[index++] = Grey;

  // place the second strut
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 1, -1);
  matrix_translate2D(&LTM, 0, -12);

  for(i=0;i<4;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(4, ptt);
  shipColor[index++] = Grey;

  // create an engine outline from the unit circle
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 31, 6);

  // make the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // send one engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, 27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Blue;

  // send the other engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, -27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Blue;

  // create an engine
  matrix_identity(&LTM);
  matrix_scale2D(&LTM, 30, 5);

  // make the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(unitCircle[i]), &(pt[i]));
  }

  // send one engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, 27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Grey;

  // send the other engine to the right location
  matrix_identity(&LTM);
  matrix_translate2D(&LTM, -5, -27);

  // move the engine
  for(i=0;i<Resolution;i++) {
    matrix_xformPoint(&LTM, &(pt[i]), &(ptt[i]));
  }

  // add the polygon
  ship[index] = polygon_createp(Resolution, ptt);
  shipColor[index++] = Grey;

  // set up the phaser
  if(firePhase) {
    matrix_identity(&LTM);

    matrix_scale2D(&LTM, 100, 2);

    // orient the phaser
    matrix_rotateZ(&LTM, cos(phaserAngle*M_PI/180.0), sin(phaserAngle*M_PI/180.0));
    
    // translate it to the center of the disk and out
    matrix_translate2D(&LTM, 
           60 + 30 * cos(phaserAngle*M_PI/180.0), 
           30 * sin(phaserAngle*M_PI/180.0) );

    // use the unit square
    for(i=0;i<4;i++) {
      matrix_xformPoint(&LTM, &(unitSquare[i]), &(pt[i]));
    }
    
    // add the polygon
    ship[index] = polygon_createp(4, pt);
    shipColor[index++] = Red;
  }

  matrix_rotateZ(&GTM, cos(theta*M_PI/180.0), sin(theta*M_PI/180.0));

  printf("GTM:\n");
  matrix_print(&GTM, stdout);

  printf("Pre-GTM/VTM\n");
  polygon_print(ship[0], stdout);

  for(i=0;i<index;i++) {

    // multiply the polygon by the global transform matrix
    matrix_xformPolygon(&GTM, ship[i]);
    if(i==0) {
      printf("Pre-VTM\n");
      polygon_print(ship[i], stdout);
    }

    // multiply the polygon by the view transformation matrix
    matrix_xformPolygon(&VTM, ship[i]);

    if(i==0) {
      printf("Pre-draw\n");
      polygon_print(ship[i], stdout);
    }

    // draw the polygon
    polygon_drawFill(ship[i], src, shipColor[i]);
  }

  image_write(src, "space.ppm");


  image_free(src);

  return(0);
}
Пример #2
0
int main(int argc, char* argv[]){
	Image* src;
	Module *scene;
	Module* GRAPHICS;
	View3D view;
	Matrix vtm, gtm;
	DrawState *ds;
	Lighting *light;
	Point center;//center of animation
	Polygon poly;//polygon that holds the animation path points
	int frameNum;//holds the frame number for animation
	char filename[100];//holds the frame name
	Color Red;
	Color Blue;
	Color Green;
	Color Grey;
	Color Lemon;
	Color Black;
	Color White;
	Color Yellow;
	Color DarkAmbiant;
	Color randColor[10];


	//setting colors
	Color_set(&Red, 1.0, 0.2, 0.1 );
	Color_set(&Blue, 0.1, 0.1, 1.0);
	Color_set(&Green, 0.1, 1, 0.1 );
	Color_set(&White, 1, 1, 1 );
	Color_set(&Grey, 0.7, 0.7, 0.7 );
	Color_set(&Lemon, 1.0, 1.0, 0.8);
	Color_set(&Black, 0.05, 0.05, 0.05);
	Color_set(&Yellow, 1, 0.894118, 0.709804);
	Color_set(&DarkAmbiant, 0.3, 0.3, 0.3);

	Color_set(&randColor[0], 0, 1, 1);
	Color_set(&randColor[1], 0.498039, 1, 0);
	Color_set(&randColor[2], 1, 0.54902, 0);
	Color_set(&randColor[3], 1, 0.0784314, 0.576471);
	Color_set(&randColor[4], 1, 0.843137, 0);
	Color_set(&randColor[5], 0.960784, 1, 0.980392);
	Color_set(&randColor[6], 1, 0.647059, 0);
	Color_set(&randColor[7], 1, 0.270588, 0);
	Color_set(&randColor[8], 0, 1, 0.498039);
	Color_set(&randColor[9], 1, 1, 0);



	// setting the view
	point_set3D( &(view.vrp), 35, 60, 30 );
	vector_set( &(view.vpn), -view.vrp.val[0]+35, -view.vrp.val[1], -view.vrp.val[2]);
	vector_set( &(view.vup), 0, 1, 0 );
	view.d = 1;
	view.du = 1.6;
	view.dv = 0.9;
	view.f = 1;
	view.b = 200;
	view.screenx = 1280;
	view.screeny = 720;

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


	//creating GRAPHICS module
	GRAPHICS = module_create();
	module_alphabet_G(GRAPHICS);

	module_identity(GRAPHICS);
	module_translate(GRAPHICS, 10, 0, 0);
	module_alphabet_R(GRAPHICS);

	module_identity(GRAPHICS);
	module_translate(GRAPHICS, 20, 0, 0);
	module_alphabet_A(GRAPHICS);

	module_identity(GRAPHICS);
	module_translate(GRAPHICS, 30, 0, 0);
	module_alphabet_P(GRAPHICS);

	module_identity(GRAPHICS);
	module_translate(GRAPHICS, 40, 0, 0);
	module_alphabet_H(GRAPHICS);

	module_identity(GRAPHICS);
	module_translate(GRAPHICS, 50, 0, 0);
	module_alphabet_I(GRAPHICS);

	module_identity(GRAPHICS);
	module_translate(GRAPHICS, 56, 0, 0);
	module_alphabet_C(GRAPHICS);

	module_identity(GRAPHICS);
	module_translate(GRAPHICS, 68, 0, 0);
	module_alphabet_S(GRAPHICS);


	// setting the light
	light = lighting_create();
	lighting_add( light, LightAmbient, &Lemon, NULL, NULL, 0, 0);
	lighting_add(light, LightPoint, &White , NULL, &view.vrp, 0, 0);
	lighting_add(light, LightSpot, &White, &view.vpn, &view.vrp, cos(10*M_PI/180), 40);

	//setting drawstate
	ds = drawstate_create();
	point_copy(&(ds->viewer), &(view.vrp) );
	ds->shade = ShadePhong;
	// ds->shade = ShadeDepth;
	drawstate_setBody(ds, Black);
	drawstate_setSurface(ds, Red);
	drawstate_setSurfaceCoeff(ds, 10);

	//Animation
	frameNum =0;

	//path #1
	point_set3D(&center, 40, 0, -10);
  	view_rotate_circle(&poly, &center, 100, 50, 0, 0, 0);
  	polygon_print(&poly, stdout);
  	for(int k=0; k<100; k++){
  		frameNum++;
		point_set3D( &(view.vrp), poly.vertex[k].val[0], poly.vertex[k].val[1], poly.vertex[k].val[2]);
		vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] );
		matrix_setView3D( &vtm, &view );

		//creating scene module
		scene = module_create();
		module_module(scene, GRAPHICS);

		// image
		src = image_create( view.screeny, view.screenx );
		image_fillrgb(src, 1.0, 1.0, 0.8);

		//Drawing
		module_draw( scene, &vtm, &gtm, ds, light, src );
		sprintf(filename, "../images/frame_%.4d.ppm",frameNum);
		image_write( src, filename);
	}

	//path #2
	point_set3D(&center, 40, 0, -10);
  	view_rotate_circle(&poly, &center, 100, 90, 0, 0 , 0);
  	// polygon_print(&poly, stdout);
  	for(int k=0; k<100; k++){
  		if(frameNum == 119){
  			point_print(&view.vrp, stdout);
  			break;
  		}
  		view_rotate_circle(&poly, &center, 50, 50+k, 0-2*k, 0 , 0);
  		frameNum++;
		point_set3D( &(view.vrp), poly.vertex[k].val[0], poly.vertex[k].val[1], poly.vertex[k].val[2]);
		vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] );
		matrix_setView3D( &vtm, &view );

		//creating scene module
		scene = module_create();
		module_module(scene, GRAPHICS);

		// image
		src = image_create( view.screeny, view.screenx );
		image_fillrgb(src, 1.0, 1.0, 0.8);

		//Drawing
		module_draw( scene, &vtm, &gtm, ds, light, src );
		sprintf(filename, "../images/frame_%.4d.ppm",frameNum);
		image_write( src, filename);
	}

	//path #3
	for(int k=0; k<120; k++){

  		frameNum++;
  		if(frameNum <= 160){
			point_set3D( &(view.vrp), -3.345+(k), 36.298+(k), 28.391-(k/2.0));
			vector_set( &(view.vpn), -view.vrp.val[0]+k, -view.vrp.val[1], -view.vrp.val[2]);
			matrix_setView3D( &vtm, &view );
		}

		//creating scene module
		scene = module_create();
		module_module(scene, GRAPHICS);

		if(frameNum == 160){
			light = lighting_create();
			lighting_add( light, LightAmbient, &Grey, NULL, NULL, 0, 0);
		}
		// setting the light
		if(frameNum == 165){
			Point plight1;
			point_set3D(&plight1, -5, 10, -10);

			lighting_add(light, LightSpot, &White, &view.vpn, &plight1, cos(10*M_PI/180), 40);
		}
		if(frameNum == 175){
			Point plight2;
			point_set3D(&plight2, 85, 10, -10);

			lighting_add(light, LightSpot, &White, &view.vpn, &plight2, cos(10*M_PI/180), 40);
		}
		if(frameNum == 180){
			lighting_add(light, LightSpot, &White, &view.vpn, &view.vrp, cos(10*M_PI/180), 40);
		}

		if(frameNum >= 183){
			light = lighting_create();
			lighting_add( light, LightAmbient, &Grey, NULL, NULL, 0, 0);

			int output1 = 0 + (rand() % (int)(10 - 0 + 1));
			int output2 = 0 + (rand() % (int)(10 - 0 + 1));
			int output3 = 0 + (rand() % (int)(10 - 0 + 1));

			Point plight1;
			point_set3D(&plight1, -5, 10, -10);
			lighting_add(light, LightSpot, &randColor[output1], &view.vpn, &plight1, cos(10*M_PI/180), 40);

			Point plight2;
			point_set3D(&plight2, 85, 10, -10);
			lighting_add(light, LightSpot, &randColor[output2], &view.vpn, &plight2, cos(10*M_PI/180), 40);

			lighting_add(light, LightSpot, &randColor[output3], &view.vpn, &view.vrp, cos(10*M_PI/180), 40);
		}
		// image
		src = image_create( view.screeny, view.screenx );
		image_fillrgb(src, 1.0, 1.0, 0.8);

		//setting drawstate
		ds = drawstate_create();
		point_copy(&(ds->viewer), &(view.vrp) );
		ds->shade = ShadePhong;
		// ds->shade = ShadeDepth;
		drawstate_setBody(ds, Black);
		drawstate_setSurface(ds, Red);
		drawstate_setSurfaceCoeff(ds, 10);

		//Drawing
		module_draw( scene, &vtm, &gtm, ds, light, src );
		sprintf(filename, "../images/frame_%.4d.ppm",frameNum);
		image_write( src, filename);
	}

	//***Uncomment for making the .gif animation***
	printf("Making the demo.gif file....\n");
	system("convert -delay 10 ../images/frame_*.ppm ../images/demo.gif");
	printf("Cleaning up...\n");
	system("rm -f ../images/frame_*.ppm");
	printf("Finished Successfully :)\n");
	return(0);
}