Exemplo n.º 1
0
/*======== void my_main() ==========
 Inputs:   int polygons
 Returns:
 
 This is the main engine of the interpreter, it should
 handle most of the commadns in mdl.
 
 If frames is not present in the source (and therefore
 num_frames is 1, then process_knobs should be called.
 
 If frames is present, the enitre op array must be
 applied frames time. At the end of each frame iteration
 save the current screen to a file named the
 provided basename plus a numeric string such that the
 files will be listed in order, then clear the screen and
 reset any other data structures that need it.
 
 Important note: you cannot just name your files in
 regular sequence, like pic0, pic1, pic2, pic3... if that
 is done, then pic1, pic10, pic11... will come before pic2
 and so on. In order to keep things clear, add leading 0s
 to the numeric portion of the name. If you use sprintf,
 you can use "%0xd" for this purpose. It will add at most
 x 0s in front of a number, if needed, so if used correctly,
 and x = 4, you would get numbers like 0001, 0002, 0011,
 0487
 
 05/17/12 09:41:35
 jdyrlandweaver
 ====================*/
void my_main( int polygons ) {
    
    int i, f, j, n;
    double step;
    double xval, yval, zval, knob_value;
    struct matrix *transform;
    struct matrix *tmp;
    struct stack *s;
    struct vary_node ** knobs;
    struct vary_node * link;
    screen t;
    color g;
    char q;
    
    num_frames = 1;
    step = 0.05;
    
    g.red = 0;
    g.green = 255;
    g.blue = 255;
    
    s = new_stack();
    tmp = new_matrix(4, 1000);
    clear_screen( t );
    
    first_pass();
    
    if(num_frames > 1) {
        knobs = second_pass();
    }
    
    int toomanyvariables;
    for(toomanyvariables = 0; toomanyvariables < num_frames; toomanyvariables++) {
        s = new_stack();
        tmp = new_matrix(4, 1000);
        clear_screen( t );
        
        for (j = 0; j < lastsym; j++) {
            if (symtab[j].type == SYM_VALUE) {
                link = knobs[toomanyvariables];
                
                while (strcmp(link->name, symtab[j].name) != 0) {
                    link = link->next;
                }
                
                if(link) {
                    (&symtab[j])->s.value = link->value;
                }
            }
        }
        
        for (i=0;i<lastop;i++) {
            
            switch (op[i].opcode) {
                    
                case SET:
                    n = op[i].op.set.p->s.value;
                    set_value(lookup_symbol(op[i].op.set.p->name),n);
                    break;
                    
                case SETKNOBS:
                    n = op[i].op.setknobs.value;
                    for (j = 0; j < lastsym; j++) {
                        if (symtab[j].type == SYM_VALUE) {
                            set_value(&(symtab[j]), n);
                        }
                    }
                    break;
                    
                case SPHERE:
                    add_sphere( tmp,op[i].op.sphere.d[0], //cx
                               op[i].op.sphere.d[1],  //cy
                               op[i].op.sphere.d[2],  //cz
                               op[i].op.sphere.r,
                               step);
                    //apply the current top origin
                    matrix_mult( s->data[ s->top ], tmp );
                    draw_polygons( tmp, t, g );
                    tmp->lastcol = 0;
                    break;
                    
                case TORUS:
                    add_torus( tmp, op[i].op.torus.d[0], //cx
                              op[i].op.torus.d[1],     //cy
                              op[i].op.torus.d[2],    //cz
                              op[i].op.torus.r0,
                              op[i].op.torus.r1,
                              step);
                    matrix_mult( s->data[ s->top ], tmp );
                    draw_polygons( tmp, t, g );
                    tmp->lastcol = 0;
                    break;
                    
                case BOX:
                    add_box( tmp, op[i].op.box.d0[0],
                            op[i].op.box.d0[1],
                            op[i].op.box.d0[2],
                            op[i].op.box.d1[0],
                            op[i].op.box.d1[1],
                            op[i].op.box.d1[2]);
                    matrix_mult( s->data[ s->top ], tmp );
                    draw_polygons( tmp, t, g );
                    tmp->lastcol = 0;
                    break;
                    
                case LINE:
                    add_edge( tmp, op[i].op.line.p0[0],
                             op[i].op.line.p0[1],
                             op[i].op.line.p0[1],
                             op[i].op.line.p1[0],
                             op[i].op.line.p1[1],
                             op[i].op.line.p1[1]);
                    draw_lines( tmp, t, g );
                    tmp->lastcol = 0;
                    break;
                    
                case MOVE:
                    
                    //get the factors
                    xval = op[i].op.move.d[0];
                    yval =  op[i].op.move.d[1];
                    zval = op[i].op.move.d[2];
                    
                    if (op[i].op.move.p) {
                        SYMTAB * variable = (lookup_symbol(op[i].op.move.p->name));
                        xval = xval * variable->s.value;
                        yval = yval * variable->s.value;
                        zval = zval * variable->s.value;
                    }
                    
                    transform = make_translate( xval, yval, zval );
                    //multiply by the existing origin
                    matrix_mult( s->data[ s->top ], transform );
                    //put the new matrix on the top
                    copy_matrix( transform, s->data[ s->top ] );
                    free_matrix( transform );
                    break;
                    
                case SCALE:
                    
                    xval = op[i].op.scale.d[0];
                    yval = op[i].op.scale.d[1];
                    zval = op[i].op.scale.d[2];
                    
                    if (op[i].op.scale.p) {
                        SYMTAB * variable = (lookup_symbol(op[i].op.scale.p->name));
                        xval = xval * variable->s.value;
                        yval = yval * variable->s.value;
                        zval = zval * variable->s.value;
                    }
                    
                    transform = make_scale( xval, yval, zval );
                    matrix_mult( s->data[ s->top ], transform );
                    //put the new matrix on the top
                    copy_matrix( transform, s->data[ s->top ] );
                    free_matrix( transform );
                    break;
                    
                case ROTATE:
                    
                    xval = op[i].op.rotate.degrees * ( M_PI / 180 );
                    
                    if (op[i].op.rotate.p) {
                        SYMTAB * variable = (lookup_symbol(op[i].op.rotate.p->name));
                        xval = xval * variable->s.value;
                    }
                    
                    //get the axis
                    if ( op[i].op.rotate.axis == 0 )
                        transform = make_rotX( xval );
                    else if ( op[i].op.rotate.axis == 1 )
                        transform = make_rotY( xval );
                    else if ( op[i].op.rotate.axis == 2 )
                        transform = make_rotZ( xval );
                    
                    matrix_mult( s->data[ s->top ], transform );
                    //put the new matrix on the top
                    copy_matrix( transform, s->data[ s->top ] );
                    free_matrix( transform );
                    break;
                    
                case PUSH:
                    push( s );
                    break;
                case POP:
                    pop( s );
                    break;
                case SAVE:
                    save_extension( t, op[i].op.save.p->name );
                    break;
                case DISPLAY:
                    display( t );
                    break;
            }
        }
        
        char directoryname[256];
        char index[256];
        
        strcpy(directoryname, "animations/");
        strcat(directoryname, name);
        sprintf(index, "%03d", toomanyvariables);
        strcat(directoryname, index);
        strcat(directoryname, ".png");
        save_extension(t, directoryname);
        printf("Saved %s\n", directoryname);
        
    }
    
    
    free_stack( s );
    free_matrix( tmp );
    //free_matrix( transform );
}
Exemplo n.º 2
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f = fopen(filename,"r"); 

  //if there's no such file name, print message and exit
  if(f == NULL)
    {
      printf("You're giving me nothin', boy.");
      exit(1);
    }
  char *boof = (char*)malloc(50*sizeof(char)); //standard buffer to read from
  double *pts = (double*)malloc(6*sizeof(double)); //incase arguments have been indicated that are individual
                 //points
  color c; //felt i had to make a color
  c.red = MAX_COLOR;
  c.green = MAX_COLOR;
  c.blue = MAX_COLOR;
  
  
  while( fgets(boof,50,f) )
    {
      if( boof[0] == 'l') //add a line to edge matrix
	{ 
	  get_args(f,pts);
	  add_edge(pm,pts[0],pts[1],pts[2],pts[3],pts[4],pts[5]);
	}
      else if( boof[0] == 'i' ) //create an identity matrix
	{
	  ident(transform);
	}
      else if( boof[0] == 's' ) //scale matrix
	{
	  get_args(f,pts);
	 struct matrix *m = make_scale(pts[0],pts[1],pts[2]);
	 matrix_mult(m,transform);
	 free_matrix(m);
	 
	}
      else if( boof[0] == 't' ) //translate matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_translate(pts[0],pts[1],pts[2]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	}
       else if( boof[0] == 'x' ) //rotation about x matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_rotX(pts[0]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	} 
       else if( boof[0] == 'y' ) //rotation about y matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_rotY(pts[0]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	} 
       else if( boof[0] == 'z' ) //rotation about z matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_rotZ(pts[0]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	}
       else if( boof[0] == 'a' )
	 {
	   matrix_mult(transform,pm);
	 }
       else if( boof[0] == 'v')
	 {
	   clear_screen(s);
	   draw_lines(pm,s,c);
	   display(s);
	 }
       else if( boof[0] == 'g')
	 {
	   draw_lines(pm,s,c);
	   fgets(boof,50,f);
	   save_extension(s,boof);
	 }
       else if( boof[0] == 'q')
	 {
	   printf("Now exiting ...");
	   exit(1);
	 }
       else
	 {
	   printf("What the hell does %c mean?\n",boof[0]);
	   return;
	 }
    }
  }
Exemplo n.º 3
0
int main() {

  screen s;
  struct matrix *edges;
  struct matrix *transform;

  color c;

  edges = new_matrix(4, 4);
  transform = new_matrix(4, 4);

  // TEST: Translation matrix
  struct matrix *transTest;
  transTest = make_translate(2,1,3.5);
  print_matrix(transTest);

  //TEST: Scale matrix
  struct matrix *scaleTest;
  scaleTest = make_scale(3,8,5);
  print_matrix(scaleTest);

  //TEST: Rotation about x-axis
  struct matrix *rotateX;
  rotateX = make_rotX(30);
  print_matrix(rotateX);

  //TEST: Rotation about y-axis
  struct matrix *rotateY;
  rotateY = make_rotY(60);
  print_matrix(rotateY);

  //TEST: Rotation about z-axis
  struct matrix *rotateZ;
  rotateZ = make_rotZ(45);
  print_matrix(rotateZ);  

  //add_edge(transform,3,3,3,2,2,2);

  /*
  c.red = 170;
  c.green = 240;
  c.blue = 30;
  */

  c.red = 50;
  c.green = 120;
  c.blue = 240;

  int i,j;
  for(i = 0; i < XRES; i++){
    for(j = 0; j < YRES; j++){
      plot(s,c,i,j);
    }
  }

  parse_file( "script_c", transform, edges, s );

  print_matrix(edges);
  print_matrix(transform);

  free_matrix( transform );
  free_matrix( edges );
}  
Exemplo n.º 4
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguments (x0, y0, z0, x1, y1, z1)
         b: add a hermite curve to the edge matrix - 
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         h: add a bezier to the edge matrix - 
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         c: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
         m: add a sphere to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
         d: add a torus to the edge matrix - 
	    takes 4 arguments (cx, cy, r1, r2)
         p: add a rectangular prism to the edge matrix - 
	    takes 6 arguments (x, y, z, width, height, depth)
	 w: clear the current edge matrix -
	    takes 0 arguments
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

03/08/12 16:22:10
jdyrlandweaver
====================*/
void
parse_file (char *filename,
	    struct matrix *transform, struct matrix *pm, screen s)
{

  FILE *f;
  char line[256];
  struct matrix *tmp;
  double angle;
  color g;

  g.red = 0;
  g.green = 255;
  g.blue = 0;

  clear_screen (s);

  if (strcmp (filename, "stdin") == 0)
    f = stdin;
  else
    f = fopen (filename, "r");

