Esempio n. 1
0
int main(int argc, char **argv)
{
	const char *keyfilename, *certfilename;
	struct sign_context *ctx;
	uint8_t *buf, *tmp;
	int rc, c, sigsize;

	ctx = talloc_zero(NULL, struct sign_context);

	keyfilename = NULL;
	certfilename = NULL;

	for (;;) {
		int idx;
		c = getopt_long(argc, argv, "o:c:k:dvVh", options, &idx);
		if (c == -1)
			break;

		switch (c) {
		case 'o':
			ctx->outfilename = talloc_strdup(ctx, optarg);
			break;
		case 'c':
			certfilename = optarg;
			break;
		case 'k':
			keyfilename = optarg;
			break;
		case 'd':
			ctx->detached = 1;
			break;
		case 'v':
			ctx->verbose = 1;
			break;
		case 'V':
			version();
			return EXIT_SUCCESS;
		case 'h':
			usage();
			return EXIT_SUCCESS;
		}
	}

	if (argc != optind + 1) {
		usage();
		return EXIT_FAILURE;
	}

	ctx->infilename = argv[optind];
	if (!ctx->outfilename)
		set_default_outfilename(ctx);

	if (!certfilename) {
		fprintf(stderr,
			"error: No certificate specified (with --cert)\n");
		usage();
		return EXIT_FAILURE;
	}
	if (!keyfilename) {
		fprintf(stderr,
			"error: No key specified (with --key)\n");
		usage();
		return EXIT_FAILURE;
	}

	ctx->image = image_load(ctx->infilename);
	if (!ctx->image)
		return EXIT_FAILURE;

	_talloc_steal(ctx, ctx->image);

	ERR_load_crypto_strings();
	OpenSSL_add_all_digests();
	OpenSSL_add_all_ciphers();

	EVP_PKEY *pkey = fileio_read_pkey(keyfilename);
	if (!pkey)
		return EXIT_FAILURE;

	X509 *cert = fileio_read_cert(certfilename);
	if (!cert)
		return EXIT_FAILURE;

	const EVP_MD *md = EVP_get_digestbyname("SHA256");

	/* set up the PKCS7 object */
	PKCS7 *p7 = PKCS7_new();
	PKCS7_set_type(p7, NID_pkcs7_signed);

	PKCS7_SIGNER_INFO *si = PKCS7_sign_add_signer(p7, cert,
			pkey, md, PKCS7_BINARY);
	if (!si) {
		fprintf(stderr, "error in key/certificate chain\n");
		ERR_print_errors_fp(stderr);
		return EXIT_FAILURE;
	}

	PKCS7_content_new(p7, NID_pkcs7_data);

	rc = IDC_set(p7, si, ctx->image);
	if (rc)
		return EXIT_FAILURE;

	sigsize = i2d_PKCS7(p7, NULL);
	tmp = buf = talloc_array(ctx->image, uint8_t, sigsize);
	i2d_PKCS7(p7, &tmp);
	ERR_print_errors_fp(stdout);

	image_add_signature(ctx->image, buf, sigsize);

	if (ctx->detached)
		image_write_detached(ctx->image, ctx->outfilename);
	else
		image_write(ctx->image, ctx->outfilename);

	talloc_free(ctx);

	return EXIT_SUCCESS;
}
Esempio n. 2
0
int main(int argc, char *argv[]) {
  Point start[NUMLINES], end[NUMLINES];
  Color color[NUMLINES];
  Image *src;
  Line line;
  long i,j;
  Color white;
  double tstart, tend;
  long numLines;
  double sum, dx, dy;
  struct timeb tp;

  color_set( &white, 1.0, 1.0, 1.0);

  // allocate an image ROWS by COLS
  src = image_create(ROWS, COLS);
  if(!src) {
    fprintf(stderr, "unable to allocate memory\n");
    exit(0);
  }

  // Initialize the image to all white
  for(i=0;i<src->rows;i++) {
    for(j=0;j<src->cols;j++) {
      image_setColor(src, i, j, white );
    }
  }

  // Pre-calculate the line endpoints and colors so we don't have to
  // call the random() function inside the main drawing loop.
  sum = 0.0;
  for(i=0;i<NUMLINES;i++) {
    int tsx, tsy, tex, tey;
    tsx = (int)(drand48() * COLS);
    tsy = (int)(drand48() * ROWS);
    tex = (int)(drand48() * COLS);
    tey = (int)(drand48() * ROWS);
    point_set2D(&(start[i]), tsx, tsy );
    point_set2D(&(end[i]), tex, tey );
    color[i].c[0] = drand48();
    color[i].c[1] = drand48();
    color[i].c[2] = drand48();

    dx = tsx - tex;
    dy = tsy - tey;
    sum += sqrt(dx * dx + dy * dy);
  }
  sum /= NUMLINES;
  printf("Average line length = %.1lf\n", sum);

  // Start drawing lines
  ftime( &tp );
  tstart = tp.time + tp.millitm / 1000.0;

  for(i=0,numLines=0; numLines < 5000000;numLines++, i++) {
    i = i % NUMLINES;
    line_set(&line, start[i], end[i]);
    line_draw(&line, src, color[i]);
  }

  ftime( &tp );
  tend = tp.time + tp.millitm / 1000.0;

  // print out the result
  printf("%.2lf lines per second\n", numLines / (tend - tstart) );

  // write out the image
  image_write(src, "lines.ppm");

  // free the image data
  image_free(src);

  return(0);
}
Esempio n. 3
0
int main(int argc, char *argv[]) {
	int frame;
	Color blue, green, purple, red, white;
	Point p[16];
	BezierSurface bc;
	DrawState ds;
	Module *curve;
	View3D view;
	Matrix VTM, GTM;
	int divisions = 4;
	int rows = 300, cols = 400;
	Image *src = image_create(rows, cols);

	// grab the command line argument, if one exists
	if(argc > 1) {
		int tmp = atoi(argv[1]);
		if( tmp >= 0 && tmp < 10 )
			divisions = tmp;
	}
	printf("Creating Bezier surface with %d subdivisions\n", divisions);

	color_set(&white, 1.0, 1.0, 1.0 );
	color_set(&blue, .1, .2, .8);
	color_set(&green, .2, 0.7, 0.3 );
	color_set(&purple, 0.6, 0.1, 0.7 );
	color_set(&red, 0.75, 0.3, 0.3 );

	curve = module_create();

	// create a flat plane
	point_set3D(&p[0], 0.0, -0.2, 0.0); // first row, constant x, even spacing in z
	point_set3D(&p[1], 0.0, -0.2, 0.33);
	point_set3D(&p[2], 0.0, -0.2, 0.66);
	point_set3D(&p[3], 0.0, -0.2, 1.0);
	point_set3D(&p[4], 0.33, -0.2, 0.0); // second row
	point_set3D(&p[5], 0.33, -0.2, 0.33);
	point_set3D(&p[6], 0.33, -0.2, 0.66);
	point_set3D(&p[7], 0.33, -0.2, 1.0);
	point_set3D(&p[8], 0.66, -0.2, 0.0); // third row
	point_set3D(&p[9], 0.66, -0.2, 0.33);
	point_set3D(&p[10], 0.66, -0.2, 0.66);
	point_set3D(&p[11], 0.66, -0.2, 1.0);
	point_set3D(&p[12], 1.0, -0.2, 0.0); // fourth row
	point_set3D(&p[13], 1.0, -0.2, 0.33);
	point_set3D(&p[14], 1.0, -0.2, 0.66);
	point_set3D(&p[15], 1.0, -0.2, 1.0);
	bezierSurface_set(&bc, p);

	// put the curve into a module
	module_color(curve, &red);
	module_bezierSurface(curve, &bc, divisions, 0);

	// create a curved surface sitting above the plane
	point_set3D(&p[0], 0.0, 0.0, 0.0); // first row, constant x, even spacing in z
	point_set3D(&p[1], 0.0, 0.2, 0.33);
	point_set3D(&p[2], 0.0, 0.5, 0.66);
	point_set3D(&p[3], 0.0, 0.1, 1.0);
	point_set3D(&p[4], 0.33, 0.8, 0.0); // second row
	point_set3D(&p[5], 0.33, -0.1, 0.33);
	point_set3D(&p[6], 0.33, 0.0, 0.66);
	point_set3D(&p[7], 0.33, 0.3, 1.0);
	point_set3D(&p[8], 0.66, 0.3, 0.0); // third row
	point_set3D(&p[9], 0.66, 0.8, 0.33);
	point_set3D(&p[10], 0.66, 0.9, 0.66);
	point_set3D(&p[11], 0.66, 0.5, 1.0);
	point_set3D(&p[12], 1.0, 0.4, 0.0); // fourth row
	point_set3D(&p[13], 1.0, 0.2, 0.33);
	point_set3D(&p[14], 1.0, 0.5, 0.66);
	point_set3D(&p[15], 1.0, 1.0, 1.0);
	bezierSurface_set(&bc, p);

	// put the curve into a module
	module_color(curve, &green);
	module_bezierSurface(curve, &bc, divisions, 1);

	// set up the drawstate
	drawstate_setColor(&ds, white);
	ds.shade = ShadeFrame;
	
	// set up the view
	point_set3D(&(view.vrp), 0.0, 1.2, -3.0 );
	vector_set( &(view.vpn), 0.0, -0.8, 2.5 );
	vector_set( &(view.vup), 0.0, 1.0, 0.0 );
	view.d = 1.5;
	view.du = 1.0;
	view.dv = 1.0*rows/cols;
	view.screeny = rows;
	view.screenx = cols;
	view.f = 0.0;
	view.b = 3.0;

	matrix_setView3D( &VTM, &view );
	matrix_identity( &GTM );

	// Create the animation by adjusting the GTM
	for(frame=0;frame<60;frame++) {
		char buffer[256];
		
		matrix_rotateY(&GTM, cos(M_PI/30.0), sin(M_PI/30.0) );
		module_draw( curve, &VTM, &GTM, &ds, NULL, src );

		sprintf(buffer, "bezSurf-frame%03d.ppm", frame);
		image_write(src, buffer);
		image_reset(src);
	}
	
	printf("converting to gif...\n");
	system("convert -delay 1.5 -loop 0 bezSurf-frame*.ppm bezSurf.gif");
	system("rm bezSurf-frame*.ppm");

	// clean up
	image_free( src );

	module_delete( curve );

	return(0);
}
Esempio n. 4
0
static void m2i_open_write(path_t *path, cbmdirent_t *dent, uint8_t type, buffer_t *buf, uint8_t append) {
  uint16_t offset;
  uint8_t *str;
  uint8_t *nameptr;
  uint8_t i;
  FRESULT res;

  /* Check for read-only image file */
  if (!(partition[path->part].imagehandle.flag & FA_WRITE)) {
    set_error(ERROR_WRITE_PROTECT);
    return;
  }

  if (append) {
    open_existing(path, dent, type, buf, 1);
  } else {
    if (check_invalid_name(dent->name)) {
      set_error(ERROR_SYNTAX_JOKER);
      return;
    }

    /* Find an empty entry */
    offset = find_empty_entry(path->part);
    if (offset < M2I_ENTRY_OFFSET)
      return;

    memset(ops_scratch, ' ', sizeof(ops_scratch));
    str = ops_scratch;

    switch (type & TYPE_MASK) {
    case TYPE_DEL:
      *str++ = 'D';
      break;

    case TYPE_SEQ:
      *str++ = 'S';
      break;

    case TYPE_PRG:
      *str++ = 'P';
      break;

    case TYPE_USR:
      *str++ = 'U';
      break;

    default:
      /* Unknown type - play it safe, don't create a file */
      return;
    }

    *str++ = ':';

    /* Generate a FAT name */
    for (i=0;i<8;i++) {
      *str++ = '0';
    }
    *str = 0;

    do {
      FILINFO finfo;

      finfo.lfn = NULL;
      /* See if it's already there */
      res = f_stat(&partition[path->part].fatfs, ops_scratch + M2I_FATNAME_OFFSET, &finfo);
      if (res == FR_OK) {
        str = ops_scratch + M2I_FATNAME_OFFSET+7;
        /* Increment name */
        while (1) {
          if (++(*str) > '9') {
            *str-- = '0';
            continue;
          }
          break;
        }
      }
    } while (res == FR_OK);

    if (res != FR_NO_FILE)
      return;

    /* Copy the CBM file name */
    nameptr = dent->name;
    str = ops_scratch + M2I_CBMNAME_OFFSET;
    while (*nameptr)
      *str++ = *nameptr++;

    /* Update dent with the new FAT name */
    ustrcpy(dent->pvt.fat.realname, ops_scratch + M2I_FATNAME_OFFSET);

    /* Finish creating the M2I entry */
    ops_scratch[M2I_FATNAME_OFFSET + 8]  = ' ';
    ops_scratch[M2I_FATNAME_OFFSET + 12] = ':';
    ops_scratch[M2I_CBMNAME_OFFSET + CBM_NAME_LENGTH] = 13;
    ops_scratch[M2I_CBMNAME_OFFSET + CBM_NAME_LENGTH + 1] = 10;

    /* Write it */
    if (image_write(path->part, offset, ops_scratch, M2I_ENTRY_LEN, 1))
      return;

    /* Write the actual file - always without P00 header */
    fat_open_write(path, dent, TYPE_RAW, buf, append);

    /* Abort on error */
    if (current_error) {
      /* No error checking here. Either it works or everything has failed. */
      ops_scratch[0] = '-';
      image_write(path->part, offset, ops_scratch, 1, 1);
    }
  }
}
Esempio n. 5
0
int main(int argc, char *argv[]) {
  const int rows = 180;
  const int cols = 320;
	const int nFrames = 100;
  View3D view;
  Matrix vtm;
  Polygon side[6];
  Polygon tpoly;
  Point  tv[4];
  Point  v[8];
  Color  color[6];
  Image *src;
  int i, t;
	char filename[256];

  // set some colors
  color_set( &color[0], 0, 0, 1 );
  color_set( &color[1], 0, 1, 0 );
  color_set( &color[2], 1, 0, 0 );
  color_set( &color[3], 1, 0, 1 );
  color_set( &color[4], 0, 1, 1 );
  color_set( &color[5], 1, 1, 0 );

	for(t=0;t<nFrames;t++) {

		// initialize polygons
		for(i=0;i<6;i++) {
		  polygon_init( &side[i] );
		}

		// corners of a cube, centered at (0, 0, 0)
		point_set1( &v[0], -1, -1, -1 );
		point_set1( &v[1],  1, -1, -1 );
		point_set1( &v[2],  1,  1, -1 );
		point_set1( &v[3], -1,  1, -1 );
		point_set1( &v[4], -1, -1,  1 );
		point_set1( &v[5],  1, -1,  1 );
		point_set1( &v[6],  1,  1,  1 );
		point_set1( &v[7], -1,  1,  1 );

		// front side
		polygon_set( &side[0], 4, &(v[0]) );

		// back side
		polygon_set( &side[1], 4, &(v[4]) );

		// top side
		point_copy( &tv[0], &v[2] );
		point_copy( &tv[1], &v[3] );
		point_copy( &tv[2], &v[7] );
		point_copy( &tv[3], &v[6] );

		polygon_set( &side[2], 4, tv );

		// bottom side
		point_copy( &tv[0], &v[0] );
		point_copy( &tv[1], &v[1] );
		point_copy( &tv[2], &v[5] );
		point_copy( &tv[3], &v[4] );

		polygon_set( &side[3], 4, tv );

		// left side
		point_copy( &tv[0], &v[0] );
		point_copy( &tv[1], &v[3] );
		point_copy( &tv[2], &v[7] );
		point_copy( &tv[3], &v[4] );

		polygon_set( &side[4], 4, tv );

		// right side
		point_copy( &tv[0], &v[1] );
		point_copy( &tv[1], &v[2] );
		point_copy( &tv[2], &v[6] );
		point_copy( &tv[3], &v[5] );

		polygon_set( &side[5], 4, tv );

		// grab command line argument to determine viewpoint
		// and set up the view structure
		/*if( argc > 1 ) {
		  float alpha = atof( argv[1] );
		  if( alpha < 0.0 || alpha > 1.0 )
		    alpha = 0.0;

		  point_set1( &(view.vrp), 3*alpha, 2*alpha, -2*alpha - (1.0-alpha)*3 );
		}
		else {
		  point_set1( &(view.vrp), 3, 2, -2 );
		} */
	
		float alpha = abs(t - (0.5*nFrames)) /  (0.5*nFrames);
		point_set1( &(view.vrp), 3*alpha, 2*alpha, -2*alpha - (1.0-alpha)*3 );

		vector_set( &(view.vpn), -view.vrp.val[0], -view.vrp.val[1], -view.vrp.val[2] );

		vector_set( &(view.vup), 0, 1, 0 );
		view.d = 1;  // focal length
		view.du = 2;
		view.dv = view.du * (float)rows / cols;
		view.f = 0; // front clip plane
		view.b = 4; // back clip plane
		view.screenx = cols;
		view.screeny = rows;

		matrix_setView3D( &vtm, &view );

		// create image
		src = image_create( rows, cols );

		// use a temprary polygon to transform stuff
		polygon_init( &tpoly );

		//printf("Drawing Polygons\n");
		for(i=0;i<6;i++) {
		  polygon_copy( &tpoly, &side[i] );
		  matrix_xformPolygon( &vtm, &tpoly );

		  // normalize by homogeneous coordinate before drawing
		  polygon_normalize( &tpoly );

		  polygon_draw( &tpoly, src, color[i] );
		  //polygon_print( &tpoly, stdout );
		}

		sprintf(filename, "frame-%04d.ppm", t );
    image_write( src, filename );
		
		/*printf("Writing image\n");
		image_write( src, "cube.ppm" );*/
	}
	system("convert frame-*.ppm cube.gif");
	system("rm frame-*.ppm");

  return(0);
}
Esempio n. 6
0
int main(int argc, char *argv[]){
  Image *src;
  Ellipse elip;
  Circle circ;
  Color color, red, pink, yellow;
  Color blue; 
  Line l;
  Point p;
  Point pt[5];
  Polygon *poly;
  int i;

  /*set the colors*/
  color_set(&color, 1.0, 1.0, 1.0);
  color_set(&red, 1.0, 0.0,0.0);
  color_set(&blue, 0.4,1.0,1.0);
  color_set(&pink, 1.0, 0.6, 0.8);
  color_set(&yellow, 1.0, 1.0, 0.4);

  /*build an image*/
  src = image_create( 700,700); 

  /*build the cupcake like I was using turtle graphics*/
  line_set2D(&l, 100,300,200,600);
  line_draw( &l, src, pink );

  line_set2D(&l, 600,300,500,600);
  line_draw( &l, src, pink );

  point_set2D( &p, 600, 350 );
  ellipse_set(&elip, p, 40, 150,0);
  ellipse_draw(&elip, src, pink);

  point_set2D( &p, 300,350 );
  ellipse_set(&elip, p, 80, 250,0);
  ellipse_draw(&elip, src, pink);

  point_set2D( &p, 275,350 );
  ellipse_set(&elip, p, 75, 225,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 250,350 );
  ellipse_set(&elip, p, 50, 200,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 225,350 );
  ellipse_set(&elip, p, 25, 175,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 200,350 );
  ellipse_set(&elip, p, 20, 150,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 175,350 );
  ellipse_set(&elip, p, 15, 125,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 150,350 );
  ellipse_set(&elip, p, 5, 75,0);
  ellipse_draw(&elip, src, blue);

  point_set2D( &p, 350, 100 );
  circle_set( &circ, p, 25 );
  circle_draw( &circ, src, red);

  /*write image*/
  image_write( src, "3Dimage.ppm" );

  /*free image*/
  image_free( src );

  /*this time fill the cupcake*/

  src = image_create( 700,700); 

  color_set(&pink, 1.0, 0.6, 0.8);
  point_set2D( &p, 600, 350 );
  ellipse_set(&elip, p, 40, 150,0);
  ellipse_drawFill(&elip, src, pink);
  
  point_set2D(&(pt[0]),100 ,300);
  point_set2D(&(pt[1]),600 ,300);
  point_set2D(&(pt[2]),505 ,600);
  point_set2D(&(pt[3]),198, 600);
  
  poly = polygon_createp(4, pt);
  polygon_drawFill(poly,src, pink);

  color_set(&pink, 1.0, 0.4, 0.8);
  point_set2D( &p, 300,350 );
  ellipse_set(&elip, p, 80, 250,0);
  ellipse_drawFill(&elip, src, pink);

  color_set(&pink, 1.0, 0., 0.8);
  for(i=0; i<10; i++){
    line_set2D(&l, 100-i,300-i,200,600);
    line_draw( &l, src, pink );

    line_set2D(&l, 600-i,300-i,500,600);
    line_draw( &l, src, pink );

    line_set2D(&l, 175-i, 300-i, 225, 623);
    line_draw(&l, src, pink);

    line_set2D(&l, 275-i, 280-i, 305, 636);
    line_draw(&l, src, pink);

    line_set2D(&l, 385-i, 280-i, 385, 640);
    line_draw(&l, src, pink);

    line_set2D(&l, 510-i, 300-i, 450, 630);
    line_draw(&l, src, pink);

  }


  point_set2D( &p, 295,350 );
  ellipse_set(&elip, p, 70, 230,0);
  ellipse_drawFill(&elip, src, yellow);

  color_set(&blue, 0.0,0.8,0.8);

  point_set2D( &p, 275,350 );
  ellipse_set(&elip, p, 75, 220,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.0,1.0,1.0);

  point_set2D( &p, 250,350 );
  ellipse_set(&elip, p, 50, 200,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.4,1.0,1.0);

  point_set2D( &p, 225,350 );
  ellipse_set(&elip, p, 25, 175,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.6,1.0,1.0);

  point_set2D( &p, 200,350 );
  ellipse_set(&elip, p, 20, 150,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.7,1.0,1.0);

  point_set2D( &p, 175,350 );
  ellipse_set(&elip, p, 15, 125,0);
  ellipse_drawFill(&elip, src, blue);

  color_set(&blue, 0.8,1.0,1.0);

  point_set2D( &p, 150,350 );
  ellipse_set(&elip, p, 5, 75,0);
  ellipse_drawFill(&elip, src, blue);

  point_set2D( &p, 350, 100 );
  circle_set( &circ, p, 25 );
  circle_drawFill( &circ, src, red);

  image_write( src, "3DimageFill.ppm" );

  polygon_free(poly);
  image_free( src );

  return(0);

}
Esempio n. 7
0
int main(int argc, char *argv[]){
	Image* src;
	int offset = 100;
	const int rows = 1000;
  	const int cols = 1000;
  	float r1, r2, r3, r4, r5, r6, r7, r8, r9;
  	int i;
	Color RED;
	Color GREEN;
	Color BLUE;
	Color WHITE;
	Color c1;
	Color c2;
	Color c3;
	Polygon* poly;
	Point points[3];
	char filename[100];
	
	Color_set(&RED, 1.0, 0.0, 0.0);
	Color_set(&GREEN, 0.0, 1.0, 0.0);
	Color_set(&BLUE, 0.0, 0.0, 1.0);
	Color_set(&WHITE, 1.0,1.0,1.0);

	for(i=0; i<offset+1; i++){
		src = image_create(rows, cols);

		//drawing the first triangle
		printf("Preparing and drawing triangle 0\n");
		point_set2D(&points[0], 300, (100 - offset + i));
		point_set2D(&points[1], 700, (100 - offset + i));
		point_set2D(&points[2], 500, (500 - offset + i));

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the second triangle
		printf("Preparing and drawing triangle 1\n");
		point_set2D(&points[0], 700+offset-i, 100-(offset-i)/2);
		point_set2D(&points[1], 900+offset-i, 500-(offset-i)/2);
		point_set2D(&points[2], 500+offset-i, 500-(offset-i)/2);
		

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the third triangle
		printf("Preparing and drawing triangle 2\n");
		point_set2D(&points[0], 900+offset-i, 500+(offset-i)/2);
		point_set2D(&points[1], 700+offset-i, 900+(offset-i)/2);
		point_set2D(&points[2], 500+offset-i, 500+(offset-i)/2);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the forth triangle
		printf("Preparing and drawing triangle 3\n");
		point_set2D(&points[0], 300, 900 + offset - i);
		point_set2D(&points[1], 700, 900 + offset - i);
		point_set2D(&points[2], 500, 497 + offset - i);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);


		//drawing the fifth triangle
		printf("Preparing and drawing triangle 4\n");
		point_set2D(&points[0], 100-offset+i, 500+(offset-i)/2);
		point_set2D(&points[1], 300-offset+i, 900+(offset-i)/2);
		point_set2D(&points[2], 500-offset+i, 500+(offset-i)/2);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);



	//drawing the sixth triangle
		printf("Preparing and drawing triangle 5\n");
		point_set2D(&points[0], 100-offset+i, 500-(offset-i)/2);
		point_set2D(&points[1], 301-offset+i, 100-(offset-i)/2);
		point_set2D(&points[2], 503-offset+i, 500-(offset-i)/2);

		poly = polygon_createp(3, points);
		r1 = rand()/((double) RAND_MAX);
		r2 = rand()/((double) RAND_MAX);
		r3 = rand()/((double) RAND_MAX);
		r4 = rand()/((double) RAND_MAX);
		r5 = rand()/((double) RAND_MAX);
		r6 = rand()/((double) RAND_MAX);
		r7 = rand()/((double) RAND_MAX);
		r8 = rand()/((double) RAND_MAX);
		r9 = rand()/((double) RAND_MAX);

		Color_set(&c1, r1, r2, r3);
		Color_set(&c2, r4, r5, r6);
		Color_set(&c3, r7, r8, r9);

		polygon_drawFillB_Gradient(poly, src, c1, c2, c3);

		if(i%5==0){
			sprintf(filename, "../images/task6_2_%3.0d.ppm",i);
			image_write(src, filename);
		}
		image_dealloc(src);

	}	
	system("convert -delay 10 -loop 0   ../images/task6_2_*.ppm   ../images/animation.gif");
	system("rm -f ../images/task6_2*");

	//clean up

	polygon_free(poly);
  	image_free(src);


	return 0;
}
Esempio n. 8
0
// makes 3 X-wing fighters in a loose formation
int main(int argc, char *argv[]) {
  int i, j; //loop variables

  Image *src;
  Module* wall;
  Module* ray;
  Module* ray2;
  Module *scene1;
  Module* scene2;
  Polygon p;
  Line l;
  Point point[4];
  Point point2[2];
  View3D view;
  Matrix vtm, gtm;
  DrawState *ds;
  char filename[100];
  Color Flame = { { 1.0, 0.7, 0.2 } };
  Color Red =  { { 1.0, 0.2, 0.1 } };
  Color Grey =  { { 0.745, 0.745, 0.745} };
  Color Blue = {{0.117647, 0.564706, 1}};
  // Color Grey = {{1, 1, 1}};


  // set up the view
  point_set3D( &(view.vrp), 4, 4, 4 );
  vector_set( &(view.vpn), -view.vrp.val[0], -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 = 50;
  view.screenx = 640;
  view.screeny = 360;

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

  // //wall
  wall = module_create();
  module_color(wall, &Red);
  polygon_init(&p);
  point_set3D(&point[0], 1,1,2);
  point_set3D(&point[1], 1,0,2);
  point_set3D(&point[2], 0,0,2);
  point_set3D(&point[3], 0,1,2);
  polygon_set(&p, 4, &point[0]);
  module_polygon(wall, &p);

//ray
  ray = module_create();
  module_color(ray, &Blue);
  for(i=0; i< 5; i++){
  point_set3D(&point2[0], -1+0.01*i, -1, 1);
  point_set3D(&point2[1], 1+0.01*i, 1, 1);
  line_set(&l, point2[0], point2[1]);
  module_line(ray, &l);
 }

 //ray2

  ray2 = module_create();
  module_color(ray2, &Red);
  for(i=0; i< 5; i++){
  point_set3D(&point2[0], -1+0.01*i, 1, -1);
  point_set3D(&point2[1], 1+0.01*i, -1, -1);
  line_set(&l, point2[0], point2[1]);
  // line_zBuffer(&l, 0);
  module_line(ray2, &l);
 }



//scene
    // scene = module_create();
    // module_module(scene, wall);
    // module_module(scene, ray);
    // module_module(scene, ray2);
    // module_module(scene, wall);
    



    

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

	//scene
    scene1 = module_create();
    scene2 = module_create();
    module_rotateZ(scene1, cos(i*10 * M_PI/180), sin(i*10 * M_PI/180));
    module_scale( scene1, 3, 1, 2 );
    module_color( scene1, &Blue );
    module_cube( scene1, 1);


    module_scale(scene2, 0.5, 0.5, 0.5);
    module_cylinder(scene2, 30);
	// create the image and drawstate
	src = image_create( 360, 640 );
	ds = drawstate_create();
  drawstate_setAlpha(ds, 1);
	ds->shade = ShadeDepth;

	// draw into the scene
  // module_draw( scene1, &vtm, &gtm, ds, src );
  drawstate_setAlpha(ds, 1 );
	module_draw( scene1, &vtm, &gtm, ds, src );

	// write out the scene
	sprintf(filename, "frame_%.2d.ppm", i);
	image_write( src, filename );
	module_delete( scene1);

}
	 


	//free the polygon data
	// polygon_clear( &p );

	// free the modules
	// module_delete( scene);
	// module_delete( wall );


	// free the drawstate
	free(ds);

	// free the image
	image_free( src );

	return(0);
}
Esempio n. 9
0
/*
  Program to test matrix library functionality
*/
int main(int argc, char *argv[]) {
  Image *src;
  const int rows = 600;
  const int cols = 800;
  const int Resolution = 50;
  Color Grey;
  Color dkGrey;
  Color Red;
  Color Blue;
  Point unitCircle[Resolution];
  Point unitSquare[4];
  Point pt[Resolution];
  Point ptt[Resolution];
  int i, 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);

  // 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, "test5a.ppm");


  image_free(src);

  return(0);
}
Esempio n. 10
0
/*
  Program to test polygon functionality using barycentric coordinates
*/
int main(int argc, char *argv[]) {
  Image *src;
  const int rows = 100;
  const int cols = 100;
  Polygon *p;
  Color Red;
  Color White;
  Color Blue;
  Point pt[100];

  color_set(&Red, 0.9, 0.2, 0.1 );
  color_set(&White, 1.0, 1.0, 1.0 );
  color_set(&Blue, 0.2, 0.1, 0.95 );
  
  src = image_create(rows, cols);

  // make a simple square to test proper areas and locations
  // the square ought to be 20x20, include pixel (30,30) and exclude pixel (50, 50)
  point_set2D(&(pt[0]), 30, 30);
  point_set2D(&(pt[1]), 50, 30);
  point_set2D(&(pt[2]), 50, 50);
  point_set2D(&(pt[3]), 30, 50);
  point_set2D(&(pt[4]), 30, 30);

  p = polygon_createp(3, pt);

  printf("drawing polygon 1\n");
  polygon_drawFillB(p, src, Blue);

  polygon_set(p, 3, &(pt[2]) );

  printf("drawing polygon 2\n");
  polygon_drawFillB(p, src, Red);

  point_set2D(&(pt[5]), 60, 20);
  point_set2D(&(pt[6]), 80, 85);
  point_set2D(&(pt[7]), 50, 70);
  polygon_set(p, 3, &(pt[5]));

  printf("drawing polygon 3\n");
  polygon_drawFillB(p, src, White);

  point_set2D(&(pt[8]), 5, 5);
  point_set2D(&(pt[9]), 25, 5);
  point_set2D(&(pt[10]), 25, 25);
  point_set2D(&(pt[11]), 5, 25);
  point_set2D(&(pt[12]), 5, 5);

  polygon_set(p, 3, &(pt[10]) );

  printf("drawing polygon 4\n");
  polygon_drawFillB(p, src, Red);

  polygon_set(p, 3, &(pt[8]));

  printf("drawing polygon 5\n");
  polygon_drawFillB(p, src, Blue);


  printf("writing output\n");
  image_write(src, "test4b.ppm");

  image_free(src);

  return(0);
}
Esempio n. 11
0
int main(int argc, char *argv[]) {
	int i, frame;
	Color blue, green, purple, red, white;
	Point p[4];
	BezierCurve bc;
	DrawState ds;
	Module *curveA;
	Module *curveB;
	Module *curves;
	View3D view;
	Matrix VTM, GTM;
	int divisions = 4;
	int rows = 300, cols = 400;
	Image *src = image_create(rows, cols);

	// grab the command line argument, if one exists
	if(argc > 1) {
		int tmp = atoi(argv[1]);
		if( tmp >= 0 && tmp < 10 )
			divisions = tmp;
	}
	printf("Creating Bezier curves with %d subdivisions\n", divisions);

	color_set(&white, 1.0, 1.0, 1.0 );
	color_set(&blue, .1, .2, .8);
	color_set(&green, .2, 0.7, 0.3 );
	color_set(&purple, 0.6, 0.1, 0.7 );
	color_set(&red, 0.75, 0.3, 0.3 );

	// set one curve
	point_set3D(&p[0], 0.0, 0.0, 0.0);
	point_set3D(&p[1], 1.0, 0.2, 0.0);
	point_set3D(&p[2], 0.7, 0.5, 0.2);
	point_set3D(&p[3], 1.0, 1.0, 1.0);
	bezierCurve_set(&bc, p);

	// put the curve into a module
	curveA = module_create();
	module_color(curveA, &blue);
	module_bezierCurve(curveA, &bc, divisions);

	// set the second curve
	point_set3D(&p[0], 0.0, 0.0, 0.0);
	point_set3D(&p[1], 0.0, 0.2, 1.0);
	point_set3D(&p[2], 0.2, 0.5, 0.7);
	point_set3D(&p[3], 1.0, 1.0, 1.0);
	bezierCurve_set(&bc, p);

	// put the curve into a module
	curveB = module_create();
	module_color(curveB, &green);
	module_bezierCurve(curveB, &bc, divisions);

	// create a module with six curves
	curves = module_create();
	for(i=0;i<3;i++) {
		module_module( curves, curveA );
		module_module( curves, curveB );
		module_rotateY( curves, cos(2.0*M_PI/3.0), sin(2.0*M_PI/3.0) );
	}

	// set up the drawstate
	drawstate_setColor(&ds, white);

	// set up the view
	point_set3D(&(view.vrp), 0.0, 0.5, -3.0 );
	vector_set( &(view.vpn), 0.0, 0.0, 1.0 );
	vector_set( &(view.vup), 0.0, 1.0, 0.0 );
	view.d = 1.0;
	view.du = 1.0;
	view.dv = 1.0*rows/cols;
	view.screeny = rows;
	view.screenx = cols;
	view.f = 0.0;
	view.b = 3.0;

	matrix_setView3D( &VTM, &view );
	matrix_identity( &GTM );

	// Create the animation by adjusting the GTM
	for(frame=0;frame<60;frame++) {
		char buffer[256];
		
		matrix_rotateY(&GTM, cos(M_PI/30.0), sin(M_PI/30.0) );
		module_draw( curves, &VTM, &GTM, &ds, NULL, src );

		sprintf(buffer, "bez3d-frame%03d.ppm", frame);
		image_write(src, buffer);
		image_reset(src);
	}
	
	printf("converting to gif...\n");
	system("convert -delay 1.5 -loop 0 bez3d-frame*.ppm bez3d.gif");
	system("rm bez3d-frame*.ppm");

	// clean up
	image_free( src );

	module_delete( curveA );
	module_delete( curveB );
	module_delete( curves );

	return(0);
}
Esempio n. 12
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);
}
Esempio n. 13
0
/*we are going to draw a cube using our new module hiearchy*/
int main( int argc, char *argv[]){
  Image *src; 
  Module *cube1;
  Module *cube2;
  Module *cube3;
  Module *cube4; 
  Module *cube5;
  Module *cube6;
  Module *cube7;
  Module *cube8;
  Module *manyCubes;
  Color Blue; 
  View3D view;
  Matrix vtm, gtm;
  int rows = 320;
  int cols = 320;
  DrawState *ds;
  int count;
  char filename[256];

  //Set the colors up
  color_set(&Blue, 0.0, 0, 1.0);
  
  // set up the view
  point_set( &(view.vrp), 1, 2, 0,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 = 5;
  view.du = 5;
  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 );

  for (count = 0; count< 205; count++){
    //Start first cube
    cube1 = module_create();
    module_color( cube1, &Blue);
    module_translate(cube1, 0, 0, 0-count*0.01);
    module_cube(cube1, 0);

    //Set the colors up
    color_set(&Blue, 0.1, 0.1, 1.0);
    //Start second cube
    cube2 = module_create();
    module_color(cube2, &Blue);
    module_translate(cube2, 0, 0, 0+count*0.01);
    module_cube(cube2, 0);

    //Set the colors up
    color_set(&Blue, 0.2, 0.2, 1.0);
    //start third cube
    cube3 = module_create();
    module_color(cube3, &Blue);
    module_translate(cube3, 0+count*0.01,0, 0);
    module_cube(cube3, 0);

    //Set the colors up
    color_set(&Blue, 0.3, 0.3, 1.0);
    //start fourth cube
    cube4 = module_create();
    module_color(cube4, &Blue);
    module_translate(cube4, 0-count*0.01,0, 0);
    module_cube(cube4, 0);

    //Set the colors up
    color_set(&Blue, 0.4, 0.4, 1.0);
    //start fifth cube
    cube5 = module_create();
    module_color(cube5, &Blue); 
    module_translate(cube5, 0-count*0.01, 0, 0-count*0.01);
    module_cube(cube5, 0);

    //Set the colors up
    color_set(&Blue, 0.5, 0.5, 1.0);
    cube6 = module_create();
    module_color(cube6, &Blue); 
    module_translate(cube6, 0+count*0.01, 0, 0+count*0.01);
    module_cube(cube6, 0);

    //Set the colors up
    color_set(&Blue, 0.6, 0.6, 1.0);
    cube7 = module_create();
    module_color(cube7, &Blue); 
    module_translate(cube7, 0-count*0.01, 0, 0+count*0.01);
    module_cube(cube7, 0);

    //Set the colors up
    color_set(&Blue, 0.7, 0.7, 1.0);
    cube8 = module_create();
    module_color(cube8, &Blue); 
    module_translate(cube8, 0+count*0.01, 0, 0-count*0.01);
    module_cube(cube8, 0);


    //put the cubes into a scene! 
    manyCubes = module_create();
    module_module(manyCubes, cube5);
    module_module(manyCubes, cube6);
    module_module(manyCubes, cube7);
    module_module(manyCubes, cube8);
    module_module(manyCubes, cube1);
    module_module(manyCubes, cube2);
    module_module(manyCubes, cube3);
    module_module(manyCubes, cube4);

    //Create the image draw state
    src = image_create(rows, cols);
    ds = drawstate_create();
    ds->shade = ShadeConstant;

    //Draw the scene
    module_draw( manyCubes, &vtm, &gtm, ds, NULL, src );
    //write out the scene
    sprintf(filename, "/export/home/vedwards/Desktop/Graphics/images/blueCube/frame-%04d.ppm", count );
    printf("Writing image\n");
    image_write( src, filename );

    //image_write(src, "blueCubeNew.ppm");
  }
  //free the modules
  module_delete( manyCubes );
  module_delete( cube1 );
  module_delete( cube2 );
  module_delete( cube3 );
  module_delete( cube4 );
  module_delete( cube5 );
  module_delete( cube6 );
  module_delete( cube7 );
  module_delete( cube8 );

  //free the drawState
  free(ds);

  //free the image
  image_free( src );

  return(0);

}
Esempio n. 14
0
/*
  Program to test polygon functionality
*/
int main(int argc, char *argv[]) {
  Image *src;
  const int rows = 300;
  const int cols = 400;
  Polygon *p;
  Color Red;
  Color Orange;
  Color White;
  Color Blue;
  Point pt[100];
  int i;
  
  srand(42);

  color_set(&Red, 0.9, 0.2, 0.1 );
  color_set(&Orange, 0.95, 0.7, 0.3 );
  color_set(&White, 1.0, 1.0, 1.0 );
  color_set(&Blue, 0.2, 0.1, 0.95 );

  src = image_create(rows, cols);

  // make a simple square to test proper areas and locations
  // the square ought to be 20x20, include pixel (30,30) and exclude pixel (50, 50)
  point_set2D(&(pt[0]), 30, 30);
  point_set2D(&(pt[1]), 50, 30);
  point_set2D(&(pt[2]), 50, 50);
  point_set2D(&(pt[3]), 30, 50);

  p = polygon_createp(4, pt);

  printf("drawing a square\n");
  polygon_drawFill(p, src, Blue);

  // something more interesting
  for(i=0;i<50;i++) {
    float dr = rand() % 20;
    point_set2D(&(pt[i]), 
    200 + cos((float)i * M_PI * 2.0 / 50.0)*(70 + dr),
    150 + sin((float)i * M_PI * 2.0 / 50.0)*(70 + dr));
  }
  polygon_set(p, 50, pt);

  printf("drawing first big polygon\n");
  polygon_drawFill(p, src, Red);

  for(i=0;i<50;i++) {
    float dr = rand() % 15;
    point_set2D(&(pt[i]), 
    200 + cos((float)i * M_PI * 2.0 / 50.0)*(50 + dr),
    150 + sin((float)i * M_PI * 2.0 / 50.0)*(50 + dr));
  }
  polygon_set(p, 50, pt);

  printf("drawing second big polygon\n");
  polygon_drawFill(p, src, Orange);

  for(i=0;i<50;i++) {
    float dr = rand() % 10;
    point_set2D(&(pt[i]), 
    200 + cos((float)i * M_PI * 2.0 / 50.0)*(30 + dr),
    150 + sin((float)i * M_PI * 2.0 / 50.0)*(30 + dr));
  }
  polygon_set(p, 50, pt);

  printf("drawing third big polygon\n");
  polygon_drawFill(p, src, White);

  printf("writing output\n");
  image_write(src, "test4a.ppm");

  image_free(src);

  return(0);
}