/*======== 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++; } }
/*======== 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; struct vary_node * temp; struct vary_node ** knobs; make_zbuf(); num_frames = 1; step = 0.05; g.red = 0; g.green = 255; g.blue = 255; first_pass(); if (num_frames >= 1) { knobs = second_pass(); } for (f = 0; f < num_frames; f++) { s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (j = 0; j < lastsym; j++) { if (symtab[j].type == SYM_VALUE) { temp = knobs[f]; while (strcmp(temp->name, symtab[j].name)) { temp = temp->next; } if (tmp) (&symtab[j])->s.value = temp->value; } } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SET: knob_value = op[i].op.set.p->s.value; set_value(lookup_symbol(op[i].op.set.p->name), knob_value); break; case SETKNOBS: knob_value = op[i].op.setknobs.value; for (j = 0; j < lastsym; j++) { if (symtab[j].type == SYM_VALUE) set_value(&(symtab[j]), knob_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: knob_value = 1; if (op[i].op.move.p) { temp = knobs[f]; while (strcmp(temp->name, op[i].op.move.p->name)) temp = temp->next; if (temp) knob_value = temp->value; } //get the factors 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: knob_value = 1; if (op[i].op.scale.p) { temp = knobs[f]; while (strcmp(temp->name, op[i].op.scale.p->name)) temp = temp->next; if (temp) knob_value = temp->value; } 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: knob_value = 1; if (op[i].op.rotate.p) { temp = knobs[f]; while (strcmp(temp->name, op[i].op.rotate.p->name)) temp = temp->next; if (temp) knob_value = temp->value; } xval = op[i].op.rotate.degrees * ( M_PI / 180 ) * 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; } } char dir[256]; char s[256]; strcpy(dir, "pics/"); sprintf(s, "%03d", f); strcat(dir, name); strcat(dir, s); strcat(dir, ".png"); save_extension(t, dir); } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
void my_main( int polygons ) { int i, axis; double step = 0.01; double xval, yval, zval, degrees, radius, r0, r1, x, y, z, h, w, d; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); g.red = 110; g.green = 115; g.blue = 120; 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 * M_PI / 180; if (axis == 0) transform = make_rotX(degrees); if (axis == 1) transform = make_rotY(degrees); if (axis == 2) transform = make_rotZ(degrees); matrix_mult(transform, s->data[s->top]); 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]; 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 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]; 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 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]; r0 = op[i].op.torus.r0; r1 = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, r0, r1, step); 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]; x = op[i].op.line.p1[0]; y = op[i].op.line.p1[1]; z = op[i].op.line.p1[2]; add_edge(tmp, xval, yval, zval, x, y, z); matrix_mult(s->data[s->top], tmp); draw_lines(tmp, t, g); break; case SAVE: display(t); save_extension(t, op[i].op.save.p->name); break; case DISPLAY: display(t); break; } } }
/*======== 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; color g; g.red = 255; g.green = 0; 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); char c; double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; 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 'i': ident(transform); break; case 'c': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, .001); break; case 'h': //hermite fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x, &y, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x, y, x1, y1, x2, y2, x3, y3, .001, 0); break; case 'b': //bezier fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x, &y, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x, y, x1, y1, x2, y2, x3, y3, .001, 1); 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"); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s color c 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 , color c ) { FILE *file = fopen(filename,"r"); char *line = (char *)malloc(MAX_LINE_SIZE); double *args = (double *)malloc(MAX_ARG_SIZE * sizeof(double)); while(fgets(line,MAX_LINE_SIZE,file)) { switch (line[0]) { case 'l': read_args(file,args); add_edge(pm,args[0],args[1],args[2],args[3],args[4],args[5]); break; case 'i': ident(transform); break; case 's': read_args(file,args); struct matrix *scale = make_scale(args[0],args[1],args[2]); matrix_mult(scale,transform); free_matrix(scale); break; case 't': read_args(file,args); struct matrix *translate = make_translate(args[0],args[1],args[2]); matrix_mult(translate,transform); free_matrix(translate); break; case 'x': read_args(file,args); struct matrix *rotx = make_rotX(args[0]); matrix_mult(rotx,transform); free_matrix(rotx); break; case 'y': read_args(file,args); struct matrix *roty = make_rotY(args[0]); matrix_mult(roty,transform); free_matrix(roty); break; case 'z': read_args(file,args); struct matrix *rotz = make_rotZ(args[0]); matrix_mult(rotz,transform); free_matrix(rotz); break; case 'a': matrix_mult(transform,pm); break; case 'v': clear_screen(s); draw_lines(pm,s,c); display(s); break; case 'g': draw_lines(pm,s,c); fgets(line,MAX_LINE_SIZE,file); save_extension(s,line); break; case 'q': return; default: printf("fatal: el comando no se encuentra\n"); return; } } free(line); free(args); fclose(file); }
/*======== 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 red,green,blue; double vx,vy,vz; vx = 0; vy = 0; vz = -1; int i, f, j; double* vector; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct matrix *zbuffer = new_Zmatrix(XRES,YRES); //print_matrix(zbuffer); struct stack *s; screen t; color g; struct light* light=(struct light*)malloc(sizeof(struct light)); light->l[0]=0;light->l[1]=0;light->l[2]=0; light->c[0]=0;light->c[0]=0;light->c[0]=0; light->next=NULL; struct light* cur_light; int ind; struct constants* constants=(struct constants*)malloc(sizeof(struct constants)); constants->red=1.0;constants->blue=1.0;constants->green=1.0; struct vary_node **knobs; struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; g.red = 0; g.green = 0; g.blue = 0; first_pass(); if (num_frames == 1) process_knobs(); else knobs = second_pass(); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case LIGHT: cur_light = light; for (;cur_light->next!=NULL;cur_light=cur_light->next){} cur_light->next=lookup_symbol(op[i].op.light.p->name)->s.l; 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; } } for ( f=0; f < num_frames; f++ ) { s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); //if there are multiple frames, set the knobs if ( num_frames > 1 ) { vn = knobs[f]; while ( vn ) { printf("knob: %s value:%lf\n", vn->name, vn->value); set_value( lookup_symbol( vn->name ), vn->value ); vn = vn-> next; } } 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.p->s.value ); break; case SETKNOBS: for ( j=0; j < lastsym; j++ ) if ( symtab[j].type == SYM_VALUE ) symtab[j].s.value = op[i].op.setknobs.value; break; case CONSTANTS: //constants=lookup_symbol(op[i].op.constants.p->name)->s.c; break; /* case LIGHT: cur_light = light; for (;light->next!=NULL;light=light->next){} light->next=lookup_symbol(op[i].op.light.p->name)->s.l; break; */ case CAMERA: vx = op[i].op.camera.eye[0] - op[i].op.camera.aim[0]; vy = op[i].op.camera.eye[1] - op[i].op.camera.aim[1]; vz = op[i].op.camera.eye[2] - op[i].op.camera.aim[2]; vector = normalize(vx,vy,vz); vx = vector[0]; vy = vector[1]; vz = vector[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 ); Zdraw_polygons( tmp, t, g ,zbuffer, light, constants,vx,vy,vz); tmp->lastcol = 0; //printf("%s\n","drawing sphere"); 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 ); Zdraw_polygons( tmp, t, g ,zbuffer, light, constants,vx,vy,vz); tmp->lastcol = 0; printf("%s\n","drawing torus"); 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 ); Zdraw_polygons( tmp, t, g ,zbuffer,light, constants,vx,vy,vz); tmp->lastcol = 0; printf("%s\n","drawing box"); 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]); Zdraw_lines( tmp, t, g ,zbuffer); 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]; //get knob if it exists if ( op[i].op.move.p != NULL ) { knob_value = lookup_symbol( op[i].op.move.p->name )->s.value; xval = xval * knob_value; yval = yval * knob_value; zval = zval * 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: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; //get knob if it exists 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 ); //get knob if it exists 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 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 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 ); //save the image with the correct filename if ( num_frames > 1 ) { printf("Drawing frome: %d\n", f ); sprintf( frame_name, "anim/%s%03d.png", name, f ); save_extension( t, frame_name ); } } //end frame loop }
/*======== void my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commands 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 entire 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; int k; char *p; p = name; int m; struct vary_node* caller; knob_value = 1; double carry; 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 < 0){ printf("AH...NO, NOT WITHOUT FRAMES CAN YOU VARY.\n"); } struct vary_node ** kn = second_pass(); for(k = 0; k < num_frames; k++){ for(i = 0; i < XRES; i++){ for(j = 0; j < YRES; j++){ zBuffer[i][j] = -2147483647; } } for ( i=0; i < lastsym; i++ ) { if ( symtab[i].type == SYM_VALUE ) { caller = kn[k]; while((caller) && (strcmp(caller->name,symtab[i].name) != 0)){ caller = caller->next; } if(caller){ (&symtab[i])->s.value = caller->value; } } } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SETKNOBS: //carry = op[i].op.setknobs.value; for( i = 0; i < lastsym; i++){ if(symtab[i].type == SYM_VALUE){ set_value(&(symtab[i]), op[i].op.setknobs.value); } } break; case SET: //carry = op[i].op.set.p->s.value; set_value(lookup_symbol(op[i].op.set.p->name), op[i].op.set.p->s.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 //pointInfo(op[i].op.sphere.constants->name); matrix_mult( s->data[ s->top ], tmp ); pointInfo(op[i].op.sphere.constants->name); ambi.red = ambiRed; ambi.green = ambiGreen; ambi.blue = ambiBlue; draw_polygons( tmp, t, g ); //printf("%f %f %f\n", ambiRed, ambiGreen, ambiBlue); 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); pointInfo(op[i].op.torus.constants->name); 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]); pointInfo(op[i].op.box.constants->name); 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: if(op[i].op.move.p != NULL){ caller = kn[k]; while((caller) && (strcmp(caller->name, op[i].op.move.p->name) != 0)){ caller = caller->next; } if(caller){ knob_value = caller->value; } } //get the factors 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 != NULL){ caller = kn[k]; while((caller) && (strcmp(caller->name, op[i].op.scale.p->name) != 0)){ caller = caller->next; } if(caller){ knob_value = caller->value; } } 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 != NULL){ caller = kn[k]; while((caller) && (strcmp(caller->name, op[i].op.rotate.p->name) != 0)){ caller = caller->next; } if(caller){ knob_value = caller->value; } } xval = op[i].op.rotate.degrees * ( M_PI / 180 ) * 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; } } strcpy(name, op[refer].op.basename.p->name); char value[256]; char adder[256]; strcpy(value, "hermano/"); strcat(value, name); sprintf(adder, "%04d", k); strcat(value, adder); strcat(value, ".png"); save_extension(t, value); clear_screen(t); s = new_stack(); tmp = new_matrix(4,1000); } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== 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 * file = fopen(filename, "r"); char line[256]; double a, b, c, d, e, f; char z[256]; color col; col.red = 64; col.green = 64; col.blue = 64; struct matrix * temp; while (fgets(line, sizeof(line), file)) { if (line[0] == 'l') { fgets(line, sizeof(line), file); sscanf(line, "%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f); add_edge(pm, a, b, c, d, e, f); } else if (line[0] == 'i') ident(transform); else if (line[0] == 's') { fgets(line, sizeof(line), file); sscanf(line, "%lf %lf %lf", &a, &b, &c); temp = make_scale(a, b, c); matrix_mult(temp, transform); } else if (line[0] == 't') { fgets(line, sizeof(line), file); sscanf(line, "%lf %lf %lf", &a, &b, &c); temp = make_translate(a, b, c); matrix_mult(temp, transform); } else if (line[0] == 'x') { fgets(line, sizeof(line), file); sscanf(line, "%lf", &a); temp = make_rotX(a); matrix_mult(temp, transform); } else if (line[0] == 'y') { fgets(line, sizeof(line), file); sscanf(line, "%lf", &a); temp = make_rotY(a); matrix_mult(temp, transform); } else if (line[0] == 'z') { fgets(line, sizeof(line), file); sscanf(line, "%lf", &a); temp = make_rotZ(a); matrix_mult(temp, transform); } else if (line[0] == 'a') matrix_mult(transform, pm); else if (line[0] == 'v') { draw_lines(pm, s, col); display(s); } else if (line[0] == 'g') { draw_lines(pm, s, col); fgets(line, sizeof(line), file); sscanf(line, "%s", z); save_extension(s, z); } else if (line[0] == 'q') break; else { printf("Invalid command.\n"); break; } } fclose(file); }
/*======== 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 <F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3>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 ) { struct vary_node ** knob_vals; 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; struct vary_node * temp_node; 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) exit(1); knob_vals= second_pass(); // print_knobs(); for(k = 0; k < num_frames; k++){ //print_knobs(); //printf("%lu %lu\n", knob_vals, knob_vals[0]); //printf("first node: %s %f\n", knob_vals[0]->name, knob_vals[1]->value); for(temp_node = knob_vals[k]; temp_node; temp_node = temp_node->next){ //print_knobs(); set_value(lookup_symbol(temp_node->name), temp_node -> value); } 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 knob_value = 1; if(op[i].op.move.p) knob_value = (double)op[i].op.move.p->s.value; 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: knob_value = 1; if(op[i].op.scale.p) knob_value = op[i].op.scale.p->s.value; 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: knob_value = 1; if(op[i].op.rotate.p) knob_value = op[i].op.rotate.p->s.value; xval = (op[i].op.rotate.degrees * ( M_PI / 180 )) * 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; } } //strcpy(name, op[].op.basename.p->name); char value[256]; char adder[256]; // strcpy(value, "pic"); // strcat(value, name); strcpy(value, name); sprintf(adder, "%04d", k); //k++; strcat(value, adder); strcat(value, ".png"); save_extension(t, value); //} clear_screen(t); s = new_stack(); } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== 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]; char next[256]; char *next_arg; color c; c.red=255;c.blue=255;c.green=25; double args[52]; double DEFAULT_STEP=0.01; 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'; if (strcmp(line,"apply")==0){ matrix_mult(transform,pm); } else if (strcmp(line,"display")==0){ draw_lines(pm,s,c); display(s); } else if (strcmp(line,"quit")==0){ exit(0); } else if (strcmp(line,"ident")==0){ ident(transform); } else{ fgets(next,255,f); next[strlen(next)-1]='\0'; next_arg=next; int i = 0; while (next_arg != 0){ args[i]=strtod(strsep(&next_arg," "),NULL); i++; } if (strcmp(line,"save")==0){ fgets(next,255,f); save_extension(s,next); } } if (strcmp(line,"line")==0){ add_edge(pm,args[0],args[1],args[2],args[3],args[4],args[5]); } if (strcmp(line,"circle")==0){ add_circle(pm,args[0],args[1],args[2],DEFAULT_STEP); } if (strcmp(line,"hermite")==0){ add_curve(pm,args[0],args[1],args[4],args[5],args[2]-args[0],args[3]-args[1],args[6]-args[4],args[7]-args[5],DEFAULT_STEP,HERMITE_MODE); } if (strcmp(line,"bezier")==0){ add_curve(pm,args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],DEFAULT_STEP,BEZIER_MODE); } if (strcmp(line,"scale")==0){ matrix_mult(make_scale(args[0],args[1],args[2]),transform); } if (strcmp(line,"translate")==0){ matrix_mult(make_translate(args[0],args[1],args[2]),transform); } if (strcmp(line,"xrotate")==0){ matrix_mult(make_rotX(M_PI*args[0]/180.0),transform); } if (strcmp(line,"yrotate")==0){ matrix_mult(make_rotY(M_PI*args[0]/180.0),transform); } if (strcmp(line,"zrotate")==0){ matrix_mult(make_rotZ(M_PI*args[0]/180.0),transform); } printf(":%s:\n",line); } }
/*======== 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, color c) { FILE * fp = fopen(filename, "r"); char * lines = (char *)malloc(1); float x0, y0, z0, x1, y1, z1, x2, y2, x3, y3; float step; float cx, cy, r; float x, y, z; float theta; while(fscanf(fp, "%c", lines)) { printf("lines gets: %c \n", *lines); switch (*lines){ case 'c': { fscanf(fp, "%f %f %f %f", &cx, &cy, &r, &step); add_circle(pm, cx, cy, r, step); break; } case 'b': { fscanf(fp, "%f %f %f %f %f %f %f %f %f", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3, &step); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 0); break; } case 'h': { fscanf(fp, "%f %f %f %f %f %f %f %f %f", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3, &step); add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, step, 1); break; } case 'l': { fscanf(fp, "%f %f %f %f %f %f", &x0, &y0, &z0, &x1, &y1, &z1); add_edge(pm, x0, y0, z0, x1, y1, z1); break; } case 'i': { ident(transform); break; } case 's': { fscanf(fp, "%f %f %f", &x, &y, &z); struct matrix * scale = make_scale(x, y, z); matrix_mult(scale, transform); free(scale); break; } case 't': { fscanf(fp, "%f %f %f", &x, &y, &z); struct matrix * trans = make_translate(x, y, z); matrix_mult(trans, transform); free(trans); break; } case 'x': { fscanf(fp, "%f", &theta); struct matrix * transx = make_rotX(theta); matrix_mult(transx, transform); free(transx); break; } case 'y': { fscanf(fp, "%f", &theta); struct matrix * transy = make_rotY(theta); matrix_mult(transy, transform); free(transy); break; } case 'z': { fscanf(fp, "%f", &theta); struct matrix * transz = make_rotZ(theta); matrix_mult(transz, transform); free(transz); break; } case 'a': { matrix_mult(transform, pm); break; } case 'v': { clear_screen(s); draw_lines(pm, s, c); display(s); break; } case 'g': { char * file = (char *)malloc(256); fscanf(fp, "%s", file); clear_screen(s); draw_lines(pm, s, c); save_extension(s, file); free(file); break; } case 'q': { exit(0); } default: { exit(1); } } fscanf(fp, "%c", lines); } fclose(fp); free(lines); }
/*======== 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]; struct matrix * tmp; double angle; color g; g.red = 100; g.green = 0; 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, z2, x3, y3, z3, x4, y4, z4, w, h, d,step,r1,r2, cx,cy,r; 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, "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, "box", strlen(line)) == 0 ){ printf("%s", line); fgets(line, 255, f); printf("%s", line); sscanf(line, "%lf %lf %lf %lf %lf %lf ", &x, &y, &z, &w, &h, &d); add_box( pm, x,y,z,w,h,d); } else if (strncmp(line, "sphere", strlen(line)) == 0 ){ printf("%s", line); fgets(line, 255, f); printf("%s\n", line); sscanf(line, "%lf %lf %lf %lf ", &cx, &cy, &r, &step); add_sphere( pm, cx, cy, r, step); } else if (strncmp(line, "torus", strlen(line)) == 0 ){ //printf("%s", line); fgets(line, 255, f); //printf("%s", line); sscanf(line, "%lf %lf %lf %lf %lf ", &cx, &cy, &r1, &r2, &step); add_torus( pm, cx,cy,r1,r2,step); } 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_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 if ( strncmp(line, "#", 1 ) == 0 ){ printf(" Comment \n"); } else if ( strncmp(line, "clear", strlen(line)) == 0 ){ clear_matrix( pm ); printf("Clear? What's that?\n"); } else { printf("Invalid command: [%s]\n", line); //printf("%c\n", line[0]); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
void my_main( int polygons ) { int i; double step = 10; //default steps double xval, yval, zval; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; double *tp; //temp pointer s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case COMMENT: //ignore break; case PUSH: push(s); //check break; case POP: pop(s); //check 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]); 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(transform, s->data[s->top]); free_matrix(transform); break; case ROTATE: xval = op[i].op.rotate.degrees; switch( (int)op[i].op.rotate.axis /*should be 0, 1, or 2*/ ) { case 0: //x-axis transform = make_rotX(xval); break; case 1: //y-axis transform = make_rotY(xval); break; case 2: //z-axis transform = make_rotZ(xval); break; default: break; } matrix_mult(transform, s->data[s->top]); free_matrix(transform); break; case BOX: xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; tp = (double*)op[i].op.box.d1; add_box(tmp, xval, yval, zval, tp[0], tp[1], tp[2]); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); tmp->lastcol = 0; break; case SPHERE: tp = op[i].op.sphere.d; xval = op[i].op.sphere.r; add_sphere(tmp, tp[0], tp[1], tp[2], xval, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); tmp->lastcol = 0; break; case TORUS: tp = op[i].op.torus.d; xval = op[i].op.torus.r0; yval = op[i].op.torus.r1; add_torus(tmp, tp[0], tp[1], tp[2], xval, yval, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); tmp->lastcol = 0; break; case LINE: tp = op[i].op.line.p0; xval = op[i].op.line.p1[0]; yval = op[i].op.line.p1[1]; zval = op[i].op.line.p1[2]; add_edge(tmp, tp[0], tp[1], tp[2], xval, yval, zval); draw_lines(tmp, t, g); tmp->lastcol = 0; break; case SAVE: save_extension(t, op[i].op.save.p->name); break; case DISPLAY: display(t); break; default: fprintf(stdout, "Unrecognized opcode...\n"); break; } //close switch } //close for-loop } //close func
/*======== 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 * origins = new_stack(); // print_matrix( origins->data[origins->top]); 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, r; 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( origins->data[ origins->top ], pm); draw_lines( pm, s, g); pm->lastcol = 0; // 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); matrix_mult( origins->data[ origins->top ], pm); draw_lines( pm, s, g); pm->lastcol = 0; //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( origins->data[ origins->top ], pm); draw_lines( pm, s, g); pm->lastcol = 0; //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( origins->data[ origins->top ], pm); draw_lines( pm, s, g); pm->lastcol = 0; //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( origins->data[ origins->top ], pm); // print_matrix( origins->data[origins->top]); draw_polygons( pm, s, g); pm->lastcol = 0; // 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( origins->data[ origins->top ], pm); print_matrix( origins->data[origins->top]); draw_polygons( pm, s, g); pm->lastcol = 0; // printf( "SPHERE\n%lf %lf %lf %lf\n\n", x, y, z, r); } 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( origins->data[ origins->top ], pm); print_matrix( origins->data[origins->top]); draw_polygons( pm, s, g); pm->lastcol = 0; //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( origins->data[ origins->top ], tmp); copy_matrix(tmp, origins->data[ origins->top ]); print_matrix( origins->data[origins->top]); // origins->data[origins->top] = tmp; //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); // print_matrix( tmp ); /* tmp2 = origins->data[origins->top]; matrix_mult(tmp, tmp2); print_matrix( tmp2); */ matrix_mult( origins->data[ origins->top ], tmp); copy_matrix(tmp, origins->data[ origins->top ]); print_matrix( origins->data[origins->top]); // origins->data[origins->top] = tmp; //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( origins->data[ origins->top ], tmp); copy_matrix(tmp, origins->data[ origins->top ]); // origins->data[origins->top] = tmp; } 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( origins->data[ origins->top ], tmp); copy_matrix(tmp, origins->data[ origins->top ]); // origins->data[origins->top] = tmp; } 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( origins->data[ origins->top ], tmp); copy_matrix(tmp, origins->data[ origins->top ]); // origins->data[origins->top] = tmp; } 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, "push", strlen(line)) == 0 ) { push(origins); // print_matrix( origins->data[ origins->top ]); printf( "%s\n", g.red); printf("\n"); } else if ( strncmp(line, "pop", strlen(line)) == 0 ) { pop(origins); } else if ( strncmp(line, "change_color", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); g.red = x; g.green = y; g.blue = z; } 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'; 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_stack(origins); free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== 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); color c; c.red = 0; c.green = 0; c.blue = 0; if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { if(strstr(line,"\n")) line[strlen(line)-1]='\0'; if(!strcmp(line, "quit")){ printf("quitting\n"); fclose(f); return; } else if (!strcmp(line, "display")) { draw_lines(pm,s,c); display(s); } else if (!strcmp(line, "save")) { draw_lines(pm,s,c); fgets(line, 256, f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; printf("saving\n"); save_extension(s,line); } else if (!strcmp(line, "apply")){ printf("applying\n"); matrix_mult(transform,pm); } else if (!strcmp(line, "line")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double x0,y0,z0,x1,y1,z1; sscanf(tmp,"%lf %lf %lf %lf %lf %lf", &x0, &y0, &z0, &x1, &y1, &z1); add_edge(pm,x0,y0,z0,x1,y1,z1); } else if (!strcmp(line, "circle")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double cx,cy,r; sscanf(tmp,"%lf %lf %lf",&cx,&cy,&r); add_circle(pm,cx,cy,r,420); printf("drawing a circle\n"); } else if (!strcmp(line, "hermite")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double x0,y0,x1,y1,x2,y2,x3,y3; sscanf(tmp,"%lf %lf %lf %lf %lf %lf %lf %lf", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3); add_curve(pm, x0, y0, x1-x0,y1-y0, x2, y2, x3-x2, y3-y2,100, HERMITE_MODE); printf("making a hermite curve\n"); } else if (!strcmp(line, "bezier")){ fgets(line,255,f); if (strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double x0,y0,x1,y1,x2,y2,x3,y3; sscanf(tmp,"%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,100, BEZIER_MODE); printf("making a bezier curve\n"); } else if (!strcmp(line, "ident")){ ident(transform); } else if (!strcmp(line, "scale")){ fgets(line,255,f); if(strstr(line, "\n")) line[strlen(line)-1]='\0'; char *tmp = line; double sx,sy,sz; sscanf(tmp,"%lf %lf %lf",&sx,&sy,&sz); matrix_mult(make_scale(sx,sy,sz),transform); } else if (!strcmp(line, "translate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double tx,ty,tz; sscanf(tmp,"%lf %lf %lf",&tx,&ty,&tz); matrix_mult(make_translate(tx,ty,tz),transform); } else if (!strcmp(line,"xrotate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double theta; sscanf(tmp, "%lf", &theta); theta*=180/M_PI; matrix_mult(make_rotX(theta),transform); } else if (!strcmp(line,"yrotate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double theta; sscanf(tmp, "%lf", &theta); theta*=180/M_PI; matrix_mult(make_rotY(theta),transform); } else if (!strcmp(line,"zrotate")){ fgets(line,255,f); if(strstr(line,"\n")) line[strlen(line)-1]='\0'; char *tmp = line; double theta; sscanf(tmp, "%lf", &theta); theta *=180/M_PI; matrix_mult(make_rotZ(theta),transform); } } }
/*======== 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; 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); } 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, "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, "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); } 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, 1); //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, 1); //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, "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); //printf("THE ISSUE IS HERE\n"); //display(s); //printf("ISSUE AVERTED\n"); // // //so i have been having repeated issue with imagemagik and thus display doesnt work, so i test by saving as a png save_extension(s, "parser.png"); } 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"); }
/*======== 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 *fp = fopen(filename,"r"); char* script = (char*)malloc(sizeof(char) * 256); while(fgets(script, 256, fp)){ script = strsep(&script, "\n"); //printf("script: \n%s\n", script); if(strncmp(script, "l", sizeof("l")) == 0){ fgets(script, 256, fp); script = strsep(&script, "\n"); int x0 = atoi(strsep(&script, " ")); int y0 = atoi(strsep(&script, " ")); int z0 = atoi(strsep(&script, " ")); int x1 = atoi(strsep(&script, " ")); int y1 = atoi(strsep(&script, " ")); int z1 = atoi(script); // add_edge(pm,atoi(strsep(&script," ")),atoi(strsep(&script," ")), atoi(strsep(&script," ")), atoi(strsep(&script," ")), atoi(strsep(&script," ")),atoi(script)); add_edge(pm, x0, y0, z0, x1, y1, z1); } if(strncmp(script, "i",sizeof("i")) == 0){ ident(transform); } if(strncmp(script, "s",sizeof("s"))==0){ fgets(script, 256, fp); struct matrix* scale = make_scale(atof(strsep(&script," ")), atof(strsep(&script," ")), atof(script)); matrix_mult(scale, transform); } if(strncmp(script, "t",sizeof("t"))==0){ fgets(script, 256, fp); int x1 = atoi(strsep(&script, " ")); int y1 = atoi(strsep(&script, " ")); int z1 = atoi(script); struct matrix* translation = make_translate(x1,y1,z1); matrix_mult(translation, transform); } if(strncmp(script, "x", sizeof("x"))==0){ fgets(script, 256, fp); int theta = atof(script); struct matrix* rotX = make_rotX(theta); print_matrix(transform); matrix_mult(rotX, transform); print_matrix(transform); } if(strncmp(script, "y", sizeof("y"))==0){ fgets(script, 256, fp); int theta = atof(script); struct matrix* rotY = make_rotY(theta); matrix_mult(rotY, transform); } if(strncmp(script, "z", sizeof("z"))==0){ fgets(script, 256, fp); int theta = atof(script); struct matrix* rotZ = make_rotZ(theta); matrix_mult(rotZ, transform); } if(strncmp(script, "a",sizeof("a"))==0){ matrix_mult(transform, pm); } if(strncmp(script, "v",sizeof("v"))==0){ color c; c.blue = 225; c.green = 200; c.red = 90; draw_lines(pm, s, c); display(s); } if(strncmp(script, "g",sizeof("g"))==0){ fgets(script, 256, fp); script = strsep(&script,"\n"); color c; c.blue = 225; c.green = 200; c.red = 90; draw_lines(pm, s, c); save_extension(s, script); } if(strncmp(script,"q",sizeof("q"))==0){ break; } } }