  while (fgets (line, 255, f) != NULL)
    {
      line[strlen (line) - 1] = '\0';
      //printf(":%s:\n",line);
      char c;
      double x, y, z, x1, y1, z1, x2, y2, x3, y3, x4, y4;

      c = line[0];

      switch (c)
	{
	case 'l':
	  //      printf("LINE!\n");
	  fgets (line, 255, f);
	  //      printf("\t%s", line);
	  //line[strlen(line)-1]='\0';
	  sscanf (line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
	  add_edge (pm, x, y, z, x1, y1, z1);
	  // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
	  break;
	case 'p':
	  fgets (line, 255, f);
	  sscanf (line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
	  add_box (pm, x, y, z, x1, y1, z1);
	  // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
	  break;
	case 'm':
	  fgets (line, 255, f);
	  sscanf (line, "%lf %lf %lf", &x, &y, &z);
	  add_sphere (pm, x, y, z, 0.05);
	  //printf( "%lf %lf %lf\n", x, y, z);
	  break;
	case 'd':
	  fgets (line, 255, f);
	  sscanf (line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
	  add_torus (pm, x, y, z, z1, 0.05);
	  //printf( "%lf %lf %lf\n", x, y, z);
	  break;
	case 'c':
	  fgets (line, 255, f);
	  sscanf (line, "%lf %lf %lf", &x, &y, &z);
	  add_circle (pm, x, y, z, 0.01);
	  //printf( "%lf %lf %lf\n", x, y, z);
	  break;
	case 'b':
	  fgets (line, 255, f);
	  sscanf (line, "%lf %lf %lf %lf %lf %lf %lf %lf",
		  &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
	  add_curve (pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE);
	  //printf( "%lf %lf %lf\n", x, y, z);
	  break;
	case 'h':
	  fgets (line, 255, f);
	  sscanf (line, "%lf %lf %lf %lf %lf %lf %lf %lf",
		  &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
	  add_curve (pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE);
	  //printf( "%lf %lf %lf\n", x, y, z);
	  break;
	case 's':
	  //printf("SCALE\n");
	  fgets (line, 255, f);
	  //line[strlen(line)-1]='\0';      
	  sscanf (line, "%lf %lf %lf", &x, &y, &z);
	  tmp = make_scale (x, y, z);
	  matrix_mult (tmp, transform);
	  //print_matrix(transform);
	  break;
	case 't':
	  //printf("TRANSLATE\n");
	  fgets (line, 255, f);
	  //      line[strlen(line)-1]='\0';      
	  sscanf (line, "%lf %lf %lf", &x, &y, &z);
	  tmp = make_translate (x, y, z);
	  matrix_mult (tmp, transform);
	  //print_matrix(transform);
	  break;
	case 'x':
	  //printf("ROTATE!\n");
	  fgets (line, 255, f);
	  sscanf (line, "%lf", &angle);
	  angle = angle * (M_PI / 180);
	  tmp = make_rotX (angle);
	  matrix_mult (tmp, transform);
	  break;
	case 'y':
	  //printf("ROTATE!\n");
	  fgets (line, 255, f);
	  sscanf (line, "%lf", &angle);
	  angle = angle * (M_PI / 180);
	  tmp = make_rotY (angle);
	  matrix_mult (tmp, transform);
	  break;
	case 'z':
	  //printf("ROTATE!\n");
	  fgets (line, 255, f);
	  sscanf (line, "%lf", &angle);
	  angle = angle * (M_PI / 180);
	  tmp = make_rotZ (angle);
	  matrix_mult (tmp, transform);
	  break;
	case 'i':
	  ident (transform);
	  break;
	case 'a':
	  //printf("APPLY!\n");
	  //print_matrix( transform );
	  //      print_matrix(pm);
	  matrix_mult (transform, pm);
	  break;
	case 'v':

	  /*
	     clear_screen(s);
	     draw_lines(pm, s, g);
	     display(s);
	     break;
	   */

	  // Second version for triangles:
	  clear_screen (s);
	  draw_polygons (pm, s, g);
	  display (s);
	  break;

	case 'w':
	  pm->lastcol = 0;
	  break;
	case 'g':
	  fgets (line, 255, f);
	  // line[strlen(line)-1] = '\0';
	  clear_screen (s);
	  draw_polygons (pm, s, g);
	  save_extension (s, line);
	  break;
	case 'q':
	  return;
	case '#':
	  break;
	default:
	  printf ("Invalid command\n");
	  break;
	}
    }

  free_matrix (tmp);
  fclose (f);
  //printf("END PARSE\n");
}
Exemplo n.º 5
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {
int i, j;
zbuf = (double **)malloc(500 * sizeof(double*));
for(i = 0; i < 500; i++){
zbuf[i] = (double*)malloc(500 * sizeof(double));
for (j = 0; j < 500; j++){
zbuf[i][j] = -DBL_MAX;
}
}
lclist = (lcons*)malloc(sizeof(lcons));
lclist->next = NULL;
i = 0;
j = 0;
  int f;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;
obj = 0;


cbmt[0] = ib;
cbmt[1] = im;
cbmt[2] = it;

int jkl = 0;
for ( i=0; i < lastsym; i++ ) {

    if ( symtab[i].type == SYM_LIGHT ) {
      jkl++;
    }
  }
ptl = jkl;

ptlights = (double**)malloc(j * sizeof(double*));

for (i = 0; i < jkl; i++){
ptlights[i] = (double*)malloc(6 * sizeof(double));
}

jkl = 0;
for ( i=0; i < lastsym; i++ ) {

    if ( symtab[i].type == SYM_LIGHT ) {
ptlights[jkl][0] = symtab[i].s.l->l[0];
ptlights[jkl][1] = symtab[i].s.l->l[1];
ptlights[jkl][2] = symtab[i].s.l->l[2];
ptlights[jkl][3] = symtab[i].s.l->c[0];
ptlights[jkl][4] = symtab[i].s.l->c[1];
ptlights[jkl][5] = symtab[i].s.l->c[2];
jkl++;
    }
  }

for(i = 0;i < ptl; i++){
printf("%lf,%lf,%lf light at (%lf,%lf,%lf)\n",ptlights[i][3],ptlights[i][4],ptlights[i][5],ptlights[i][0],ptlights[i][1],ptlights[i][2]);
}

  clear_screen( t );
  num_frames = 1;
  step = 0.02;
 
  g.red = 40;
  g.green = 50;
  g.blue = 60;

  s = new_stack();
  tmp = new_matrix(4, 1000);

first_pass();
srand(time(NULL));


if (num_frames < 0){
printf("ERROR- can't vary knobs when there's only 1 frame.\n");
}
if (num_frames >= 1){
struct vary_node ** klist = second_pass();
for (f = 0; f < num_frames; f++){
for ( i = 0; i < 500; i++){
for (j = 0; j < 500; j++){
zbuf[i][j] = -DBL_MAX;
}
}
  for (i=0;i<lastop;i++) {
setc = 0;



struct vary_node* v = klist[f];
SYMTAB * vvv;
while ( v  != NULL){
//printf("set %s %lf \n",v->name, v->value );
vvv = lookup_symbol(v->name);
if (vvv != NULL){
set_value(vvv, v->value);
}
else{
add_symbol(v->name,SYM_VALUE,(void *)&(v->value));
}
v = v->next;
}


  //print_knobs();
    switch (op[i].opcode) {
case SETKNOBS:
xval = 0;
	double abcdef = op[i].op.setknobs.value;
//printf("Setting knobs to %lf.\n",abcdef);
for ( i=0; i < lastsym; i++ ) {

    if ( symtab[i].type == SYM_VALUE ) {
      set_value(&(symtab[i]), abcdef);
    }
  }
break;
case AMBIENT:
g.red = op[i].op.ambient.c[0];
g.green = op[i].op.ambient.c[1];
g.blue = op[i].op.ambient.c[2];
break;
    case SPHERE:
      add_sphere( tmp,op[i].op.sphere.d[0], //cx
		  op[i].op.sphere.d[1],  //cy
		  op[i].op.sphere.d[2],  //cz
		  op[i].op.sphere.r,
		  step);
      //apply the current top origin
      matrix_mult( s->data[ s->top ], tmp );
///////////////////////////lcons
if (f == 0){
lcons * v = lclist;
while (v->next != NULL){
v = v->next;
}
v->next = (lcons*)malloc(sizeof(lcons));
v->next->next = NULL;
v->next->kar = .685* rand() / (double)RAND_MAX;
v->next->kag = .685* rand() / (double)RAND_MAX;
v->next->kab = .685* rand() / (double)RAND_MAX;
v->next->kdr = .685* rand() / (double)RAND_MAX;
v->next->kdg = .685* rand() / (double)RAND_MAX;
v->next->kdb = .685* rand() / (double)RAND_MAX;
v->next->ksr = .685* rand() / (double)RAND_MAX;
v->next->ksg = .685* rand() / (double)RAND_MAX;
v->next->ksb = .685* rand() / (double)RAND_MAX;
}
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
obj++;
      break;

    case TORUS:
      add_torus( tmp, op[i].op.torus.d[0], //cx
		 op[i].op.torus.d[1],     //cy
		 op[i].op.torus.d[2],    //cz
		 op[i].op.torus.r0,
		 op[i].op.torus.r1,
		 step);
      matrix_mult( s->data[ s->top ], tmp );
///////////////////////////lcons
if (f == 0){
lcons * v = lclist;
while (v->next != NULL){
v = v->next;
}
v->next = (lcons*)malloc(sizeof(lcons));
v->next->next = NULL;
v->next->kar = .685* rand() / (double)RAND_MAX;
v->next->kag = .685* rand() / (double)RAND_MAX;
v->next->kab = .685* rand() / (double)RAND_MAX;
v->next->kdr = .685* rand() / (double)RAND_MAX;
v->next->kdg = .685* rand() / (double)RAND_MAX;
v->next->kdb = .685* rand() / (double)RAND_MAX;
v->next->ksr = .685* rand() / (double)RAND_MAX;
v->next->ksg = .685* rand() / (double)RAND_MAX;
v->next->ksb = .685* rand() / (double)RAND_MAX;
}
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
obj++;
      break;

    case BOX:
      add_box( tmp, op[i].op.box.d0[0],
	       op[i].op.box.d0[1],
	       op[i].op.box.d0[2],
	       op[i].op.box.d1[0],
	       op[i].op.box.d1[1],
	       op[i].op.box.d1[2]);
      matrix_mult( s->data[ s->top ], tmp );
///////////////////////////lcons
if (f == 0){
lcons * v = lclist;
while (v->next != NULL){
v = v->next;
}
v->next = (lcons*)malloc(sizeof(lcons));
v->next->next = NULL;
v->next->kar = .685* rand() / (double)RAND_MAX;
v->next->kag = .685* rand() / (double)RAND_MAX;
v->next->kab = .685* rand() / (double)RAND_MAX;
v->next->kdr = .685* rand() / (double)RAND_MAX;
v->next->kdg = .685* rand() / (double)RAND_MAX;
v->next->kdb = .685* rand() / (double)RAND_MAX;
v->next->ksr = .685* rand() / (double)RAND_MAX;
v->next->ksg = .685* rand() / (double)RAND_MAX;
v->next->ksb = .685* rand() / (double)RAND_MAX;
}
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
obj++;
      break;

    case LINE:
      add_edge( tmp, op[i].op.line.p0[0],
		op[i].op.line.p0[1],
		op[i].op.line.p0[2],
		op[i].op.line.p1[0],
		op[i].op.line.p1[1],
		op[i].op.line.p1[2]);
matrix_mult( s->data[ s->top ], tmp );
///////////////////////////lcons
if (f == 0){
lcons * v = lclist;
while (v->next != NULL){
v = v->next;
}
v->next = (lcons*)malloc(sizeof(lcons));
v->next->next = NULL;
v->next->kar = .685* rand() / (double)RAND_MAX;
v->next->kag = .685* rand() / (double)RAND_MAX;
v->next->kab = .685* rand() / (double)RAND_MAX;
v->next->kdr = .685* rand() / (double)RAND_MAX;
v->next->kdg = .685* rand() / (double)RAND_MAX;
v->next->kdb = .685* rand() / (double)RAND_MAX;
v->next->ksr = .685* rand() / (double)RAND_MAX;
v->next->ksg = .685* rand() / (double)RAND_MAX;
v->next->ksb = .685* rand() / (double)RAND_MAX;
}
      draw_lines( tmp, t, g );
      tmp->lastcol = 0;
obj++;
      break;

    case MOVE:
      //get the factors
      xval = op[i].op.move.d[0];
      yval =  op[i].op.move.d[1];
      zval = op[i].op.move.d[2];
//printf("MOVE %lf %lf %lf\n",xval,yval,zval);
if (op[i].op.move.p != NULL){
SYMTAB* thing = (lookup_symbol(op[i].op.move.p->name));
xval *= thing->s.value;
yval *= thing->s.value;
zval *= thing->s.value;
//printf("new MOVE %lf %lf %lf\n",xval,yval,zval);
}

      transform = make_translate( xval, yval, zval );
      //multiply by the existing origin
      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];
      
//printf("scalE %lf %lf %lf\n",xval,yval,zval);
if (op[i].op.scale.p != NULL){
SYMTAB*  thing = (lookup_symbol(op[i].op.scale.p->name));
xval *= thing->s.value;
yval *= thing->s.value;
zval *= thing->s.value;
//printf("new scale %lf %lf %lf\n",xval,yval,zval);
}

      transform = make_scale( xval, yval, zval );
      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case ROTATE:
      xval = op[i].op.rotate.degrees * ( M_PI / 180 );
//printf("rotate %lf\n",xval);
if (op[i].op.rotate.p != NULL){
xval *= (lookup_symbol(op[i].op.rotate.p->name))->s.value;
//printf("new rotate%lf\n",xval);
}

      //get the axis
      if ( op[i].op.rotate.axis == 0 ) 
	transform = make_rotX( xval );
      else if ( op[i].op.rotate.axis == 1 ) 
	transform = make_rotY( xval );
      else if ( op[i].op.rotate.axis == 2 ) 
	transform = make_rotZ( xval );

      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case PUSH:
      push( s );
      break;
    case POP:
      pop( s );
      break;
    case SAVE:
      save_extension( t, op[i].op.save.p->name );
      break;
    case DISPLAY:
      display( t );
      break;
    }
  }

if (num_frames > 1){
char nopq[256];
char rst[256];
strcpy(nopq,"animations/");
strcat(nopq, name);
sprintf (rst, "%03d", f );
strcat(nopq,rst);
strcat(nopq,".png");
printf("Saved frame %d to %s\n", (f+1) ,nopq);
save_extension( t, nopq );
  clear_screen( t );
screen t;
if (f < num_frames - 1){
obj = 0;
  g.red = 40;
  g.green = 50;
  g.blue = 60;
}

  free_stack( s );
  free_matrix( tmp );

  s = new_stack();
  tmp = new_matrix(4, 1000);
}
//printf("finished frame %d\n",f);
}
  ////////////////////////////////////////////////////////////

for (j = 0; j < num_frames; j++){
struct vary_node * v2 = klist[j];
struct vary_node * v = klist[j];
while (v2  != NULL){
v = v2;
v2 = v2->next;
free(v);
}
}
  free(klist);
}

/////////////////////////////////////////////////////
	

lcons* v2 = lclist;
lcons* v = lclist;
while (v2  != NULL){
v = v2;
v2 = v2->next;
free(v);
}

////////////////////////////////////////////////
/*for ( i=0; i < lastsym; i++ ) {

    if ( symtab[i].type == SYM_LIGHT ) {
      //printf( "Freeing %s:\n", symtab[i].name);
//print_light(symtab[i].s.l);
      //free(symtab[i].s.l);
    }
  }*/
////////////////////////////////////////////////////
  free_stack( s );
  free_matrix( tmp );
for(i = 0; i < 500; i++){
free(zbuf[i]);
}
for (i = 0; i < ptl; i++){
free(ptlights[i]);
}
free(ptlights);
free(zbuf);
printf("ambient light =  %d,%d,%d\n",g.red,g.green,g.blue);
  //free_matrix( transform );    
}
Exemplo n.º 6
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 c: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 h: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 b: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {
    FILE *f = fopen(filename, "r");
    if (f == NULL) {
        printf("%s: %s\n", filename, strerror(errno));
    }

    color c;
    c.red = 255;
    c.green = 255;
    c.blue = 255;

    char buffer[100];
    while (fgets(buffer, 100, f)) {
        /*printf("%c\n", buffer[0]);*/
        switch (buffer[0]) {
            case 'l':
                {
                    int x0, y0, z0, x1, y1, z1;
                    fscanf(f, "%d %d %d %d %d %d\n", &x0, &y0, &z0, &x1, &y1, &z1);
                    add_edge(pm, x0, y0, z0, x1, y1, z1);
                    break;
                }
            case 'c':
                {
                    int cx, cy, r;
                    fscanf(f, "%d %d %d\n", &cx, &cy, &r);
                    add_circle(pm, cx, cy, r, .001);
                    break;
                }
            case 'h':
                {
                    double x0, y0, x1, y1, x2, y2, x3, y3;
                    fscanf(f, "%lf %lf %lf %lf %lf %lf %lf %lf\n", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3);
                    add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, .001, HERMITE_MODE);
                    break;
                }
            case 'b':
                {
                    double x0, y0, x1, y1, x2, y2, x3, y3;
                    fscanf(f, "%lf %lf %lf %lf %lf %lf %lf %lf\n", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3);
                    add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, .001, BEZIER_MODE);
                    break;
                }
            case 'i':
                transform = new_matrix(4, 4);
                ident(transform);
                break;
            case 's':
                {
                    double sx, sy, sz;
                    fscanf(f, "%lf %lf %lf\n", &sx, &sy, &sz);
                    struct matrix *scale = make_scale(sx, sy, sz);
                    matrix_mult(scale, transform);
                    break;
                }
            case 't':
                {
                    double tx, ty, tz;
                    fscanf(f, "%lf %lf %lf\n", &tx, &ty, &tz);
                    struct matrix *translate = make_translate(tx, ty, tz);
                    matrix_mult(translate, transform);
                    break;
                }
                break;
            case 'x':
                {
                    double theta;
                    fscanf(f, "%lf\n", &theta);
                    struct matrix *rotX = make_rotX(rad(theta));
                    matrix_mult(rotX, transform);
                    break;
                }
                break;
            case 'y':
                {
                    double theta;
                    fscanf(f, "%lf\n", &theta);
                    struct matrix *rotY = make_rotY(rad(theta));
                    matrix_mult(rotY, transform);
                    break;
                }
                break;
            case 'z':
                {
                    double theta;
                    fscanf(f, "%lf\n", &theta);
                    struct matrix *rotZ = make_rotZ(rad(theta));
                    matrix_mult(rotZ, transform);
                    break;
                }
                break;
            case 'a':
                matrix_mult(transform, pm);
                break;
            case 'v':
                clear_screen(s);
                draw_lines(pm, s, c);
                display( s );
                break;
            case 'g':
                {
                    clear_screen(s);
                    draw_lines(pm, s, c);
                    char filename_buffer[100];
                    fgets(filename_buffer, 100, f);
                    save_extension(s, filename_buffer);
                    break;
                }
            case 'q':
                return;
        }
    }
}
Exemplo n.º 7
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         sphere: add a sphere to the edge matrix - 
	    takes 3 arguemnts (cx, cy, r)
         torus: add a torus to the edge matrix - 
	    takes 4 arguemnts (cx, cy, r1, r2)
         box: add a rectangular prism to the edge matrix - 
	    takes 6 arguemnts (x, y, z, width, height, depth)
	 clear: clear the currnt edge matrix -
	    takes 0 arguments
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;
  struct stack* shakestack;  //relative coordinate system stack
  int draw_mode=-1; //equals 0 if draw_edges, equals 1 if draw_polygons
  
  shakestack = new_stack();
  
  g.red = 0;
  g.green = 255;
  g.blue = 0;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
    
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
	  draw_mode = 0;
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 0;
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      //printf("BEZIER\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 0;
    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 0;
    }
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
	  draw_mode = 1;
    }
    else if (strncmp(line, "sphere", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 10);
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 1;
    }
    else if (strncmp(line, "torus", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 10);
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 1;
    }
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      //matrix_mult(tmp, transform);
      //print_matrix(transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      //matrix_mult(tmp, transform);
      //print_matrix(transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      //matrix_mult(tmp, transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      //matrix_mult(tmp, transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      //matrix_mult(tmp, transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      //matrix_mult(transform, pm);
	  matrix_mult(shakestack->data[ shakestack->top ], pm);
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      clear_screen(s);
	  if( draw_mode==0 ) {
		draw_lines(pm, s, g);
	  }
	  else {
		draw_polygons(pm, s, g);
	  }
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      if( draw_mode==0 ) {
		draw_lines(pm, s, g);
	  }
	  else {
		draw_polygons(pm, s, g);
	  }
	  save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    //==================STACK COMMANDS=================================
    else if( strncmp(line, "push", strlen(line)) == 0 ) {
      push(shakestack);
      print_stack(shakestack);
    }
    else if( strncmp(line, "pop", strlen(line)) == 0 ) {
      pop(shakestack);
      print_stack(shakestack);
    }
    else if ( line[0] == '#' ) {
      printf("Invalid command\n");
    }
    //adding color coommand==================
    else if( strncmp(line, "color", strlen(line)) == 0 ) {
    	
    	fgets(line, 255, f);
    	sscanf(line, "%i %i %i", &x, &y, &z);
    	
   		g.red = x;
   		g.green = y;
   		g.blue = z;
   		
    }
	else {
		  printf("Invalid command\n");
    
	}
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
Exemplo n.º 8
0
/*======== void parse_file () ==========
Inputs:   char * filename 
struct matrix * transform, 
struct matrix * pm,
screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
Every command is a single character that takes up a line
Any command that requires arguments must have those arguments in the second line.
The commands are as follows:
l: add a line to the edge matrix - 
takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
c: add a circle to the edge matrix - 
takes 3 arguments (cx, cy, r)
h: add a hermite curve to the edge matrix -
takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
b: add a bezier curve to the edge matrix -
takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
i: set the transform matrix to the identity matrix - 
s: create a scale matrix, 
then multiply the transform matrix by the scale matrix - 
takes 3 arguments (sx, sy, sz)
t: create a translation matrix, 
then multiply the transform matrix by the translation matrix - 
takes 3 arguments (tx, ty, tz)
x: create an x-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
y: create an y-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
z: create an z-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
a: apply the current transformation matrix to the 
edge matrix
v: draw the lines of the edge matrix to the screen
display the screen
g: draw the lines of the edge matrix to the screen
save the screen to a file -
takes 1 argument (file name)
q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

03/08/12 16:22:10
jdyrlandweaver
====================*/
void parse_file ( char * filename, 
        struct matrix * transform, 
        struct matrix * pm,
        screen s) {

    FILE *f;
    char line[256];
    struct matrix * tmp;
    double angle;
    double step = .025;
    color g;

    g.red = 255;
    g.green = 80;
    g.blue = 80;

    clear_screen(s);

    if ( strcmp(filename, "stdin") == 0 ) 
        f = stdin;
    else
        f = fopen(filename, "r");

    while ( fgets(line, 255, f) != NULL ) {
        line[strlen(line)-1]='\0';
        //printf(":%s:\n",line);
        char c;
        double x, y, z, x0, y0, x1, y1, z1, x2, y2, x3, y3, cx, cy, r;

        c = line[0];

        switch (c) {
            case 'l':
                //      printf("LINE!\n");
                fgets(line, 255, f);
                //      printf("\t%s", line);
                //line[strlen(line)-1]='\0';
                sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
                add_edge(pm, x, y, z, x1, y1, z1);
                // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
                break;
            case 's':
                //printf("SCALE\n");
                fgets(line, 255, f);
                //line[strlen(line)-1]='\0';      
                sscanf(line, "%lf %lf %lf", &x, &y, &z);
                tmp = make_scale(x, y, z);
                matrix_mult(tmp, transform);
                //print_matrix(transform);
                break;
            case 't':
                //printf("TRANSLATE\n");
                fgets(line, 255, f);
                //      line[strlen(line)-1]='\0';      
                sscanf(line, "%lf %lf %lf", &x, &y, &z);
                tmp = make_translate(x, y, z);
                matrix_mult(tmp, transform);
                //print_matrix(transform);
                break;
            case 'x':
                //printf("ROTATE!\n");
                fgets(line, 255, f);
                sscanf(line, "%lf", &angle);
                angle = angle * (M_PI / 180);
                tmp = make_rotX( angle);
                matrix_mult(tmp, transform);
                break;
            case 'y':
                //printf("ROTATE!\n");
                fgets(line, 255, f);
                sscanf(line, "%lf", &angle);
                angle = angle * (M_PI / 180);
                tmp = make_rotY( angle);
                matrix_mult(tmp, transform);
                break;
            case 'z':
                //printf("ROTATE!\n");
                fgets(line, 255, f);
                sscanf(line, "%lf", &angle);
                angle = angle * (M_PI / 180);
                tmp = make_rotZ( angle);
                matrix_mult(tmp, transform);
                break;
            case 'c':
                fgets(line, 255, f);
                sscanf(line, "%lf %lf %lf", &cx, &cy, &r);
                add_circle(pm, cx, cy, r, step);
                break;
            case 'h':
                fgets(line, 255, f);
                sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3);
                add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 0);
                break;
            case 'b':
                fgets(line, 255, f);
                sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3);
                add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 1);
                break;
            case 'i':
                ident(transform);
                break;
            case 'a':
                //printf("APPLY!\n");
                //print_matrix( transform );
                //      print_matrix(pm);
                matrix_mult(transform, pm);
                break;
            case 'v':
                clear_screen(s);
                draw_lines(pm, s, g);
                display(s);
                break;
            case 'g':
                fgets(line, 255, f);
                // line[strlen(line)-1] = '\0';
                clear_screen(s);
                draw_lines(pm, s, g);
                save_extension(s, line);
                break;
            case 'q':
                return;
            default:
                printf("Invalid command\n");
                break;
        }
    }

    free_matrix(tmp);
    fclose(f);
    //printf("END PARSE\n");
}
Exemplo n.º 9
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         sphere: add a sphere to the edge matrix - 
	    takes 3 arguemnts (cx, cy, r)
         torus: add a torus to the edge matrix - 
	    takes 4 arguemnts (cx, cy, r1, r2)
         box: add a rectangular prism to the edge matrix - 
	    takes 6 arguemnts (x, y, z, width, height, depth)
	 clear: clear the currnt edge matrix -
	    takes 0 arguments
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;


  g.red = 0;
  g.green = 255;
  g.blue = 0;

  struct stack * original = new_stack();

  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      matrix_mult(original->data[original->top], tmp);
      draw_lines(tmp, s, g);
      //free_matrix(tmp);

      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
    }
    else if ( strncmp(line, "push", strlen(line)) == 0 ) {
      push(original);
    }
    else if ( strncmp(line, "pop", strlen(line)) == 0 ) {
      pop(original);
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      matrix_mult(original->data[original->top], tmp);
      draw_lines(tmp, s, g);
      //free_matrix(tmp);

      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      //printf("BEZIER\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      matrix_mult(original->data[original->top], tmp);
      draw_lines(tmp, s, g);

      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      matrix_mult(original->data[original->top], tmp);
      draw_lines(tmp, s, g);
      //printf( "%lf %lf %lf\n", x, y, z);
    }
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      matrix_mult(original->data[original->top], tmp);
      draw_polygons(tmp, s, g);

      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
    }
    else if (strncmp(line, "sphere", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 10);
      matrix_mult(original->data[original->top], tmp);
      draw_polygons(tmp, s, g);
      //printf( "%lf %lf %lf\n", x, y, z);
    }
    else if (strncmp(line, "torus", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 10);
      matrix_mult(original->data[original->top], tmp);
      draw_polygons(tmp, s, g);
      //printf( "%lf %lf %lf\n", x, y, z);
    }
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      matrix_mult(original->data[original->top], tmp);
      copy_matrix(tmp, original->data[original->top]);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      copy_matrix(tmp, original->data[original->top]);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(original->data[original->top], tmp);
      copy_matrix(tmp, original->data[original->top]);
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(original->data[original->top], tmp);
      copy_matrix(tmp, original->data[original->top]);
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(original->data[original->top], tmp);
      copy_matrix(tmp, original->data[original->top]);
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      //clear_screen(s);
      //draw_polygons(pm, s, g);
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      line[strlen(line)-1] = '\0';
      //clear_screen(s);
      //draw_polygons(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if ( line[0] != '#' ) {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
