Ejemplo n.º 1
0
/*======== void process_knobs() ==========
  Inputs:   
  Returns: 
  Displays the current knob values and provides
  an rface for the user to set them
  jdyrlandweaver
  ====================*/
void process_knobs() {
  
  int i;
  double v;

  if ( lastsym == 0 )
    return;

  do {
    printf( "Knob List:\n" );
    print_knobs();
    printf( "Enter knob ID to set (-1 to stop): ");
    scanf( "%d", &i );
    
    if ( i >= lastsym || i < -1 )
      printf( "Invalid entry, please try again.\n" );
    
    else if ( i != -1 ) {
      
      printf( "Enter new value for %s: ", symtab[i].name );
      scanf( "%lf", &v );
      symtab[i].s.value = v;
    }
    printf("\n");
    
  } while ( i != -1 );
}
Ejemplo n.º 2
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);
        }
    }
}
Ejemplo n.º 3
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 = new_stack();
  screen t;
  clear_screen(t);
  color g;

  double factor;
  //struct vary_node **knobs;
  //struct vary_node *vn;
  char frame_name[128];

  num_frames = 1;
  step = 5;
  int animated = 0;
  
  g.red = 0;
  g.green = 255;
  g.blue = 255;
  
  first_pass(&num_frames, frame_name);
  if (num_frames!=1)
    animated=1;
  struct vary_node ** knobs=second_pass(num_frames);
  struct vary_node * current;
  j=0;
  while(j<num_frames){
    tmp=new_matrix(4, 4);
    struct stack *s=new_stack();
    if (animated){
      current=knobs[j];
      while (current->next){
	set_value(lookup_symbol(current->name), current->value);
	current=current->next;
      }
      set_value(lookup_symbol(current->name), current->value);
    }
    for (i=0;i<lastop;i++) {
      switch (op[i].opcode) {
	
      case SPHERE:
	add_sphere( tmp, op[i].op.sphere.d[0],  //x
		    op[i].op.sphere.d[1],       //y
		    op[i].op.sphere.d[2],       //z
		    op[i].op.sphere.r, 
		    step);

	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],  //x
		   op[i].op.torus.d[1],       //y
		   op[i].op.torus.d[2],       //z
		   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]);
	matrix_mult( s->data[ s->top ], tmp );
	draw_lines( tmp, t, g );
	tmp->lastcol = 0;
	break;

      case MOVE:
	//get the factor
	if (op[i].op.move.p)
	  factor=op[i].op.move.p->s.value;
	else
	  factor=1;
	if (factor != 0){
	  xval = op[i].op.move.d[0]*factor;
	  yval = op[i].op.move.d[1]*factor;
	  zval = op[i].op.move.d[2]*factor;

	  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)
	  factor=op[i].op.scale.p->s.value;
	else
	  factor=1;
	if (factor!=0){
	  xval = op[i].op.scale.d[0]*factor;
	  yval = op[i].op.scale.d[1]*factor;
	  zval = op[i].op.scale.d[2]*factor;
      
	  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)
	  factor=op[i].op.rotate.p->s.value;
	else
	  factor=1;
	if (factor!=0){
	  xval = op[i].op.rotate.degrees * ( M_PI / 180 )*factor;

	  //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 (animated){
      print_knobs();
      char * name[150];
      printf("frame number: %d\n", j);
      printf("name: %s\n", frame_name);
      if (num_frames < 10)
	sprintf(name, "animate/%s%0d.png", frame_name, j);
      else if (10 < num_frames < 100)
	sprintf(name, "animate/%s%03d.png", frame_name, j);
      //      else if (100 < num_frames < 1000)
      //sprintf(name, "animate/%s%04d.png", frame_name, j);
      printf("Saving %s\n\n", name);
      save_extension(t, name);
      clear_screen(t);
    }
    free_stack( s );
    free_matrix( tmp );
    j++;
  }
}