Exemplo n.º 10
0
Arquivo: my_main.c Projeto: stuydw/mdl
void my_main( int polygons ) {

    int i, ax;
    double step;
    double xval, yval, zval, x2, y2, z2;
    double w, h, d, r1, r2, m;
    struct matrix *transform;
    struct matrix *tmp;
    struct stack *s;
    screen t;
    color g;

    g.red = 250;
    g.blue = 0;
    g.green = 0;

    s = new_stack();
    tmp = new_matrix(4, 1000);
    clear_screen( t );

    for (i=0; i<lastop; i++) {
        switch (op[i].opcode) {

        case COMMENT:
            break;

        case PUSH:
            push(s);
            break;

        case POP:
            pop(s);
            break;

        case MOVE:
            xval = op[i].op.move.d[0];
            yval = op[i].op.move.d[1];
            zval = op[i].op.move.d[2];

            transform = make_translate(xval, yval, zval);
            matrix_mult(transform, s->data[s->top]);
            break;

        case SCALE:
            xval = op[i].op.scale.d[0];
            yval = op[i].op.scale.d[1];
            zval = op[i].op.scale.d[2];

            transform = make_scale(xval, yval, zval);
            matrix_mult(transform, s->data[s->top]);
            break;

        case ROTATE:
            ax = op[i].op.rotate.axis;
            m = op[i].op.rotate.degrees;

            if(ax == 0)
                transform = make_rotX(m);

            if(ax == 1)
                transform = make_rotY(m);

            if(ax ==2)
                transform = make_rotZ(m);

            matrix_mult(transform, s->data[s->top]);
            break;

        case BOX:
            xval = op[i].op.box.d0[0];
            yval = op[i].op.box.d0[1];
            zval = op[i].op.box.d0[2];
            w = op[i].op.box.d1[0];
            h = op[i].op.box.d1[1];
            d = op[i].op.box.d1[2];


            add_box(tmp, xval, yval, zval, w, h, d);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            break;

        case SPHERE:
            xval = op[i].op.sphere.d[0];
            yval = op[i].op.sphere.d[1];
            zval = op[i].op.sphere.d[2];
            r1 = op[i].op.sphere.r;

            add_sphere(tmp, xval, yval, zval, r1, .01);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            break;

        case TORUS:
            xval = op[i].op.torus.d[0];
            yval = op[i].op.torus.d[1];
            zval = op[i].op.torus.d[2];
            r1 = op[i].op.torus.r0;
            r2 = op[i].op.torus.r1;

            add_torus(tmp, xval, yval, zval, r1, r2, .01);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            break;

        case LINE:
            xval = op[i].op.line.p0[0];
            yval = op[i].op.line.p0[1];
            zval = op[i].op.line.p0[2];
            x2 = op[i].op.line.p1[0];
            y2 = op[i].op.line.p1[1];
            z2 = op[i].op.line.p1[2];

            add_edge(tmp, xval, yval, zval, x2, y2, z2);
            matrix_mult(s->data[s->top], tmp);
            draw_lines(tmp, t, g);
            break;

        case SAVE:
            save_extension(t, op[i].op.save.p->name);
            break;

        case DISPLAY:
            display(t);
            break;

        }
    }
}
Exemplo n.º 11
0
Arquivo: my_main.c Projeto: stuydw/mdl
void my_main( int polygons ) {

  int i, j;
  double step = 0.05; // Decided to make step this value
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;

  g = change_color(2);

  int axis; // 0,1,2 correspond to x,y,z axes of rotation, respectively
  double measure; // corresponds to angle of rotation

  double width, height, depth; // For box

  double radius; // For sphere

  double r1, r2; // For torus

  double x2,y2,z2; // For line
  
  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {

    case COMMENT:
      break;

    case PUSH:
      push(s);
      break;

    case POP:
      pop(s);
      break;

    case MOVE:
      xval = op[i].op.move.d[0];
      yval = op[i].op.move.d[1];
      zval = op[i].op.move.d[2];

      transform = make_translate(xval, yval, zval);      
      matrix_mult(s->data[s->top], transform);
      s->data[s->top] = transform;
      break;

    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];

      transform = make_scale(xval, yval, zval);
      matrix_mult(s->data[s->top], transform);
      s->data[s->top] = transform;
      break;

    case ROTATE:
      axis = op[i].op.rotate.axis;
      measure = op[i].op.rotate.degrees;

      if(axis == 0){
	transform = make_rotX(measure);}
      if(axis == 1){
	transform = make_rotY(measure);}
      if(axis == 2){
	transform = make_rotZ(measure);}

      matrix_mult(s->data[s->top], transform);
      s->data[s->top] = transform;
      break;

    case BOX:
      for(j = 0; j < tmp->lastcol; j++){
	tmp->m[0][j] = 0;
	tmp->m[1][j] = 0;
	tmp->m[2][j] = 0;
      }
      tmp->lastcol = 0;

      xval = op[i].op.box.d0[0];
      yval = op[i].op.box.d0[1];
      zval = op[i].op.box.d0[2];

      width = op[i].op.box.d1[0];
      height = op[i].op.box.d1[1];
      depth = op[i].op.box.d1[2];

      add_box(tmp, xval, yval, zval, width, height, depth);
      matrix_mult(s->data[s->top], tmp);
      //matrix_mult(transform, tmp);
      draw_polygons(tmp, t, g);
      //free_matrix(tmp);
      break;

    case SPHERE:
      for(j = 0; j < tmp->lastcol; j++){
	tmp->m[0][j] = 0;
	tmp->m[1][j] = 0;
	tmp->m[2][j] = 0;
      }
      tmp->lastcol = 0;

      xval = op[i].op.sphere.d[0];
      yval = op[i].op.sphere.d[1];
      zval = op[i].op.sphere.d[2];

      radius = op[i].op.sphere.r;

      add_sphere(tmp, xval, yval, zval, radius, step);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;

    case TORUS:
      for(j = 0; j < tmp->lastcol; j++){
	tmp->m[0][j] = 0;
	tmp->m[1][j] = 0;
	tmp->m[2][j] = 0;
      }
      tmp->lastcol = 0;

      xval = op[i].op.torus.d[0];
      yval = op[i].op.torus.d[1];
      zval = op[i].op.torus.d[2];

      r1 = op[i].op.torus.r0;
      r2 = op[i].op.torus.r1;

      add_torus(tmp, xval, yval, zval, r1, r2, step);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;

    case LINE:
      for(j = 0; j < tmp->lastcol; j++){
	tmp->m[0][j] = 0;
	tmp->m[1][j] = 0;
	tmp->m[2][j] = 0;
      }
      tmp->lastcol = 0;

      xval = op[i].op.line.p0[0];
      yval = op[i].op.line.p0[1];
      zval = op[i].op.line.p0[2];

      x2 = op[i].op.line.p1[0];
      y2 = op[i].op.line.p1[1];
      z2 = op[i].op.line.p1[2];

      add_edge(tmp, xval, yval, zval, x2, y2, z2);
      matrix_mult(s->data[s->top], tmp);
      draw_lines(tmp, t, g);
      break;

    case SAVE:
      save_extension(t, op[i].op.save.p->name);
      break;
      
    case DISPLAY:
      display(t);
      break;
      
    }
  }

  //free_stack(s);
  //free_matrix(transform);
}
Exemplo n.º 12
0
/*======== void my_main() ==========
  Inputs:
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {

  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;

  struct vary_node **knobs;
  struct vary_node *vn;
  char frame_name[128];
  
  num_frames = 1;
  step = 5;
 
  g.red = 0;
  g.green = 255;
  g.blue = 255;

  first_pass();
  if (num_frames>1){
    knobs = second_pass();
  }

  int cur_frame;
  char frame_num_string[4];
  
  for (cur_frame=0;cur_frame<num_frames-1;cur_frame++){
    strcpy(frame_name,"animation_frames/");
    strcat(frame_name,name);
    sprintf(frame_num_string,"%03d",cur_frame+1);
    strcat(frame_name,frame_num_string);

    s = new_stack();
    tmp = new_matrix(4,0);
    for (i=0;i<lastop;i++) {
      
      switch (op[i].opcode) {

      case SPHERE:
	add_sphere( tmp,op[i].op.sphere.d[0], //cx
		    op[i].op.sphere.d[1],  //cy
		    op[i].op.sphere.d[2],  //cz
		    op[i].op.sphere.r,
		    step);
	//apply the current top origin
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case TORUS:
	add_torus( tmp, op[i].op.torus.d[0], //cx
		   op[i].op.torus.d[1],      //cy
		   op[i].op.torus.d[2],      //cz
		   op[i].op.torus.r0,
		   op[i].op.torus.r1,
		   step);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case BOX:
	add_box( tmp, op[i].op.box.d0[0],
		 op[i].op.box.d0[1],
		 op[i].op.box.d0[2],
		 op[i].op.box.d1[0],
		 op[i].op.box.d1[1],
		 op[i].op.box.d1[2]);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case LINE:
	add_edge( tmp, op[i].op.line.p0[0],
		  op[i].op.line.p0[1],
		  op[i].op.line.p0[1],
		  op[i].op.line.p1[0],
		  op[i].op.line.p1[1],
		  op[i].op.line.p1[1]);
	draw_lines( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case SET:
	vn = knobs[cur_frame];
	while (vn != NULL){
	  if (strcmp(vn->name,op[i].op.set.p->name)==0){
	    vn->value = op[i].op.set.val;
	  }
	  vn = vn->next;
	}
	break;

      case SETKNOBS:
	vn = knobs[cur_frame];
	while (vn != NULL){
	  vn->value = op[i].op.setknobs.value;
	  vn = vn->next;
	}
	break;

      case MOVE:
	//get the factors
	xval = op[i].op.move.d[0];
	yval = op[i].op.move.d[1];
	zval = op[i].op.move.d[2];
	if (op[i].op.move.p != NULL){
	  vn = knobs[cur_frame];
	  while (vn != NULL){
	    if (strcmp(vn->name,op[i].op.scale.p->name)==0){
	      xval*=vn->value;
	      yval*=vn->value;
	      zval*=vn->value;
	    }
	    vn = vn->next;
	  }
	}
	transform = make_translate( xval, yval, zval );
	//multiply by the existing origin
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );

	break;

      case SCALE:

	xval = op[i].op.scale.d[0];
	yval = op[i].op.scale.d[1];
	zval = op[i].op.scale.d[2];

	if (op[i].op.scale.p != NULL){
	  vn = knobs[cur_frame];
	  while (vn != NULL){
	    if (strcmp(vn->name,op[i].op.scale.p->name)==0){
	      xval*=vn->value;
	      yval*=vn->value;
	      zval*=vn->value;
	    }
	    vn = vn->next;
	  }
	}
	transform = make_scale( xval, yval, zval );
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;
      case ROTATE:
	xval = op[i].op.rotate.degrees * ( M_PI / 180 );
	if (op[i].op.scale.p == NULL){
	  vn = knobs[cur_frame];
	  while (vn != NULL){
	    if (strcmp(vn->name,op[i].op.rotate.p->name)==0){
	      xval*=vn->value;
	    }
	    vn = vn->next;
	  }
	}
	//get the axis
	if ( op[i].op.rotate.axis == 0 ) 
	  transform = make_rotX( xval );
	else if ( op[i].op.rotate.axis == 1 ) 
	  transform = make_rotY( xval );
	else if ( op[i].op.rotate.axis == 2 ) 
	  transform = make_rotZ( xval );
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;
      case PUSH:
	push( s );
	break;
      case POP:
	pop( s );
	break;
      case SAVE:
	save_extension( t, op[i].op.save.p->name );
	break;
      case DISPLAY:
	display( t );
	break;
      }
    }
    if (num_frames > 1){
      save_extension( t, frame_name );
      clear_screen(t);
      free_stack( s );
      free_matrix( tmp );
    }
  }
}
Exemplo n.º 13
0
void my_main( int polygons ) {

  int i;
  double step;
  double xval, yval, zval, xcor, ycor, zcor, angle, radius;
  struct matrix *transform;
  struct matrix *tmp;
  screen t;
  color g;
  g.red = 0;
  g.green = 255;
  g.blue = 0;
  struct stack *s;
  
  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    printf("loop %d\n", i);
    switch (op[i].opcode){
    case PUSH:
      push(s);
      printf("%s\n", "pop\n");
      break;
    case POP:
      pop(s);
      printf("%s\n", "pop\n");
      break;
    case MOVE:
      xval = op[i].op.move.d[0];
      yval = op[i].op.move.d[1];
      zval = op[i].op.move.d[2];
      tmp = make_translate(xval, yval, zval);
      matrix_mult(s->data[s->top], tmp);
      copy_matrix(tmp, s->data[s->top]);
      printf("%s\n", "move\n");
      break;
    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];
      tmp = make_scale(xval, yval, zval);
      matrix_mult(s->data[s->top], tmp);
      copy_matrix(tmp, s->data[s->top]);
      printf("%s\n", "scale\n");
      break;
    case ROTATE:
      angle = op[i].op.rotate.degrees * M_PI / 180;
      if(op[i].op.rotate.axis == 0){
	tmp = make_rotX(angle);
	matrix_mult(s->data[s->top], tmp);
	copy_matrix(tmp, s->data[s->top]);
      }
      else if(op[i].op.rotate.axis == 1){
	tmp = make_rotY(angle);
	matrix_mult(s->data[s->top], tmp);
	copy_matrix(tmp, s->data[s->top]);
      }
      else{
	tmp = make_rotZ(angle);
	matrix_mult(s->data[s->top], tmp);
	copy_matrix(tmp, s->data[s->top]);
      }
      printf("%s\n", "rotate\n");
      break;
    case BOX:
      xval = op[i].op.box.d0[0];
      yval = op[i].op.box.d0[1];
      zval = op[i].op.box.d0[2];
      xcor = op[i].op.box.d0[0];
      ycor = op[i].op.box.d0[1];
      zcor = op[i].op.box.d0[2];
      add_box(tmp, xval, yval, zval, xcor, ycor, zcor);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      printf("box\n");
      break;

    case SPHERE:
      xval = op[i].op.sphere.d[0];
      yval = op[i].op.sphere.d[1];
      zval = op[i].op.sphere.d[2];
      radius = op[i].op.sphere.r;
      add_sphere(tmp, xval, yval, zval, radius, 10);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      printf("%s\n", "sphere\n");
      break;
    case TORUS:
      xval = op[i].op.torus.d[0];
      yval = op[i].op.torus.d[1];
      zval = op[i].op.torus.d[2];
      double inner = op[i].op.torus.r0;
      double outer = op[i].op.torus.r1;
      add_torus(tmp, xval, yval, zval, inner, outer, 10);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      printf("%s\n", "torus\n");
      break;
    case LINE:
      xval = op[i].op.line.d[0];
      yval = op[i].op.line.d[1];
      zval = op[i].op.line.d[2];
      xcor = op[i].op.line.d[0];
      ycor = op[i].op.line.d[1];
      zcor = op[i].op.line.d[2];
      add_edge(tmp, xval, yval, zval, xcor, ycor, zcor);
      matrix_mult(s->data[s->top], tmp);
      draw_lines(tmp, t, g);
      printf("%s\n", "line\n");
      break;
    case SAVE:
      save_extension(t, op[i].op.save.p->name);
      printf("%s\n", "save\n");
      break;
    case DISPLAY:
      display(t);
      printf("%s\n", "display\n");
    }
  }
}
Exemplo n.º 14
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  
  clear_screen(s);

  double arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
  color c;

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    printf(":%s:\n",line);
    
    if (strcmp(line, "quit") == 0){
      exit(0);
    }
    else if (strcmp(line, "line") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf" "%lf" "%lf" "%lf" "%lf" "%lf", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6);
      add_edge(pm, arg1, arg2, arg3, arg4, arg5, arg6);
    }
    else if (strcmp(line, "circle") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf" "%lf" "%lf", &arg1, &arg2, &arg3);
      add_circle(pm, arg1, arg2, arg3, 0.01);
    }
    else if (strcmp(line, "hermite") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8);
      add_curve(pm, arg1, arg2, arg5, arg6, arg3, arg4, arg7, arg8, 0.01, HERMITE_MODE);
    }
    else if (strcmp(line, "bezier") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf" "%lf", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8);
      add_curve(pm, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, 0.01, BEZIER_MODE);
    }
    else if (strcmp(line, "ident") == 0){
      ident(transform);
    }
    else if (strcmp(line, "scale") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf" "%lf" "%lf", &arg1, &arg2, &arg3);
      matrix_mult(make_scale(arg1, arg2, arg3), transform);
    }
    else if (strcmp(line, "translate") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf" "%lf" "%lf", &arg1, &arg2, &arg3);
      matrix_mult(make_translate(arg1, arg2, arg3), transform);
    }
    else if (strcmp(line, "xrotate") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf", &arg1);
      matrix_mult(make_rotX(arg1 * M_PI / 180), transform);
    }
    else if (strcmp(line, "yrotate") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf", &arg1);
      matrix_mult(make_rotY(arg1 * M_PI / 180), transform);
    }
    else if (strcmp(line, "zrotate") == 0){
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      sscanf(line, "%lf", &arg1);
      matrix_mult(make_rotZ(arg1 * M_PI / 180), transform);
    }
    else if (strcmp(line, "apply") == 0){
      matrix_mult(transform, pm);
    }
    else if (strcmp(line, "display") == 0){
      clear_screen(s);
      draw_lines(pm, s, c);
      display(s);
    }
    else if (strcmp(line, "save") == 0){
      clear_screen(s);
      draw_lines(pm, s, c);
      fgets(line, 255, f);
      line[strlen(line)]='\0';
      save_extension(s, line);
    }
    else {
      printf("Parse error\n");
    }
  }
}
Exemplo n.º 15
0
  /*======== void my_main() ==========
    Inputs:   int polygons  
    Returns: 

    This is the main engine of the interpreter, it should
    handle most of the commadns in mdl.

    If frames is not present in the source (and therefore 
    num_frames is 1, then process_knobs should be called.

    If frames is present, the enitre op array must be
    applied frames time. At the end of each frame iteration
    save the current screen to a file named the
    provided basename plus a numeric string such that the
    files will be listed in order, then clear the screen and
    reset any other data structures that need it.

    Important note: you cannot just name your files in 
    regular sequence, like pic0, pic1, pic2, pic3... if that
    is done, then pic1, pic10, pic11... will come before pic2
    and so on. In order to keep things clear, add leading 0s
    to the numeric portion of the name. If you use sprintf, 
    you can use "%0xd" for this purpose. It will add at most
    x 0s in front of a number, if needed, so if used correctly,
    and x = 4, you would get numbers like 0001, 0002, 0011,
    0487

    05/17/12 09:41:35
    jdyrlandweaver
    ====================*/
void my_main( int polygons ) {

  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;

  extern double zbuff[XRES][YRES];

  num_frames = -1;
  step = 0.05;
  strcpy(name, "lol\0");
  g.red = 0;
  g.green = 255;
  g.blue = 255;

  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  instantiate_zbuff();

  first_pass();

  struct vary_node** frames = second_pass();
  //test_second_pass(frames);

  if (num_frames < 2) {

    for (i=0;i<lastop;i++) {
  
      switch (op[i].opcode) {       
	
      case SPHERE:
	add_sphere( tmp,op[i].op.sphere.d[0], //cx
		    op[i].op.sphere.d[1],  //cy
		    op[i].op.sphere.d[2],  //cz
		    op[i].op.sphere.r,
		    step);
	//apply the current top origin
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case TORUS:
	add_torus( tmp, op[i].op.torus.d[0], //cx
		   op[i].op.torus.d[1],     //cy
		   op[i].op.torus.d[2],    //cz
		   op[i].op.torus.r0,
		   op[i].op.torus.r1,
		   step);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case BOX:
	add_box( tmp, op[i].op.box.d0[0],
		 op[i].op.box.d0[1],
		 op[i].op.box.d0[2],
		 op[i].op.box.d1[0],
		 op[i].op.box.d1[1],
		 op[i].op.box.d1[2]);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case LINE:
	add_edge( tmp, op[i].op.line.p0[0],
		  op[i].op.line.p0[1],
		  op[i].op.line.p0[1],
		  op[i].op.line.p1[0],
		  op[i].op.line.p1[1],
		  op[i].op.line.p1[1]);
	draw_lines( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case MOVE:
	//get the factors
	xval = op[i].op.move.d[0];
	yval = op[i].op.move.d[1];
	zval = op[i].op.move.d[2];

	transform = make_translate( xval, yval, zval );
	//multiply by the existing origin
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case SCALE:
	xval = op[i].op.scale.d[0];
	yval = op[i].op.scale.d[1];
	zval = op[i].op.scale.d[2];
      
	transform = make_scale( xval, yval, zval );
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case ROTATE:
	xval = op[i].op.rotate.degrees * ( M_PI / 180 );

	//get the axis
	if ( op[i].op.rotate.axis == 0 ) 
	  transform = make_rotX( xval );
	else if ( op[i].op.rotate.axis == 1 ) 
	  transform = make_rotY( xval );
	else if ( op[i].op.rotate.axis == 2 ) 
	  transform = make_rotZ( xval );

	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case PUSH:
	push( s );
	break;
      case POP:
	pop( s );
	break;
      case SAVE:
	save_extension( t, op[i].op.save.p->name );
	break;
      case DISPLAY:
	display( t );
	break;
      }
    }
  }


  else {

    int currframe;
    for (currframe = 0; currframe < num_frames; currframe++) { //Caryy out all commands once per frame.

      struct vary_node*knobs = frames[currframe];

      for (i=0;i<lastop;i++) {
  
	switch (op[i].opcode) {   

	  /*EXPLANATION

	    case COMMAND:
	    If op[i].op.command.cs == NULL, then no knob is defined for this command.
	    Then do it normally.
	    Otherwise, iterate to the "end. But there are two "ends"...
	    If the value of this knob is -1, you went to the end of the list without finding the corresponding knob. That means it isnt there. Since the command is meant to be varied, but isn't defined for this frame, then don't draw it at this time.
	    If the name of this knob in the linked list matches the name of the corresponding knob, then crry out the command in its knob'd format.

	  */    

	case SET:
	  while ( (knobs->value != -1) && strcmp( op[i].op.set.p->name, knobs->name ) )
	    knobs = knobs->next;
	  if (knobs->value == -1)
	    break;
	  knobs->value = op[i].op.set.val;
	  break;

	case SETKNOBS:
	  while ( knobs->value != -1 ) {
	    knobs->value = op[i].op.set.val;
	    knobs = knobs->next;
	  }
	  break;
	
	case SPHERE:
	  if (!op[i].op.sphere.cs) {
	    add_sphere( tmp,op[i].op.sphere.d[0], //cx
			op[i].op.sphere.d[1],  //cy
			op[i].op.sphere.d[2],  //cz
			op[i].op.sphere.r,
			step);
	  }
	  else {
	    while ( (knobs->value != -1) && strcmp( op[i].op.sphere.cs->name, knobs->name ) )
	      knobs = knobs->next;
	    double value = knobs->value;
	    if (value == -1)
	      break;
	    add_sphere( tmp,op[i].op.sphere.d[0], //cx
			op[i].op.sphere.d[1],  //cy
			op[i].op.sphere.d[2],  //cz
			op[i].op.sphere.r*value,
			step);
	  }
	  //apply the current top origin
	  matrix_mult( s->data[ s->top ], tmp );
	  draw_polygons( tmp, t, g );
	  tmp->lastcol = 0;
	  break;
	 

	case TORUS:
	  if (!op[i].op.torus.cs) {
	    add_torus( tmp, op[i].op.torus.d[0], //cx
		       op[i].op.torus.d[1],     //cy
		       op[i].op.torus.d[2],    //cz
		       op[i].op.torus.r0,
		       op[i].op.torus.r1,
		       step);
	  }
	  else {
	    while ( (knobs->value != -1) && strcmp( op[i].op.torus.cs->name, knobs->name ) )
	      knobs = knobs->next;
	    double value = knobs->value;
	    if (value == -1)
	      break; 
	    add_torus( tmp, op[i].op.torus.d[0], //cx
		       op[i].op.torus.d[1],     //cy
		       op[i].op.torus.d[2],    //cz
		       op[i].op.torus.r0*value,
		       op[i].op.torus.r1*value,
		       step);
	  }
	  matrix_mult( s->data[ s->top ], tmp );
	  draw_polygons( tmp, t, g );
	  tmp->lastcol = 0;
	  break;       
	case BOX:
	  if (!op[i].op.box.cs) {
	    add_box( tmp, op[i].op.box.d0[0],
		     op[i].op.box.d0[1],
		     op[i].op.box.d0[2],
		     op[i].op.box.d1[0],
		     op[i].op.box.d1[1],
		     op[i].op.box.d1[2]);
	  }
	  else {
	    while ( (knobs->value != -1) && strcmp( op[i].op.box.cs->name, knobs->name ) )
	      knobs = knobs->next;
	    double value = knobs->value;
	    if (value == -1)
	      break; 
	    add_box( tmp, op[i].op.box.d0[0],
		     op[i].op.box.d0[1],
		     op[i].op.box.d0[2],
		     op[i].op.box.d1[0]*value,
		     op[i].op.box.d1[1]*value,
		     op[i].op.box.d1[2]*value);
	  }
	  matrix_mult( s->data[ s->top ], tmp );
	  draw_polygons( tmp, t, g );
	  tmp->lastcol = 0;
	  break;

	case LINE:
	  add_edge( tmp, op[i].op.line.p0[0],
		    op[i].op.line.p0[1],
		    op[i].op.line.p0[1],
		    op[i].op.line.p1[0],
		    op[i].op.line.p1[1],
		    op[i].op.line.p1[1]);
	  draw_lines( tmp, t, g );
	  tmp->lastcol = 0;
	  break;

	case MOVE:
	  //get the factors
	  xval = op[i].op.move.d[0];
	  yval = op[i].op.move.d[1];
	  zval = op[i].op.move.d[2];

	  transform = make_translate( xval, yval, zval );
	  //multiply by the existing origin
	  matrix_mult( s->data[ s->top ], transform );
	  //put the new matrix on the top
	  copy_matrix( transform, s->data[ s->top ] );
	  free_matrix( transform );
	  break;

	case SCALE:
	  if (!op[i].op.scale.p) {
	    xval = op[i].op.scale.d[0];
	    yval = op[i].op.scale.d[1];
	    zval = op[i].op.scale.d[2];
	  }
	  else {
	    while ( (knobs->value != -1) && strcmp( op[i].op.scale.p->name, knobs->name ) )
	      knobs = knobs->next;
	    double value = knobs->value;
	    if (value == -1)
	      break; 
	    xval = op[i].op.scale.d[0]*value;
	    yval = op[i].op.scale.d[1]*value;
	    zval = op[i].op.scale.d[2]*value;
	  }
	  transform = make_scale( xval, yval, zval );
	  matrix_mult( s->data[ s->top ], transform );
	  //put the new matrix on the top
	  copy_matrix( transform, s->data[ s->top ] );
	  free_matrix( transform );
	  break;

	case ROTATE:
	  if (!op[i].op.rotate.p) {
	    xval = op[i].op.rotate.degrees * ( M_PI / 180 );
	  }
	  else {
	    while ( (knobs->value != -1) && strcmp( op[i].op.rotate.p->name, knobs->name ) )
	      knobs = knobs->next;
	    double value = knobs->value;
	    if (value == -1)
	      break;
	    xval = op[i].op.rotate.degrees * ( M_PI / 180 ) * value;
	  }
	  //get the axis
	  if ( op[i].op.rotate.axis == 0 ) 
	    transform = make_rotX( xval );
	  else if ( op[i].op.rotate.axis == 1 ) 
	    transform = make_rotY( xval );
	  else if ( op[i].op.rotate.axis == 2 ) 
	    transform = make_rotZ( xval );

	  matrix_mult( s->data[ s->top ], transform );
	  //put the new matrix on the top
	  copy_matrix( transform, s->data[ s->top ] );
	  free_matrix( transform );
	  break;

	case PUSH:
	  push( s );
	  break;
	case POP:
	  pop( s );
	  break;
	case SAVE:
	  save_extension( t, op[i].op.save.p->name );
	  break;
	case DISPLAY:
	  display( t );
	  break;
	}
      }

      char framename[256];
      mkdir("animation", 0777);     
      sprintf(framename, "animation/%s%04d.png", name, currframe);
      save_extension( t, framename);
      clear_screen( t );

    }

  }

  free_stack( s );
  free_matrix( tmp );
  //free_matrix( transform );    
}
Exemplo n.º 16
0
Arquivo: my_main.c Projeto: stuydw/mdl
void my_main( int polygons ) {

  int i;
  double step = .05;
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;

  g = change_color(4);

  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
      
    case COMMENT:
      break;

    case PUSH:
      push(s);
      break;

    case POP:
      pop(s);
      break;

    case MOVE:
      xval = op[i].op.move.d[0];
      yval = op[i].op.move.d[1];
      zval = op[i].op.move.d[2];

      transform = make_translate(xval, yval, zval);
      matrix_mult(s->data[s->top], transform);
      s->data[s->top] = transform;
      break;
      
    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];
      
      transform - make_scale(xval, yval, zval);
      matrix_mult(s->data[s->top], transform);
      s->data[s->top] = transform;
      break;
 
    case ROTATE:
      switch ((int)op[i].op.rotate.axis) 
	{

	  double theta = op[i].op.rotate.degrees;

	case 0:
	  transform = make_rotX(theta);
	  break;
	case 1:
	  transform = make_rotY(theta);
	  break;
	case 2:
	  transform = make_rotZ(theta);
	  break;
      }
      
      matrix_mult(s->data[s->top], transform);
      s->data[s->top] = transform;
      break;

    case BOX:
      empty_matrix(tmp);

      xval = op[i].op.box.d0[0];
      yval = op[i].op.box.d0[1];
      zval = op[i].op.box.d0[2];

      double width = op[i].op.box.d1[0];
      double height = op[i].op.box.d1[1];
      double depth = op[i].op.box.d1[2];
      
      add_box(tmp, xval, yval, zval, width, height, depth);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;

    case SPHERE:
      empty_matrix(tmp);
      
      xval = op[i].op.sphere.d[0];
      yval = op[i].op.sphere.d[1];
      zval = op[i].op.sphere.d[2];
      
      double radius = op[i].op.sphere.r;

      add_sphere(tmp, xval, yval, zval, radius, step);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;

    case TORUS:
      empty_matrix(tmp);

      xval = op[i].op.torus.d[0];
      yval = op[i].op.torus.d[1];
      zval = op[i].op.torus.d[2];

      double r1 = op[i].op.torus.r0;
      double r2 = op[i].op.torus.r1;

      add_torus(tmp, xval, yval, zval, r1, r2, step);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;

    case LINE:
      empty_matrix(tmp);

      add_edge(tmp,
	       op[i].op.line.p0[0],
	       op[i].op.line.p0[1],
	       op[i].op.line.p0[2],
	       op[i].op.line.p1[0],
	       op[i].op.line.p1[1],
	       op[i].op.line.p1[2]);
      
      draw_lines(tmp, t, g);
      break;

    case SAVE:
      save_extension(t, op[i].op.save.p->name);
      break;

    case DISPLAY:
      display(t);
      break;
    
    }
  }
}
Exemplo n.º 17
0
/*======== void my_main() ==========
  Inputs:
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {
  
  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  
  struct vary_node **knobs;
  struct vary_node *vn;
  char frame_name[128];

  num_frames = 1;
  step = 5;
  
  g.red = 0;
  g.green = 255;
  g.blue = 255;
  first_pass();
  knobs=second_pass();
  puts("hi");
  
  
  /*
    int swag; 
    for (i=0;i<lastop;i++){
    
    if (op[i].opcode == FRAMES)
    num_frames = op[i].op.frames.num_frames;


    }
   */

  //for (swag=0;swag<num_frames;swag++){
  for (i=0;i<lastop;i++) {
    puts("flag");
    switch (op[i].opcode) {
    case SPHERE:
      add_sphere( tmp,op[i].op.sphere.d[0], //cx
		  op[i].op.sphere.d[1],  //cy
		  op[i].op.sphere.d[2],  //cz
		  op[i].op.sphere.r,
		  step);
      //apply the current top origin
      matrix_mult( s->data[ s->top ], tmp );
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case TORUS:
      add_torus( tmp, op[i].op.torus.d[0], //cx
		 op[i].op.torus.d[1],     //cy
		 op[i].op.torus.d[2],    //cz
		 op[i].op.torus.r0,
		 op[i].op.torus.r1,
		 step);
      matrix_mult( s->data[ s->top ], tmp );
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case BOX:
      add_box( tmp, op[i].op.box.d0[0],
	       op[i].op.box.d0[1],
	       op[i].op.box.d0[2],
	       op[i].op.box.d1[0],
	       op[i].op.box.d1[1],
	       op[i].op.box.d1[2]);
      matrix_mult( s->data[ s->top ], tmp );
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case LINE:
      add_edge( tmp, op[i].op.line.p0[0],
		op[i].op.line.p0[1],
		op[i].op.line.p0[1],
		op[i].op.line.p1[0],
		op[i].op.line.p1[1],
		op[i].op.line.p1[1]);
      draw_lines( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case MOVE:
      //get the factors
      xval = op[i].op.move.d[0];
      yval =  op[i].op.move.d[1];
      zval = op[i].op.move.d[2];
      
      transform = make_translate( xval, yval, zval );
      //multiply by the existing origin
      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];
      
      transform = make_scale( xval, yval, zval );
      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case ROTATE:
      xval = op[i].op.rotate.degrees * ( M_PI / 180 );

      //get the axis
      if ( op[i].op.rotate.axis == 0 ) 
	transform = make_rotX( xval );
      else if ( op[i].op.rotate.axis == 1 ) 
	transform = make_rotY( xval );
      else if ( op[i].op.rotate.axis == 2 ) 
	transform = make_rotZ( xval );

      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case PUSH:
      push( s );
      break;
    case POP:
      pop( s );
      break;
    case SAVE:
      save_extension( t, op[i].op.save.p->name );
      break;
    case DISPLAY:
      display( t );
      break;
    }
  }
  //}
  free_stack( s );
  free_matrix( tmp );
  //free_matrix( transform );
}
Exemplo n.º 18
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  color c;
  c.red=MAX_COLOR;
  c.blue=0;
  c.green=0;

  double step=10000;

  FILE *f;
  char line[256];

  double x0,y0,z0,x1,y1,z1;
  double cx,cy,radius;
  double x2,y2,x3,y3;
  double sx,sy,sz;
  double tx,ty,tz;
  double theta;

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    printf(":%s:\n",line);  
    if (strcmp(line,"line") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf %lf %lf %lf %lf %lf",&x0,&y0,&z0,&x1,&y1,&z1);
      printf("%f %f %f %f %f %f\n",x0,y0,z0,x1,y1,z1);
      add_edge(pm,x0,y0,z0,x1,y1,z1);
    }
    if (strcmp(line,"circle") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf %lf %lf",&cx,&cy,&radius);
      add_circle(pm,cx,cy,radius,step);
    }
    if (strcmp(line,"hermite") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf %lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2,&x3,&y3);
      add_curve(pm,x0,y0,x1,y1,x2,y2,x3,y3,step,2);
    }
    if (strcmp(line,"bezier") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf %lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2,&x3,&y3);
      add_curve(pm,x0,y0,x1,y1,x2,y2,x3,y3,step,1);
    }
    if (strcmp(line,"ident") == 0)
      ident(transform);
    if (strcmp(line,"scale") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf %lf %lf",&sx,&sy,&sz);
      struct matrix *scale = make_scale(sx,sy,sz);
      matrix_mult(scale,transform);
      free_matrix(scale);
    }
    if (strcmp(line,"translate") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf %lf %lf",&tx,&ty,&tz);
      struct matrix *translate = make_translate(tx,ty,tz);
      matrix_mult(translate,transform);
      free_matrix(translate);
    }
    if (strcmp(line,"xrotate") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf",&theta);
      struct matrix *rotX = make_rotX((theta/180.)*M_PI);
      matrix_mult(rotX,transform);
      free_matrix(rotX);
    }
    if (strcmp(line,"yrotate") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf",&theta);
      struct matrix *rotY = make_rotY((theta/180.)*M_PI);
      matrix_mult(rotY,transform);
      free_matrix(rotY);
    }
    if (strcmp(line,"zrotate") == 0){
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      sscanf(line,"%lf",&theta);
      struct matrix *rotZ = make_rotZ((theta/180.)*M_PI);
      matrix_mult(rotZ,transform);
      free_matrix(rotZ);
    }
    if (strcmp(line,"apply") == 0)
      matrix_mult(transform,pm);
    if (strcmp(line,"display") == 0){
      clear_screen(s);
      draw_lines(pm,s,c);
      display(s);
    }
    if (strcmp(line,"save") == 0){
      clear_screen(s);
      fgets(line,255, f);
      line[strlen(line)-1]='\0';
      printf(":%s:\n",line); 
      draw_lines(pm,s,c);
      save_extension(s,line);
    }
    if (strcmp(line,"quit") == 0)
      exit(42); 
  }
}
Exemplo n.º 19
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

////////// ////////// //////////
////////// ////////// //////////
NOW INCLUDES SPHERE, TORUS, AND BOX
////////// ////////// //////////
////////// ////////// //////////

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  g.red = 0;
  g.green = 255;
  g.blue = 255;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, x3, y3, x4, y4, r2, w, h, d;
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "sphere", strlen(line)) == 0 ) {
      printf("SPHERE\n");                                       
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 0.01);
      printf( "%lf %lf %lf\n", x, y, z);        
    }
    else if ( strncmp(line, "torus", strlen(line)) == 0 ) {
      //printf("TORUS\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &r2);
      add_torus(pm, x, y, z, r2, 0.01);
      //printf( "%lf %lf %lf %lf\n", x, y, z, r2);      
    }
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      //      printf("BOX\n");                                        
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &w, &h, &d);
      add_box(pm, x, y, z, w, h, d);
      //      printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, w, h, d);
    }
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      //printf("BEZIER\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      //      transform = new_matrix( 4,0 );
      pm = new_matrix( 4,0 );
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      clear_screen(s);
      draw_lines(pm, s, g);
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      draw_lines(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
Exemplo n.º 20
0
void my_main( int polygons ) {

  int i;
  double step;
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *temp;
  struct stack *stax;
  screen t;

  color g;
  g.red = 225;
  g.blue = 0;
  g.green = 0;

  stax = new_stack();
  temp = new_matrix(4, 1000);
  transform = new_matrix(4, 4);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
    case BOX:
      add_box(temp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]);
      matrix_mult(stax->data[stax->top], temp);
      draw_polygons(temp, t, g);
      free_matrix(temp);
      break;
    case SPHERE:
      add_sphere(temp, op[i].op.sphere.d[0], op[i].op.sphere.d[1], op[i].op.sphere.d[2], op[i].op.sphere.r, 10);
      matrix_mult(stax->data[stax->top], temp);
      draw_polygons(temp, t, g);
      free_matrix(temp);
      break;
    case TORUS:
      add_torus(temp, op[i].op.torus.d[0], op[i].op.torus.d[1], op[i].op.torus.d[2], op[i].op.torus.r0,op[i].op.torus.r1, 10);
      matrix_mult(stax->data[stax->top], temp);
      draw_polygons(temp, t, g);
      free_matrix(temp);
      break;
    case SCALE:
      transform = make_scale(op[i].op.scale.d[0], op[i].op.scale.d[1], op[i].op.scale.d[2]);
      matrix_mult(stax->data[stax->top], transform);
      copy_matrix(transform, stax->data[stax->top]);
      free_matrix(transform);
      break;
    case MOVE:
      transform = make_translate(op[i].op.move.d[0], op[i].op.move.d[1], op[i].op.move.d[2]);
      matrix_mult(stax->data[stax->top], transform);
      copy_matrix(transform, stax->data[stax->top]);
      free_matrix(transform);
      break;
    case PUSH:
      push(stax);
      break;
    case POP:
      pop(stax);
      break;
    case ROTATE:
      if (op[i].op.rotate.axis == 0)
	//case X
	transform = make_rotX(op[i].op.rotate.degrees * M_PI/180 );
      else if (op[lastop].op.rotate.axis == 1)
	//case Y
	transform = make_rotY(op[i].op.rotate.degrees * M_PI/180 );
      else if (op[lastop].op.rotate.axis == 2)
	//case Z
	transform = make_rotZ(op[i].op.rotate.degrees * M_PI/180 );
      matrix_mult(stax->data[stax->top], transform);
      copy_matrix(transform, stax->data[stax->top]);
      free_matrix(transform);
      break;
    case LINE:
      add_edge(temp, 
	       op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2],
	       op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]);
      matrix_mult(stax->data[stax->top], temp);
      draw_lines(temp, t, g);
      free_matrix(temp);
      break;
    case SAVE:
      save_extension(t,op[i].op.save.p->name);
      break;
    case DISPLAY:
      display(t);
      break;
    }
  }
  free_matrix(temp);
  free_matrix(transform);
  free_stack(stax);
}
Exemplo n.º 21
0
Arquivo: my_main.c Projeto: stuydw/mdl
void my_main( int polygons ) {

  int i, axis;
  double xval, xval1, yval, yval1, zval, zval1, degrees, width, height, depth, r, r1;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  
  g = change_color(0);

  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
    case PUSH:
      push(s);
      break;
    case POP:
      pop(s);
      break;
    case MOVE:
      xval = op[i].op.move.d[0];
      yval = op[i].op.move.d[1];
      zval = op[i].op.move.d[2];
      transform = make_translate(xval, yval, zval);
      matrix_mult(transform, s->data[s->top]);
      break;
    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];
      transform = make_scale(xval, yval, zval);
      matrix_mult(transform, s->data[s->top]);
      break;
    case ROTATE:
      axis = op[i].op.rotate.axis;
      degrees = op[i].op.rotate.degrees;
      if(axis == 0)
	transform = make_rotX(degrees);
      else if(axis == 1)
	transform = make_rotY(degrees);
      else if(axis == 2)
	transform = make_rotZ(degrees);
      matrix_mult(transform, s->data[s->top]);
      break;
    case BOX:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.box.d0[0];
      yval = op[i].op.box.d0[1];
      zval = op[i].op.box.d0[2];
      width = op[i].op.box.d1[0];
      height = op[i].op.box.d1[1];
      depth = op[i].op.box.d1[2];
      add_box(tmp, xval, yval, zval, width, height, depth);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;
    case SPHERE:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.sphere.d[0];
      yval = op[i].op.sphere.d[1];
      zval = op[i].op.sphere.d[2];
      r = op[i].op.sphere.r;
      add_sphere(tmp, xval, yval, zval, r, 0.05);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;
    case TORUS:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.torus.d[0];
      yval = op[i].op.torus.d[1];
      zval = op[i].op.torus.d[2];
      r = op[i].op.torus.r0;
      r1 = op[i].op.torus.r1;
      add_torus(tmp, xval, yval, zval, r, r1, 0.05);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;
    case LINE:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.line.p0[0];
      yval = op[i].op.line.p0[1];
      zval = op[i].op.line.p0[2];
      xval1 = op[i].op.line.p1[0];
      yval1 = op[i].op.line.p1[1];
      zval1 = op[i].op.line.p1[2];
      add_edge(tmp, xval, yval, zval, xval1, yval1, zval1);
      matrix_mult(s->data[s->top], tmp);
      draw_lines(tmp, t, g);
      break;
    case SAVE:
      save_extension(t, op[i].op.save.p->name);
      break;
    case DISPLAY:
      display(t);
      break;
    }
  }
  free_stack(s);
  free_matrix(tmp);
}
Exemplo n.º 22
0
/*
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
        //   l: add a line to the edge matrix - 
	    takes 6 arguments (x0, y0, z0, x1, y1, z1)
	//   i: set the transform matrix to the identity matrix - 
	//   s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	//   t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	//    x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	//    y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	//    z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	//   a: apply the current transformation matrix to the 
	    edge matrix
	//   v: draw the lines of the edge matrix to the screen
	    display the screen
	//   g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	//    q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian measure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant///////////////////////did this in matrix.c
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

int fd = open(filename, O_RDONLY, 0666);
FILE * f;
f = fopen(filename,"r");
printf("Opened %s\n", filename);
int ln = 1;
int ran = 0;
char input[128];
char dummy[128];
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
color c;
int iiiii,jjjjj;
  for( iiiii=0; iiiii<XRES; iiiii++)
    for ( jjjjj=0; jjjjj<YRES; jjjjj++) {

      c.red = 0;//random() % (MAX_COLOR + 1);
      c.green = 0;//random() % (MAX_COLOR + 1);
      c.blue = 0;//random() % (MAX_COLOR + 1);

      plot( s, c, iiiii, jjjjj);
    }
c.red = 0;
c.green = 255;
c.blue = 0;
long end = lseek(fd,0,SEEK_END) - 1;
lseek(fd,0,SEEK_SET);
while( (lseek(fd,0, SEEK_CUR) < end) && (strcmp(input, "q") != 0)){
ran = 0;


//printf("Running %s\n", input);


if (ran == 0 && strcmp(input, "v") == 0){///////////////////////////////////v
ran = 1;
draw_lines(pm,s,c);
display(s);
//print_matrix(pm);
//print_matrix(transform);
}

if (ran == 0 && strcmp(input, "g") == 0){///////////////////////////////////g
ran = 1;
draw_lines(pm,s,c);
display(s);
ln++;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
if(input[strlen(input) - 1] == '\n'){
input[strlen(input) - 1] = '\0';
}
save_extension(s, input);
printf("Saved to %s\n",input);
}

if (ran == 0 && strcmp(input, "l") == 0){////////////////////////////////////////////////////////////////l
ran = 1;
ln++;
double pts[6];
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
int neg = 0;
double pow = 1;
double num = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 6; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
pts[p++] = num;
}
}

if (p == 6 ){
//printf("Added (%lf,%lf,%lf) to (%lf,%lf,%lf).\n",pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
add_edge(pm,pts[0], pts[1], pts[2], pts[3], pts[4], pts[5] );
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "i") == 0){///////////////////////////////////i
ran = 1;
ident(transform);
}




if (ran == 0 && strcmp(input, "a") == 0){///////////////////////////////////a
ran = 1;
matrix_mult(transform, pm);
}




if (ran == 0 && strcmp(input, "s") == 0){///////////////////////////////////s
ran = 1;
ln++;
double factors[3];
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 3; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
factors[p++] = num;
}
}
if (p == 3 ){
//printf("Scaled by %lf,%lf,and %lf.\n",factors[0], factors[1], factors[2]);
struct matrix* scm = make_scale(factors[0], factors[1], factors[2]);
matrix_mult(scm,transform);
free_matrix(scm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}



if (ran == 0 && strcmp(input, "t") == 0){///////////////////////////////////t
ran = 1;
ln++;
double factors[3];
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 3; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
factors[p++] = num;
}
}
if (p == 3 ){
//printf("Moved by %lf,%lf,and %lf.\n",factors[0], factors[1], factors[2]);
struct matrix* trm = make_translate(factors[0], factors[1], factors[2]);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "x") == 0){///////////////////////////////////x
ran = 1;
ln++;
double theta;
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 1; i++){
len = 0;
neg = 0;;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
theta = num;
p++;
}
}
if (p == 1 ){
//printf("Rotated by %lf degrees.\n",theta);
struct matrix* trm = make_rotX(theta);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "y") == 0){///////////////////////////////////y
ran = 1;
ln++;
double theta;
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 1; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
theta = num;
p++;
}
}
if (p == 1 ){
//printf("Rotated by %lf degrees.\n",theta);
struct matrix* trm = make_rotY(theta);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "z") == 0){///////////////////////////////////z
ran = 1;
ln++;
double theta;
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 1; i++){
len = 0;
dloc = -1;
neg = 0;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
theta = num;
p++;
}
}
if (p == 1 ){
//printf("Rotated by %lf degrees.\n",theta);
struct matrix* trm = make_rotZ(theta);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0){///////////////////////////////////////////////////////////////////////ERROR
printf("Error in file- line number %d: %s\n", ln, input);
}
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
ln++;
}
close(fd);
}
Exemplo n.º 23
0
Arquivo: my_main.c Projeto: stuydw/mdl
void my_main( int polygons ) {

  int i,ROT_AXIS;
  double step;
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  g.red = 122;
  g.green = 218;
  g.blue = 225;
  
  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
    case PUSH:
      push(s);
      break;
    case POP:
      pop(s);
      break;
    case DISPLAY:
      display(t);
      break;
    case SAVE:
      save_extension(t,op[i].op.save.p->name);
      break;
    case MOVE:
      transform = make_translate(op[i].op.move.d[0],
			         op[i].op.move.d[1],
			         op[i].op.move.d[2]);
      matrix_mult(transform,s->data[s->top]);
      break;
    case SCALE:
      transform = make_scale(op[i].op.scale.d[0],
	    		     op[i].op.scale.d[1],
			     op[i].op.scale.d[2]);
      matrix_mult(transform,s->data[s->top]);
      break;
    case ROTATE:
      ROT_AXIS = op[i].op.rotate.axis;
      if(ROT_AXIS = 0)
	transform = make_rotX(op[i].op.rotate.degrees);
      else if(ROT_AXIS = 1)
	transform = make_rotY(op[i].op.rotate.degrees);
      else if(ROT_AXIS = 2)
	transform = make_rotZ(op[i].op.rotate.degrees);
      matrix_mult(transform,s->data[s->top]);
      break;
    case LINE:
      add_edge(tmp, op[i].op.line.p0[0],
	            op[i].op.line.p0[1],
	            op[i].op.line.p0[2],
	            op[i].op.line.p1[0], 
	            op[i].op.line.p1[1],
	            op[i].op.line.p1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_lines(tmp,t,g);
      break;
    case SPHERE:
      add_sphere(tmp,op[i].op.sphere.d[0],
  	             op[i].op.sphere.d[1],
	             op[i].op.sphere.d[3],
	             op[i].op.sphere.r,
	             .1);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      break;
    case BOX:
      add_box(tmp,op[i].op.box.d0[0],
	          op[i].op.box.d0[1],
	          op[i].op.box.d0[2],
	          op[i].op.box.d1[0],
	          op[i].op.box.d1[1],
	          op[i].op.box.d1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      break;
    case TORUS:
      add_torus(tmp,op[i].op.torus.d[0],
		    op[i].op.torus.d[1],
		    op[i].op.torus.d[2],
		    op[i].op.torus.r0,
		    op[i].op.torus.r1,
	  	    .1);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      break;
    }
  }
}
Exemplo n.º 24
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {
int i, j;
zbuf = (double **)malloc(500 * sizeof(double*));
for(i = 0; i < 500; i++){
zbuf[i] = (double*)malloc(500 * sizeof(double));
for (j = 0; j < 500; j++){
zbuf[i][j] = -DBL_MAX;
}
}
i = 0;
j = 0;
  int f;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;

  clear_screen( t );
  num_frames = 1;
  step = 0.05;
 
  g.red = 0;
  g.green = 125;
  g.blue = 255;

  s = new_stack();
  tmp = new_matrix(4, 1000);

first_pass();
if (num_frames < 0){
printf("ERROR- can't vary knobs when there's only 1 frame.\n");
}
if (num_frames >= 1){
struct vary_node ** klist = second_pass();
for (f = 0; f < num_frames; f++){
for ( i = 0; i < 500; i++){
for (j = 0; j < 500; j++){
zbuf[i][j] = -DBL_MAX;
}
}
  for (i=0;i<lastop;i++) {



struct vary_node* v = klist[f];
SYMTAB * vvv;
while ( v  != NULL){
//printf("set %s %lf \n",v->name, v->value );
vvv = lookup_symbol(v->name);
if (vvv != NULL){
set_value(vvv, v->value);
}
else{
add_symbol(v->name,SYM_VALUE,(void *)&(v->value));
}
v = v->next;
}


  //print_knobs();
    switch (op[i].opcode) {
case SETKNOBS:
xval = 0;
	double abcdef = op[i].op.setknobs.value;
//printf("Setting knobs to %lf.\n",abcdef);
for ( i=0; i < lastsym; i++ ) {

    if ( symtab[i].type == SYM_VALUE ) {
      set_value(&(symtab[i]), abcdef);
    }
  }
break;
    case SPHERE:
      add_sphere( tmp,op[i].op.sphere.d[0], //cx
		  op[i].op.sphere.d[1],  //cy
		  op[i].op.sphere.d[2],  //cz
		  op[i].op.sphere.r,
		  step);
      //apply the current top origin
      matrix_mult( s->data[ s->top ], tmp );
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case TORUS:
      add_torus( tmp, op[i].op.torus.d[0], //cx
		 op[i].op.torus.d[1],     //cy
		 op[i].op.torus.d[2],    //cz
		 op[i].op.torus.r0,
		 op[i].op.torus.r1,
		 step);
      matrix_mult( s->data[ s->top ], tmp );
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case BOX:
      add_box( tmp, op[i].op.box.d0[0],
	       op[i].op.box.d0[1],
	       op[i].op.box.d0[2],
	       op[i].op.box.d1[0],
	       op[i].op.box.d1[1],
	       op[i].op.box.d1[2]);
      matrix_mult( s->data[ s->top ], tmp );
      draw_polygons( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case LINE:
      add_edge( tmp, op[i].op.line.p0[0],
		op[i].op.line.p0[1],
		op[i].op.line.p0[1],
		op[i].op.line.p1[0],
		op[i].op.line.p1[1],
		op[i].op.line.p1[1]);
      draw_lines( tmp, t, g );
      tmp->lastcol = 0;
      break;

    case MOVE:
      //get the factors
      xval = op[i].op.move.d[0];
      yval =  op[i].op.move.d[1];
      zval = op[i].op.move.d[2];
//printf("MOVE %lf %lf %lf\n",xval,yval,zval);
if (op[i].op.move.p != NULL){
SYMTAB* thing = (lookup_symbol(op[i].op.move.p->name));
xval *= thing->s.value;
yval *= thing->s.value;
zval *= thing->s.value;
//printf("new MOVE %lf %lf %lf\n",xval,yval,zval);
}

      transform = make_translate( xval, yval, zval );
      //multiply by the existing origin
      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];
      
//printf("scalE %lf %lf %lf\n",xval,yval,zval);
if (op[i].op.scale.p != NULL){
SYMTAB*  thing = (lookup_symbol(op[i].op.scale.p->name));
xval *= thing->s.value;
yval *= thing->s.value;
zval *= thing->s.value;
//printf("new scale %lf %lf %lf\n",xval,yval,zval);
}

      transform = make_scale( xval, yval, zval );
      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case ROTATE:
      xval = op[i].op.rotate.degrees * ( M_PI / 180 );
//printf("rotate %lf\n",xval);
if (op[i].op.rotate.p != NULL){
xval *= (lookup_symbol(op[i].op.rotate.p->name))->s.value;
//printf("new rotate%lf\n",xval);
}

      //get the axis
      if ( op[i].op.rotate.axis == 0 ) 
	transform = make_rotX( xval );
      else if ( op[i].op.rotate.axis == 1 ) 
	transform = make_rotY( xval );
      else if ( op[i].op.rotate.axis == 2 ) 
	transform = make_rotZ( xval );

      matrix_mult( s->data[ s->top ], transform );
      //put the new matrix on the top
      copy_matrix( transform, s->data[ s->top ] );
      free_matrix( transform );
      break;

    case PUSH:
      push( s );
      break;
    case POP:
      pop( s );
      break;
    case SAVE:
      save_extension( t, op[i].op.save.p->name );
      break;
    case DISPLAY:
      display( t );
      break;
    }
  }

if (num_frames > 1){
char nopq[256];
char rst[256];
strcpy(nopq,"animations/");
strcat(nopq, name);
sprintf (rst, "%03d", f );
strcat(nopq,rst);
strcat(nopq,".png");
//printf("Saved frame %d to %s\n", (f+1) ,nopq);
save_extension( t, nopq );
  clear_screen( t );
screen t;
  g.red = 0;
  g.green = 255;
  g.blue = 255;


  free_stack( s );
  free_matrix( tmp );

  s = new_stack();
  tmp = new_matrix(4, 1000);
}
//printf("finished frame %d\n",f);
}
  ////////////////////////////////////////////////////////////

for (j = 0; j < num_frames; j++){
struct vary_node * v2 = klist[j];
struct vary_node * v = klist[j];
while (v2  != NULL){
v = v2;
v2 = v2->next;
free(v);
}
}
  free(klist);
}	
////////////////////////////////////////////////
  free_stack( s );
  free_matrix( tmp );
for(i = 0; i < 500; i++){
free(zbuf[i]);
}
free(zbuf);
  //free_matrix( transform );    
}
Exemplo n.º 25
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {
  int f = open(filename,O_RDONLY | O_CREAT,0);
  char *buf1 = (char *)malloc(sizeof(char) * 1000);
  //int i=0;   
  read(f,buf1,1000);
  //printf("%s \n",buf1);
  //char **buf = buf1;
  //  strsep(&buf1,"\n");
  char *tmp = (char *)malloc(sizeof(char) * 1000);
  char *tmp1 =(char *)malloc(sizeof(char) * 1000);
  //  printf("This is strlen:%d\n",strlen(buf1));
  int u,v,w,x,y,z;

  while(buf1 && strlen(buf1) > 0){
    //printf("This is strlen:%d\n",strlen(buf1));
    tmp = strsep(&buf1,"\n");
    if(tmp[0] == 'l'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';
      v=strsep(&tmp," ")-'0';
      w=strsep(&tmp," ")-'0';
      x=strsep(&tmp," ")-'0';
      y=strsep(&tmp," ")-'0';
      z=strsep(&tmp," ")-'0';

      printf("%d %d %d %d %d %d\n\n\n\n",u,v,w,x,y,z);
      
      add_edge(pm,u,v,w,x,y,z);
      print_matrix(pm);
    }
    if(tmp[0] == 'i'){
      ident(transform);
    }
    if(tmp[0] == 'q'){
      return;
    }
    if(tmp[0] == 's'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';
      v=strsep(&tmp," ")-'0';
      w=strsep(&tmp," ")-'0';
      // x=strsep(&tmp," ")-'0';
      //y=strsep(&tmp," ")-'0';
      //z=strsep(&tmp," ")-'0';
      
      printf("%d %d %d\n\n\n\n",u,v,w);
      
      //make_scale(u,v,w);
      matrix_mult(transform,make_scale(u,v,w));
    }
    if(tmp[0] == 'x'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';

      tmp = make_rotX(u);
      matrix_mult(tmp,transform);
    }
    if(tmp[0] == 'y'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';

      tmp = make_rotY(u);
      matrix_mult(tmp,transform);
    }
    if(tmp[0] == 'z'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';

      tmp = make_rotZ(u);
      matrix_mult(tmp,transform);
    }
    if(tmp[0] == 't'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';
      v=strsep(&tmp," ")-'0';
      w=strsep(&tmp," ")-'0';
      printf("%d %d %d\n\n\n\n",u,v,w);
      
      tmp = make_translate(u,v,w);
      matrix_mult(tmp,tranform);
    }
    if(tmp[0] == 'a'){
      matrix_mult(transform,pm);
    }
    if(tmp[0] == 'v'){
      draw_lines(pm,s,c);
      display(s);
    }
    if(tmp[0] == 'g'){
      clear_screen();
      draw_lines();
      display(s);
      u=strsep(&buf1,"1");
      save_extension(s,u);
    }
  close(f);

}
Exemplo n.º 26
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1) then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {

  int i, f, j, k, frame, num;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  char fname[100];
  char number[4];
  screen t;
  color g;
  char q;

  step = 0.05;
 
  g.red = 0;
  g.green = 255;
  g.blue = 255;

  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  first_pass();

  if(num_frames == -1){
    printf("Stop being silly and give num_frames a value please!");
    return;
  }

  if(num_frames == 0){
    for (i=0;i<lastop;i++) {
      
      switch (op[i].opcode) {
	
      case SPHERE:
	add_sphere( tmp,op[i].op.sphere.d[0], //cx
		    op[i].op.sphere.d[1],  //cy
		    op[i].op.sphere.d[2],  //cz
		    op[i].op.sphere.r,
		    step);
	//apply the current top origin
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;
	
      case TORUS:
	add_torus( tmp, op[i].op.torus.d[0], //cx
		   op[i].op.torus.d[1],     //cy
		   op[i].op.torus.d[2],    //cz
		   op[i].op.torus.r0,
		   op[i].op.torus.r1,
		   step);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;
	
      case BOX:
	add_box( tmp, op[i].op.box.d0[0],
		 op[i].op.box.d0[1],
		 op[i].op.box.d0[2],
		 op[i].op.box.d1[0],
		 op[i].op.box.d1[1],
		 op[i].op.box.d1[2]);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;
	
      case LINE:
	add_edge( tmp, op[i].op.line.p0[0],
		  op[i].op.line.p0[1],
		  op[i].op.line.p0[1],
		  op[i].op.line.p1[0],
		  op[i].op.line.p1[1],
		  op[i].op.line.p1[1]);
	draw_lines( tmp, t, g );
      tmp->lastcol = 0;
      break;
      
      case PUSH:
	push( s );
	break;
	
      case POP:
	pop( s );
      break;
      
      case SAVE:
	save_extension( t, op[i].op.save.p->name );
	break;
	
      case DISPLAY:
	display( t );
	break;
	
      }
    }
  }
    
    else{

      struct vary_node **knobs = second_pass(); 
 
      for(frame = 0; frame < num_frames; frame++){
	clear_screen(t);
	free_stack(s);
	s = new_stack();

	struct vary_node *curr = (struct vary_node *)malloc(sizeof(struct vary_node *));
	curr = knobs[frame];
	while(curr){
	  set_value(lookup_symbol(curr->name), curr->value);
	  curr = curr->next;
	}
	free(curr);

	for (i=0;i<lastop;i++) {
	  
	  switch (op[i].opcode) {

	  case SET:
	    set_value(lookup_symbol(op[i].op.set.p->name), op[i].op.set.val);
	    break;
	    
	  case SETKNOBS:
	    for(k=0; k<lastsym; k++){
	      if(symtab[k].type == SYM_VALUE)
		symtab[k].s.value = op[i].op.setknobs.value;
	    }
	    break;
	    
	  case SPHERE:
	    add_sphere( tmp,op[i].op.sphere.d[0], //cx
			op[i].op.sphere.d[1],  //cy
			op[i].op.sphere.d[2],  //cz
			op[i].op.sphere.r,
			step);
	    //apply the current top origin
	    matrix_mult( s->data[ s->top ], tmp );
	    draw_polygons( tmp, t, g );
	    tmp->lastcol = 0;
	    break;
	    
	  case TORUS:
	    add_torus( tmp, op[i].op.torus.d[0], //cx
		       op[i].op.torus.d[1],     //cy
		       op[i].op.torus.d[2],    //cz
		       op[i].op.torus.r0,
		       op[i].op.torus.r1,
		       step);
	    matrix_mult( s->data[ s->top ], tmp );
	    draw_polygons( tmp, t, g );
	    tmp->lastcol = 0;
	    break;
	    
	  case BOX:
	    add_box( tmp, op[i].op.box.d0[0],
		     op[i].op.box.d0[1],
		     op[i].op.box.d0[2],
		     op[i].op.box.d1[0],
		     op[i].op.box.d1[1],
		     op[i].op.box.d1[2]);
	    matrix_mult( s->data[ s->top ], tmp );
	    draw_polygons( tmp, t, g );
	    tmp->lastcol = 0;
	    break;
	    
	  case LINE:
	    add_edge( tmp, op[i].op.line.p0[0],
		    op[i].op.line.p0[1],
		      op[i].op.line.p0[1],
		      op[i].op.line.p1[0],
		      op[i].op.line.p1[1],
		      op[i].op.line.p1[1]);
	    draw_lines( tmp, t, g );
	    tmp->lastcol = 0;
	    break;
	    
	  case MOVE:
	    //get the factors
	    xval = op[i].op.move.d[0];
	    yval =  op[i].op.move.d[1];
	    zval = op[i].op.move.d[2];
	    
	    transform = make_translate( xval, yval, zval );
	    //multiply by the existing origin
	    matrix_mult( s->data[ s->top ], transform );
	    //put the new matrix on the top
	    copy_matrix( transform, s->data[ s->top ] );
	    free_matrix( transform );
	    break;
	    
	  case SCALE:
	    xval = op[i].op.scale.d[0];
	    yval = op[i].op.scale.d[1];
	    zval = op[i].op.scale.d[2];

	    if (op[i].op.scale.p != NULL){
	      knob_value = lookup_symbol(op[i].op.scale.p->name)->s.value;
	      xval = xval * knob_value;
	      yval = yval * knob_value;
	      zval = zval * knob_value;
	    }
	    
	    transform = make_scale( xval, yval, zval );
	    matrix_mult( s->data[ s->top ], transform );
	    //put the new matrix on the top
	    copy_matrix( transform, s->data[ s->top ] );
	    free_matrix( transform );
	    break;
	    
	  case ROTATE:
	    xval = op[i].op.rotate.degrees * ( M_PI / 180 );

	    if (op[i].op.rotate.p != NULL){
	      knob_value = lookup_symbol(op[i].op.rotate.p->name)->s.value;
	      xval = xval * knob_value;
	    }

	    
	    //get the axis
	    if ( op[i].op.rotate.axis == 0 ) 
	      transform = make_rotX( xval );
	    else if ( op[i].op.rotate.axis == 1 ) 
	      transform = make_rotY( xval );
	    else if ( op[i].op.rotate.axis == 2 ) 
	      transform = make_rotZ( xval );
	    
	    matrix_mult( s->data[ s->top ], transform );
	    //put the new matrix on the top
	    copy_matrix( transform, s->data[ s->top ] );
	    free_matrix( transform );
	    break;
	    
	  case PUSH:
	    push( s );
	    break;
	  
	  case POP:
	    pop( s );
	    break;
      
	  case SAVE:
	    save_extension( t, op[i].op.save.p->name );
	    break;
	    
	  case DISPLAY:
	    display( t );
	    break;
	  }
	}

	sprintf(number,"%03d",frame);
	strcpy(fname,name);
	strcat(fname,number);
	strcat(fname,".png");
	save_extension(t,fname);
	printf("Saving %s ...\n",fname);          
    }

      struct vary_node *current;
      struct vary_node *previous;
      int index;
      
      for(index = 0; index < num_frames; index++){
	current = knobs[index];
	previous = knobs[index];
	while(current && current->next){
	  current = current->next;
	  free(previous);
	  previous = current;
	}
	free(current);
      }

      free_stack( s );
      free_matrix( tmp );
      //free_matrix( transform );
      
  }  
}
Exemplo n.º 27
0
Arquivo: my_main.c Projeto: stuydw/mdl
void my_main( int polygons ) {

    int i;
    double step = 0.01;
    double xval, yval, zval;
    double theta; // rotation angle measure
    int xyz; // rotation axis
    double w,h,d; // box dimensions
    double rad; // sphere/torus radius
    double rad2; // torus radius 2
    struct matrix *transform;
    struct matrix *tmp;
    struct stack *s;
    screen t;
    color g;

    g = change_color(6);

    s = new_stack();
    tmp = new_matrix(4, 1000);
    clear_screen( t );

    for (i=0; i<lastop; i++) {
        switch (op[i].opcode) {
        case PUSH:
            //if (i == 0) {
            tmp = new_matrix(4,4);
            ident(tmp);
            //}
            push(s);
            continue;
        case POP:
            pop(s);
            continue;
        case MOVE:
            xval = op[i].op.move.d[0];
            yval = op[i].op.move.d[1];
            zval = op[i].op.move.d[2];
            transform = make_translate(xval, yval, zval);
            matrix_mult(s->data[s->top], transform);
            s->data[s->top] = transform;
            continue;
        case SCALE:
            xval = op[i].op.scale.d[0];
            yval = op[i].op.scale.d[1];
            zval = op[i].op.scale.d[2];
            transform = make_scale(xval, yval, zval);
            matrix_mult(s->data[s->top], transform);
            s->data[s->top] = transform;
            continue;
        case ROTATE:
            xyz = op[i].op.rotate.axis;
            theta = op[i].op.rotate.degrees;
            if (xyz == 0)
                transform = make_rotX(theta);
            if (xyz == 1)
                transform = make_rotY(theta);
            if (xyz == 2)
                transform = make_rotZ(theta);
            matrix_mult(s->data[s->top], transform);
            s->data[s->top] = transform;
            continue;
        case BOX:
            xval = op[i].op.box.d0[0];
            yval = op[i].op.box.d0[1];
            zval = op[i].op.box.d0[2];
            w = op[i].op.box.d1[0];
            h = op[i].op.box.d1[1];
            d = op[i].op.box.d1[2];
            add_box(tmp, xval, yval, zval, w, h, d);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            continue;
        case SPHERE:
            xval = op[i].op.sphere.d[0];
            yval = op[i].op.sphere.d[1];
            zval = op[i].op.sphere.d[2];
            rad = op[i].op.sphere.r;
            add_sphere(tmp, xval, yval, zval, rad, step);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            continue;
        case TORUS:
            xval = op[i].op.torus.d[0];
            yval = op[i].op.torus.d[1];
            zval = op[i].op.torus.d[2];
            rad = op[i].op.torus.r0;
            rad2 = op[i].op.torus.r1;
            add_torus(tmp, xval, yval, zval, rad, rad2, step);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            continue;
        case LINE:
            xval = op[i].op.line.p0[0];
            yval = op[i].op.line.p0[1];
            zval = op[i].op.line.p0[2];
            w = op[i].op.line.p1[0];
            h = op[i].op.line.p1[1];
            d = op[i].op.line.p1[2];
            add_edge(tmp, xval, yval, zval, w, h, d);
            matrix_mult(s->data[s->top], tmp);
            draw_lines(tmp, t, g);
            continue;
        case SAVE:
            save_extension(t, op[i].op.save.p->name);
            continue;
        case DISPLAY:
            display(t);
            continue;
        case COMMENT:
            continue;
        }
    }
}
Exemplo n.º 28
0
Arquivo: parser.c Projeto: stuydw/3d
/*======== void parse_file () ==========
Inputs: char * filename
struct matrix * transform,
struct matrix * pm,
screen s
Returns:

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
Every command is a single character that takes up a line
Any command that requires arguments must have those arguments in the second line.
The commands are as follows:
l: add a line to the edge matrix -
takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
i: set the transform matrix to the identity matrix -
s: create a scale matrix,
then multiply the transform matrix by the scale matrix -
takes 3 arguments (sx, sy, sz)
t: create a translation matrix,
then multiply the transform matrix by the translation matrix -
takes 3 arguments (tx, ty, tz)
x: create an x-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
y: create an y-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
z: create an z-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
a: apply the current transformation matrix to the
edge matrix
v: draw the lines of the edge matrix to the screen
display the screen
g: draw the lines of the edge matrix to the screen
save the screen to a file -
takes 1 argument (file name)
q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename,
                  struct matrix * transform,
                  struct matrix * pm,
                  screen s) {
  char temp;
  float a,b,c,d,e,f,g,h;
  int i,j;
  char * ImgName;
  FILE * file = fopen(filename, "r");
  while(fscanf(file,"%c", &temp) ==1){
    //add torus
    if(temp == 'd'){
      fscanf(file,"%c",&temp);
    if(fscanf(file,"%f %f %f %f",&a,&b,&c,&d) == 4)
      add_torus(pm,a,b,c,d,0.01);
    else
      printf("somethings wrong that need to be fix in d");
    }
    //add sphere
    if(temp == 'm'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f", &a,&b,&c) == 3)
	add_sphere(pm,a,b,c,0.01);
      else
	printf("somethings wrong that need to be fix in m");
    }
    //add box point
    if(temp == 'p'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f) ==6)
	add_box(pm,a,b,c,d,e,f);
      else
	printf("somethings wrong that need to be fix in p\n");
    }
    //add edge
    if(temp == 'l'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f) == 6)
	add_edge(pm,a,b,c,d,e,f);
      else
	printf("SomeThings wrong that need to be fix in l\n");
    }
    //add circle
    if(temp == 'c'){
      fscanf(file,"%c", &temp);
      if(fscanf(file,"%f %f %f",&a,&b,&c) == 3)
	add_circle(pm,a,b,c,M_PI/20);
      else
	printf("fix in c\n");
    }
    //add hermite
    if(temp == 'h'){
      fscanf(file,"%c", &temp);
      if(fscanf(file,"%f %f %f %f %f %f %f %f", &a, &b,&c,&d,&e,&f,&g,&h) == 8)
	add_curve(pm,a,b,c,d,e,f,g,h,0.001,0);
      else
	printf("fix in h\n");
    }
    //add bezier
    if(temp == 'b'){
      fscanf(file,"%c", &temp);
      if(fscanf(file, "%f %f %f %f %f %f %f %f", &a, &b,&c,&d,&e,&f,&g,&h) == 8)
	add_curve(pm,a,b,c,d,e,f,g,h,0.001,1);
      else
	printf("fix in b\n");
    }
    //ident transform
    else if(temp == 'i'){
      ident(transform);
    }
    //scaling
    else if(temp == 's'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f", &a,&b,&c) == 3){
	struct matrix * scalar = make_scale(a,b,c);
	matrix_mult(scalar,transform);
      }
      else
	printf("Somethings wrong that need to be fix in s\n");
    }
    //Translating
    else if (temp == 't'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f", &d,&e,&f) == 3){
	struct matrix * tranlator = make_translate(d,e,f);
	matrix_mult(tranlator, transform);
      }
      else
	printf("somethings wrong that need to be fix in t\n");
    }
    //Rotating with x
    else if(temp == 'x'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f",&a) ==1){
	struct matrix * rotX = make_rotX(a);
	matrix_mult(rotX,transform);
      }
      else{
	printf("something wrong that need to be fix in x\n");
      }
    }
    //Rotating with Y
    else if(temp == 'y'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f",&b) ==1){
	struct matrix * rotY = make_rotY(b);
	matrix_mult(rotY,transform);
      }
      else{
	printf("something wrong that need to be fix in y\n");
      }
    }
    //Rotating with Z
    else if(temp == 'z'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f",&c) ==1){
	struct matrix * rotZ = make_rotZ(a);
	matrix_mult(rotZ,transform);
      }
      else{
	printf("something wrong that need to be fix in z\n");
      }
    }
    //Applying Transformation
    else if(temp == 'a'){
      matrix_mult(transform,pm);
    }
    else if(temp == 'v'){
      color c;
      c.red = 122;
      c.green = 218;
      c.blue = 225;
      
      clear_screen(s);
      for( i=0; i<XRES; i++){
	for ( j=0; j<YRES; j++){
	  
	  //c.red = random() % (MAX_COLOR + 1);
	  //c.green = random() % (MAX_COLOR + 1);
	  //c.blue = random() % (MAX_COLOR + 1);
	  plot( s, c, i, j);
	}
      }
      c.green = 40;
      draw_lines(pm,s,c);
      display(s);
    }
    else if(temp == 'g'){
      color c;
      c.red = 122;
      c.green = 218;
      c.blue = 225;
      
      clear_screen(s);
      for( i=0; i<XRES; i++){
	for ( j=0; j<YRES; j++){
	  
	  //c.red = random() % (MAX_COLOR + 1);
	  //c.green = random() % (MAX_COLOR + 1);
	  //c.blue = random() % (MAX_COLOR + 1);
	  plot( s, c, i, j);
	}
      }
      c.green = 40;
      draw_lines(pm,s,c);
      ImgName = (char *)malloc(256);
      
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%s",ImgName) ==1){
	ImgName = strsep(&ImgName, ".");
	save_ppm(s,ImgName);
	ImgName = strncat(ImgName,".png",4);
	save_extension(s,ImgName);
	free(ImgName);
      }
      else
	printf("Something wrong that need to be fix in g\n");
    }
    else if (temp == 'q'){
      return;
    }
    }
    }
Exemplo n.º 29
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {
 
  int i, f, j, k;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;
  char dir[256]; 
  char p[256]; 

  num_frames = 1; 
  step = 0.05        ;
 
  g.red = 0;
  g.green = 255;
  g.blue = 255;

  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  first_pass(); 
  if (num_frames == -1)
    return; 

  int variable; 
  struct vary_node **table; 
  table = second_pass(); 
  struct vary_node* inside; 

  for (variable = 0; variable < num_frames; variable++){
    clear_screen(t); 
    free_stack(s); 
    s = new_stack(); 
    inside = table[variable]; 

    while(inside){
      set_value(lookup_symbol(inside->name),inside->value);  
      inside = inside -> next; 
    }
      
    for (i=0;i<lastop;i++) {
  
      switch (op[i].opcode) {
    
      case SPHERE:
	add_sphere( tmp,op[i].op.sphere.d[0], //cx
		    op[i].op.sphere.d[1],  //cy
		    op[i].op.sphere.d[2],  //cz
		    op[i].op.sphere.r,
		    step);
	//apply the current top origin
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case TORUS:
	add_torus( tmp, op[i].op.torus.d[0], //cx
		   op[i].op.torus.d[1],     //cy
		   op[i].op.torus.d[2],    //cz
		   op[i].op.torus.r0,
		   op[i].op.torus.r1,
		   step);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case BOX:
	add_box( tmp, op[i].op.box.d0[0],
		 op[i].op.box.d0[1],
		 op[i].op.box.d0[2],
		 op[i].op.box.d1[0],
		 op[i].op.box.d1[1],
		 op[i].op.box.d1[2]);
	matrix_mult( s->data[ s->top ], tmp );
	draw_polygons( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case LINE:
	add_edge( tmp, op[i].op.line.p0[0],
		  op[i].op.line.p0[1],
		  op[i].op.line.p0[1],
		  op[i].op.line.p1[0],
		  op[i].op.line.p1[1],
		  op[i].op.line.p1[1]);
	draw_lines( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case MOVE:
	//get the factors
	if( op[ i ].op.move.p )
	  knob_value = lookup_symbol( op[ i ].op.move.p->name )->s.value;
	else
	  knob_value = 1;

	xval = op[i].op.move.d[0] * knob_value;
	yval = op[i].op.move.d[1] * knob_value;
	zval = op[i].op.move.d[2] * knob_value;

	transform = make_translate( xval, yval, zval );
	//multiply by the existing origin
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case SCALE:
	if( op[ i ].op.scale.p )
	  knob_value = lookup_symbol( op[ i ].op.scale.p->name )->s.value;
	else
	  knob_value = 1;
	xval = op[i].op.scale.d[0] * knob_value;
	yval = op[i].op.scale.d[1] * knob_value;
	zval = op[i].op.scale.d[2] * knob_value;
      
	transform = make_scale( xval, yval, zval );
	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case ROTATE:
	if( op[ i ].op.rotate.p )
	  knob_value = lookup_symbol( op[ i ].op.rotate.p->name )->s.value;
	else
	  knob_value = 1;

	xval = op[i].op.rotate.degrees * ( M_PI / 180 );

	//get the axis
	if ( op[i].op.rotate.axis == 0 ) 
	  transform = make_rotX( xval * knob_value );
	else if ( op[i].op.rotate.axis == 1 ) 
	  transform = make_rotY( xval * knob_value );
	else if ( op[i].op.rotate.axis == 2 ) 
	  transform = make_rotZ( xval * knob_value );

	matrix_mult( s->data[ s->top ], transform );
	//put the new matrix on the top
	copy_matrix( transform, s->data[ s->top ] );
	free_matrix( transform );
	break;

      case PUSH:
	push( s );
	break;
      case POP:
	pop( s );
	break;
      case SAVE:
	save_extension( t, op[i].op.save.p->name );
	break;
      case DISPLAY:
	display( t );
	break;
      case SET:
	set_value( lookup_symbol( op[ i ].op.set.p->name ), op[ i ].op.set.val );
	break;
      case SETKNOBS:
	for( k = 0; k < lastsym; k++ )
	  if( symtab[ k ].type == SYM_VALUE )
	    symtab[ k ].s.value = op[ i ].op.setknobs.value;
	break;
      default:
	break;
      }
       
    }
      
    strcpy(dir,name);
    sprintf(p,"%03d", variable);
    strcat(dir,p);
    strcat(dir, ".png");
    save_extension(t, dir);
    printf("%s \n", dir);
  }

  free(table); 
  free_stack( s );
  free_matrix( tmp );
  //free_matrix( transform );    
 


}
Exemplo n.º 30
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void my_main( int polygons ) {
    int i;
    double step;
    double xval, yval, zval;
    struct matrix *transform;
    struct matrix *tmp;
    struct stack *s;
    screen t;
    color g;

    g.red = 255;
    g.green = 255;
    g.blue = 255;


    first_pass();
    struct vary_node ** vals = second_pass();

    int j;
    for (j = 0; j < num_frames; j++) {
        s = new_stack();
        tmp = new_matrix(4, 1000);
        clear_screen( t );
        for (i=0;i<lastop;i++) {  
            struct vary_node *this_frame;
            this_frame = vals[j];
            while(this_frame) {
                set_value(lookup_symbol(this_frame->name),this_frame->value);
                this_frame = this_frame->next;
            }
            switch (op[i].opcode) {
                case PUSH:
                    push(s);
                    break;
                case POP:
                    pop(s);
                    break;
                case MOVE:
                    if (num_frames > 1 && op[i].op.move.p) {
                        step = lookup_symbol(op[i].op.move.p->name)->s.value;
                    }
                    else {
                        step = 1;
                    }
                    transform = make_translate(op[i].op.move.d[0]*step, op[i].op.move.d[1]*step, op[i].op.move.d[2]*step);
                    matrix_mult(transform, peek(s));
                    free_matrix(transform);
                    break;
                case SCALE:
                    if (num_frames > 1 && op[i].op.scale.p) {
                        step = lookup_symbol(op[i].op.scale.p->name)->s.value;
                    }
                    else {
                        step = 1;
                    }
                    transform = make_scale(op[i].op.scale.d[0]*step, op[i].op.scale.d[1]*step, op[i].op.scale.d[2]*step);
                    matrix_mult(transform, peek(s));
                    free_matrix(transform);
                    break;
                case ROTATE:
                    if (num_frames > 1 && op[i].op.rotate.p) {
                        step = lookup_symbol(op[i].op.rotate.p->name)->s.value;
                    }
                    else {
                        step = 1;
                    }
                    switch ((int) op[i].op.rotate.axis) {
                        case 0: // X
                            transform = make_rotX(rad(op[i].op.rotate.degrees*step));
                            break;
                        case 1: // Y
                            transform = make_rotY(rad(op[i].op.rotate.degrees*step));
                            break;
                        case 2: // Z
                            transform = make_rotZ(rad(op[i].op.rotate.degrees*step));
                            break;
                    }
                    matrix_mult(transform, peek(s));
                    free_matrix(transform);
                    break;
                case BOX:
                    add_box(tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2],
                            op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]);
                    matrix_mult(peek(s), tmp);
                    draw_polygons(tmp, t, g);
                    free_matrix(tmp);
                    tmp = new_matrix(4, 1000);
                    break;
                case SPHERE:
                    add_sphere(tmp, op[i].op.sphere.d[0], op[i].op.sphere.d[1], op[i].op.sphere.d[2],
                            op[i].op.sphere.r, .02);
                    matrix_mult(peek(s), tmp);
                    draw_polygons(tmp, t, g);
                    free_matrix(tmp);
                    tmp = new_matrix(4, 1000);
                    break;
                case TORUS:
                    add_torus(tmp, op[i].op.torus.d[0], op[i].op.torus.d[1], op[i].op.torus.d[2],
                            op[i].op.torus.r0, op[i].op.torus.r1, .02);
                    matrix_mult(peek(s), tmp);
                    draw_polygons(tmp, t, g);
                    free_matrix(tmp);
                    tmp = new_matrix(4, 1000);
                    break;
                case LINE:
                    add_edge(tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[2],
                            op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[2]);
                    matrix_mult(peek(s), tmp);
                    draw_lines(tmp, t, g);
                    free_matrix(tmp);
                    tmp = new_matrix(4, 1000);
                    break;
                case SAVE:
                    save_extension(t, op[i].op.save.p->name);
                    break;
                case DISPLAY:
                    display(t);
                    break;
            }
        }
        print_knobs();
        if (num_frames > 1) {
            char filename[128];
            char count[3];
            sprintf(count,"%03d",j);
            strcpy(filename, "animations/");
            strcat(filename, name);
            strcat(filename, count);
            strcat(filename, ".png");
            save_extension(t, filename);
            free_stack(s);
            free_matrix(tmp);
        }
    }
}