void draw_shortest_path(struct Status s,unsigned int path_length,unsigned int* path){ int i,k; char j,collision; // reference polygons struct polygon obstacle1, obstacle2, link1Poly,link2Poly,link3Poly; struct point link1BaseRef,link2BaseRef,link3BaseRef;; generate_obstacles_and_links(&obstacle1, &obstacle2, &link1Poly, &link2Poly, &link3Poly , &link1BaseRef , &link2BaseRef , &link3BaseRef); int numberOfObstacles=2; struct polygon *obstacleList; obstacleList=(struct polygon*)malloc(numberOfObstacles*sizeof(struct polygon)); obstacleList[0]=obstacle1; obstacleList[1]=obstacle2; // list of all polygons used for drawing static struct polygon* polygons; polygons=(struct polygon*)malloc(5*sizeof(struct polygon)); static struct point displacedLinkEnd1, displacedLinkEnd2, displacedLinkEnd3; static struct polygon displacedLink1, displacedLink2, displacedLink3; initTempPolys(link1Poly,link2Poly,link2Poly, &displacedLink1 , &displacedLink2 , &displacedLink3 ); // iterate through sample_list and check for collision wit obstacles double temp[3]; for(i=0;i< path_length ;i++){ temp[0]=s.cs_total[ path[i] ][0]; temp[1]=s.cs_total[ path[i] ][1]; temp[2]=s.cs_total[ path[i] ][2]; displaceLinkPoly(temp[0], &displacedLink1, &displacedLinkEnd1, link1BaseRef, link1Poly, link2BaseRef); displaceLinkPoly(temp[0]+temp[1],&displacedLink2,&displacedLinkEnd2,displacedLinkEnd1,link2Poly,link3BaseRef); displaceLinkPoly(temp[0]+temp[1]+temp[2],&displacedLink3,&displacedLinkEnd3,displacedLinkEnd2, link3Poly, link3BaseRef); polygons[0] = displacedLink1; polygons[1] = displacedLink2; polygons[2] = displacedLink3; polygons[3] = obstacleList[0]; polygons[4] = obstacleList[1]; draw_polygons(polygons,5,1000); } for(i=0;i<20;i++){ draw_polygons(polygons,5,100000); } free(polygons); free(displacedLink1.x_list); free(displacedLink2.x_list); free(displacedLink3.x_list); free(displacedLink1.y_list); free(displacedLink2.y_list); free(displacedLink3.y_list); }
//------------------------------------------------------------------------ void draw_scene(scanline_rasterizer& ras, solid_renderer& solid, draft_renderer& draft) { ras.gamma(agg::gamma_none()); srand(100); if(m_draw_nodes.status()) { if(m_draft.status()) { draw_nodes_draft(); } else { draw_nodes_fine(ras); } } if(m_draw_edges.status()) { if(m_draft.status()) { switch(m_type.cur_item()) { case 0: draw_lines_draft(); break; case 1: draw_curves_draft(); break; case 2: draw_dashes_draft(); break; } } else { switch(m_type.cur_item()) { case 0: draw_lines_fine(ras, solid, draft); break; case 1: draw_curves_fine(ras, solid, draft); break; case 2: draw_dashes_fine(ras, solid, draft); break; case 3: case 4: draw_polygons(ras, solid, draft); break; } } } }
void Frustrum::Draw( void ) { if ( ! enabled ) return; PrepDraw(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable( GL_LIGHTING ); draw_polygons(); glDisable( GL_LIGHTING ); glColor4fv( OpenGLColors[ RED ] ); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE ); draw_end(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable( GL_LIGHTING ); FinishDraw(); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) sphere: add a sphere to the edge matrix - takes 3 arguemnts (cx, cy, r) torus: add a torus to the edge matrix - takes 4 arguemnts (cx, cy, r1, r2) box: add a rectangular prism to the edge matrix - takes 6 arguemnts (x, y, z, width, height, depth) clear: clear the currnt edge matrix - takes 0 arguments ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; struct stack* shakestack; //relative coordinate system stack int draw_mode=-1; //equals 0 if draw_edges, equals 1 if draw_polygons shakestack = new_stack(); g.red = 0; g.green = 255; g.blue = 0; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); draw_mode = 0; } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 0; } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { //printf("BEZIER\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 0; } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 0; } else if ( strncmp(line, "box", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); draw_mode = 1; } else if (strncmp(line, "sphere", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 10); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 1; } else if (strncmp(line, "torus", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 10); //printf( "%lf %lf %lf\n", x, y, z); draw_mode = 1; } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); //matrix_mult(tmp, transform); //print_matrix(transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); //matrix_mult(tmp, transform); //print_matrix(transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); //matrix_mult(tmp, transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); //matrix_mult(tmp, transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); //matrix_mult(tmp, transform); matrix_mult(tmp, shakestack->data[ shakestack->top ]); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); //matrix_mult(transform, pm); matrix_mult(shakestack->data[ shakestack->top ], pm); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { clear_screen(s); if( draw_mode==0 ) { draw_lines(pm, s, g); } else { draw_polygons(pm, s, g); } display(s); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); if( draw_mode==0 ) { draw_lines(pm, s, g); } else { draw_polygons(pm, s, g); } save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { pm->lastcol = 0; } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } //==================STACK COMMANDS================================= else if( strncmp(line, "push", strlen(line)) == 0 ) { push(shakestack); print_stack(shakestack); } else if( strncmp(line, "pop", strlen(line)) == 0 ) { pop(shakestack); print_stack(shakestack); } else if ( line[0] == '#' ) { printf("Invalid command\n"); } //adding color coommand================== else if( strncmp(line, "color", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%i %i %i", &x, &y, &z); g.red = x; g.green = y; g.blue = z; } else { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== void my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; char q; extern double zbuff[XRES][YRES]; num_frames = -1; step = 0.05; strcpy(name, "lol\0"); g.red = 0; g.green = 255; g.blue = 255; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); instantiate_zbuff(); first_pass(); struct vary_node** frames = second_pass(); //test_second_pass(frames); if (num_frames < 2) { for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } } else { int currframe; for (currframe = 0; currframe < num_frames; currframe++) { //Caryy out all commands once per frame. struct vary_node*knobs = frames[currframe]; for (i=0;i<lastop;i++) { switch (op[i].opcode) { /*EXPLANATION case COMMAND: If op[i].op.command.cs == NULL, then no knob is defined for this command. Then do it normally. Otherwise, iterate to the "end. But there are two "ends"... If the value of this knob is -1, you went to the end of the list without finding the corresponding knob. That means it isnt there. Since the command is meant to be varied, but isn't defined for this frame, then don't draw it at this time. If the name of this knob in the linked list matches the name of the corresponding knob, then crry out the command in its knob'd format. */ case SET: while ( (knobs->value != -1) && strcmp( op[i].op.set.p->name, knobs->name ) ) knobs = knobs->next; if (knobs->value == -1) break; knobs->value = op[i].op.set.val; break; case SETKNOBS: while ( knobs->value != -1 ) { knobs->value = op[i].op.set.val; knobs = knobs->next; } break; case SPHERE: if (!op[i].op.sphere.cs) { add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); } else { while ( (knobs->value != -1) && strcmp( op[i].op.sphere.cs->name, knobs->name ) ) knobs = knobs->next; double value = knobs->value; if (value == -1) break; add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r*value, step); } //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: if (!op[i].op.torus.cs) { add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); } else { while ( (knobs->value != -1) && strcmp( op[i].op.torus.cs->name, knobs->name ) ) knobs = knobs->next; double value = knobs->value; if (value == -1) break; add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0*value, op[i].op.torus.r1*value, step); } matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: if (!op[i].op.box.cs) { add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); } else { while ( (knobs->value != -1) && strcmp( op[i].op.box.cs->name, knobs->name ) ) knobs = knobs->next; double value = knobs->value; if (value == -1) break; add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0]*value, op[i].op.box.d1[1]*value, op[i].op.box.d1[2]*value); } matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: if (!op[i].op.scale.p) { xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; } else { while ( (knobs->value != -1) && strcmp( op[i].op.scale.p->name, knobs->name ) ) knobs = knobs->next; double value = knobs->value; if (value == -1) break; xval = op[i].op.scale.d[0]*value; yval = op[i].op.scale.d[1]*value; zval = op[i].op.scale.d[2]*value; } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: if (!op[i].op.rotate.p) { xval = op[i].op.rotate.degrees * ( M_PI / 180 ); } else { while ( (knobs->value != -1) && strcmp( op[i].op.rotate.p->name, knobs->name ) ) knobs = knobs->next; double value = knobs->value; if (value == -1) break; xval = op[i].op.rotate.degrees * ( M_PI / 180 ) * value; } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } char framename[256]; mkdir("animation", 0777); sprintf(framename, "animation/%s%04d.png", name, currframe); save_extension( t, framename); clear_screen( t ); } } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== 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; double step; double xval, yval, zval; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; g.red = 255; g.green = 0; g.blue = 0; s = new_stack(); transform = new_matrix(4, 1); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { // push case PUSH: push(s); break; // pop case POP: pop(s); break; // rotate case ROTATE: xval = op[i].op.rotate.axis; yval = op[i].op.rotate.degrees; yval = yval * (M_PI / 180); if (xval == 0) { tmp = make_rotX(yval); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); } else if (xval == 1) { tmp = make_rotY(yval); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); } else { tmp = make_rotZ(yval); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); } break; // scale case SCALE: tmp = make_scale(op[i].op.scale.d[0],op[i].op.scale.d[1], op[i].op.scale.d[2]); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); break; // move case MOVE: tmp = make_translate(op[i].op.move.d[0],op[i].op.move.d[1], op[i].op.move.d[2]); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); break; // box case BOX: add_box(transform, 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], transform); draw_polygons(transform, t, g); ident(transform); break; // sphere case SPHERE: add_sphere(transform, op[i].op.sphere.d[0],op[i].op.sphere.d[1], op[i].op.sphere.d[2], op[i].op.sphere.r, 5); matrix_mult(s->data[s->top], transform); draw_polygons(transform, t, g); ident(transform); break; // torus case TORUS: add_torus(transform, 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, 5); matrix_mult(s->data[s->top], transform); draw_polygons(transform, t, g); ident(transform); break; // display case DISPLAY: display(t); break; } } }
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; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { //simple cases //i realized that you already defined these constants in another //file after looking at this for 30 min -_- case POP: pop(s); break; case PUSH: push(s); break; //move,scale,rotate case MOVE: transform = make_translate(op[i].op.move.d[0],op[i].op.move.d[1], op[i].op.move.d[2]); matrix_mult(transform,s->data[s->top]); free_matrix(transform); break; case ROTATE: //there are 3 rotations possible, SO SWITCH-CEPTION! switch((int)op[i].op.rotate.axis) { case ROT_X: transform = make_rotX(op[i].op.rotate.degrees); break; case ROT_Y: transform = make_rotY(op[i].op.rotate.degrees); break; case ROT_Z: transform = make_rotZ(op[i].op.rotate.degrees); break; } matrix_mult(transform,s->data[s->top]); free_matrix(transform); break; case SCALE: transform = make_scale(op[i].op.scale.d[0],op[i].op.scale.d[1], op[i].op.scale.d[2]); matrix_mult(transform,s->data[s->top]); free_matrix(transform); break; //box,sphere,torus 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); free_matrix(tmp); //reset 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,0.01); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); free_matrix(tmp); //reset 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,0.01); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); free_matrix(tmp); //reset tmp=new_matrix(4,1000); break; //line case LINE: add_edge(tmp,op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2], op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]); matrix_mult(s->data[s->top],tmp); draw_lines(tmp,t,g); free_matrix(tmp); tmp=new_matrix(4,1000);//RESET break; //EVERYTIN else case SAVE: save_extension(t,op[i].op.save.p->name); break; case DISPLAY: display(t); break; default: break; } } }
/*======== void my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j, k; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; char q; char dir[256]; char p[256]; num_frames = 1; step = 0.05 ; g.red = 0; g.green = 255; g.blue = 255; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); first_pass(); if (num_frames == -1) return; int variable; struct vary_node **table; table = second_pass(); struct vary_node* inside; for (variable = 0; variable < num_frames; variable++){ clear_screen(t); free_stack(s); s = new_stack(); inside = table[variable]; while(inside){ set_value(lookup_symbol(inside->name),inside->value); inside = inside -> next; } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors if( op[ i ].op.move.p ) knob_value = lookup_symbol( op[ i ].op.move.p->name )->s.value; else knob_value = 1; xval = op[i].op.move.d[0] * knob_value; yval = op[i].op.move.d[1] * knob_value; zval = op[i].op.move.d[2] * knob_value; transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: if( op[ i ].op.scale.p ) knob_value = lookup_symbol( op[ i ].op.scale.p->name )->s.value; else knob_value = 1; xval = op[i].op.scale.d[0] * knob_value; yval = op[i].op.scale.d[1] * knob_value; zval = op[i].op.scale.d[2] * knob_value; transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: if( op[ i ].op.rotate.p ) knob_value = lookup_symbol( op[ i ].op.rotate.p->name )->s.value; else knob_value = 1; xval = op[i].op.rotate.degrees * ( M_PI / 180 ); //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval * knob_value ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval * knob_value ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval * knob_value ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; case SET: set_value( lookup_symbol( op[ i ].op.set.p->name ), op[ i ].op.set.val ); break; case SETKNOBS: for( k = 0; k < lastsym; k++ ) if( symtab[ k ].type == SYM_VALUE ) symtab[ k ].s.value = op[ i ].op.setknobs.value; break; default: break; } } strcpy(dir,name); sprintf(p,"%03d", variable); strcat(dir,p); strcat(dir, ".png"); save_extension(t, dir); printf("%s \n", dir); } free(table); free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== void my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, j; zbuf = (double **)malloc(500 * sizeof(double*)); for(i = 0; i < 500; i++){ zbuf[i] = (double*)malloc(500 * sizeof(double)); for (j = 0; j < 500; j++){ zbuf[i][j] = -DBL_MAX; } } lclist = (lcons*)malloc(sizeof(lcons)); lclist->next = NULL; i = 0; j = 0; int f; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; char q; obj = 0; cbmt[0] = ib; cbmt[1] = im; cbmt[2] = it; int jkl = 0; for ( i=0; i < lastsym; i++ ) { if ( symtab[i].type == SYM_LIGHT ) { jkl++; } } ptl = jkl; ptlights = (double**)malloc(j * sizeof(double*)); for (i = 0; i < jkl; i++){ ptlights[i] = (double*)malloc(6 * sizeof(double)); } jkl = 0; for ( i=0; i < lastsym; i++ ) { if ( symtab[i].type == SYM_LIGHT ) { ptlights[jkl][0] = symtab[i].s.l->l[0]; ptlights[jkl][1] = symtab[i].s.l->l[1]; ptlights[jkl][2] = symtab[i].s.l->l[2]; ptlights[jkl][3] = symtab[i].s.l->c[0]; ptlights[jkl][4] = symtab[i].s.l->c[1]; ptlights[jkl][5] = symtab[i].s.l->c[2]; jkl++; } } for(i = 0;i < ptl; i++){ printf("%lf,%lf,%lf light at (%lf,%lf,%lf)\n",ptlights[i][3],ptlights[i][4],ptlights[i][5],ptlights[i][0],ptlights[i][1],ptlights[i][2]); } clear_screen( t ); num_frames = 1; step = 0.02; g.red = 40; g.green = 50; g.blue = 60; s = new_stack(); tmp = new_matrix(4, 1000); first_pass(); srand(time(NULL)); if (num_frames < 0){ printf("ERROR- can't vary knobs when there's only 1 frame.\n"); } if (num_frames >= 1){ struct vary_node ** klist = second_pass(); for (f = 0; f < num_frames; f++){ for ( i = 0; i < 500; i++){ for (j = 0; j < 500; j++){ zbuf[i][j] = -DBL_MAX; } } for (i=0;i<lastop;i++) { setc = 0; struct vary_node* v = klist[f]; SYMTAB * vvv; while ( v != NULL){ //printf("set %s %lf \n",v->name, v->value ); vvv = lookup_symbol(v->name); if (vvv != NULL){ set_value(vvv, v->value); } else{ add_symbol(v->name,SYM_VALUE,(void *)&(v->value)); } v = v->next; } //print_knobs(); switch (op[i].opcode) { case SETKNOBS: xval = 0; double abcdef = op[i].op.setknobs.value; //printf("Setting knobs to %lf.\n",abcdef); for ( i=0; i < lastsym; i++ ) { if ( symtab[i].type == SYM_VALUE ) { set_value(&(symtab[i]), abcdef); } } break; case AMBIENT: g.red = op[i].op.ambient.c[0]; g.green = op[i].op.ambient.c[1]; g.blue = op[i].op.ambient.c[2]; break; case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); ///////////////////////////lcons if (f == 0){ lcons * v = lclist; while (v->next != NULL){ v = v->next; } v->next = (lcons*)malloc(sizeof(lcons)); v->next->next = NULL; v->next->kar = .685* rand() / (double)RAND_MAX; v->next->kag = .685* rand() / (double)RAND_MAX; v->next->kab = .685* rand() / (double)RAND_MAX; v->next->kdr = .685* rand() / (double)RAND_MAX; v->next->kdg = .685* rand() / (double)RAND_MAX; v->next->kdb = .685* rand() / (double)RAND_MAX; v->next->ksr = .685* rand() / (double)RAND_MAX; v->next->ksg = .685* rand() / (double)RAND_MAX; v->next->ksb = .685* rand() / (double)RAND_MAX; } draw_polygons( tmp, t, g ); tmp->lastcol = 0; obj++; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); ///////////////////////////lcons if (f == 0){ lcons * v = lclist; while (v->next != NULL){ v = v->next; } v->next = (lcons*)malloc(sizeof(lcons)); v->next->next = NULL; v->next->kar = .685* rand() / (double)RAND_MAX; v->next->kag = .685* rand() / (double)RAND_MAX; v->next->kab = .685* rand() / (double)RAND_MAX; v->next->kdr = .685* rand() / (double)RAND_MAX; v->next->kdg = .685* rand() / (double)RAND_MAX; v->next->kdb = .685* rand() / (double)RAND_MAX; v->next->ksr = .685* rand() / (double)RAND_MAX; v->next->ksg = .685* rand() / (double)RAND_MAX; v->next->ksb = .685* rand() / (double)RAND_MAX; } draw_polygons( tmp, t, g ); tmp->lastcol = 0; obj++; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); ///////////////////////////lcons if (f == 0){ lcons * v = lclist; while (v->next != NULL){ v = v->next; } v->next = (lcons*)malloc(sizeof(lcons)); v->next->next = NULL; v->next->kar = .685* rand() / (double)RAND_MAX; v->next->kag = .685* rand() / (double)RAND_MAX; v->next->kab = .685* rand() / (double)RAND_MAX; v->next->kdr = .685* rand() / (double)RAND_MAX; v->next->kdg = .685* rand() / (double)RAND_MAX; v->next->kdb = .685* rand() / (double)RAND_MAX; v->next->ksr = .685* rand() / (double)RAND_MAX; v->next->ksg = .685* rand() / (double)RAND_MAX; v->next->ksb = .685* rand() / (double)RAND_MAX; } draw_polygons( tmp, t, g ); tmp->lastcol = 0; obj++; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[2], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[2]); matrix_mult( s->data[ s->top ], tmp ); ///////////////////////////lcons if (f == 0){ lcons * v = lclist; while (v->next != NULL){ v = v->next; } v->next = (lcons*)malloc(sizeof(lcons)); v->next->next = NULL; v->next->kar = .685* rand() / (double)RAND_MAX; v->next->kag = .685* rand() / (double)RAND_MAX; v->next->kab = .685* rand() / (double)RAND_MAX; v->next->kdr = .685* rand() / (double)RAND_MAX; v->next->kdg = .685* rand() / (double)RAND_MAX; v->next->kdb = .685* rand() / (double)RAND_MAX; v->next->ksr = .685* rand() / (double)RAND_MAX; v->next->ksg = .685* rand() / (double)RAND_MAX; v->next->ksb = .685* rand() / (double)RAND_MAX; } draw_lines( tmp, t, g ); tmp->lastcol = 0; obj++; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; //printf("MOVE %lf %lf %lf\n",xval,yval,zval); if (op[i].op.move.p != NULL){ SYMTAB* thing = (lookup_symbol(op[i].op.move.p->name)); xval *= thing->s.value; yval *= thing->s.value; zval *= thing->s.value; //printf("new MOVE %lf %lf %lf\n",xval,yval,zval); } transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; //printf("scalE %lf %lf %lf\n",xval,yval,zval); if (op[i].op.scale.p != NULL){ SYMTAB* thing = (lookup_symbol(op[i].op.scale.p->name)); xval *= thing->s.value; yval *= thing->s.value; zval *= thing->s.value; //printf("new scale %lf %lf %lf\n",xval,yval,zval); } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); //printf("rotate %lf\n",xval); if (op[i].op.rotate.p != NULL){ xval *= (lookup_symbol(op[i].op.rotate.p->name))->s.value; //printf("new rotate%lf\n",xval); } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } if (num_frames > 1){ char nopq[256]; char rst[256]; strcpy(nopq,"animations/"); strcat(nopq, name); sprintf (rst, "%03d", f ); strcat(nopq,rst); strcat(nopq,".png"); printf("Saved frame %d to %s\n", (f+1) ,nopq); save_extension( t, nopq ); clear_screen( t ); screen t; if (f < num_frames - 1){ obj = 0; g.red = 40; g.green = 50; g.blue = 60; } free_stack( s ); free_matrix( tmp ); s = new_stack(); tmp = new_matrix(4, 1000); } //printf("finished frame %d\n",f); } //////////////////////////////////////////////////////////// for (j = 0; j < num_frames; j++){ struct vary_node * v2 = klist[j]; struct vary_node * v = klist[j]; while (v2 != NULL){ v = v2; v2 = v2->next; free(v); } } free(klist); } ///////////////////////////////////////////////////// lcons* v2 = lclist; lcons* v = lclist; while (v2 != NULL){ v = v2; v2 = v2->next; free(v); } //////////////////////////////////////////////// /*for ( i=0; i < lastsym; i++ ) { if ( symtab[i].type == SYM_LIGHT ) { //printf( "Freeing %s:\n", symtab[i].name); //print_light(symtab[i].s.l); //free(symtab[i].s.l); } }*/ //////////////////////////////////////////////////// free_stack( s ); free_matrix( tmp ); for(i = 0; i < 500; i++){ free(zbuf[i]); } for (i = 0; i < ptl; i++){ free(ptlights[i]); } free(ptlights); free(zbuf); printf("ambient light = %d,%d,%d\n",g.red,g.green,g.blue); //free_matrix( transform ); }
void my_main( int polygons ) { int i, j; double step = 0.05; // Decided to make step this value double xval, yval, zval; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; g = change_color(2); int axis; // 0,1,2 correspond to x,y,z axes of rotation, respectively double measure; // corresponds to angle of rotation double width, height, depth; // For box double radius; // For sphere double r1, r2; // For torus double x2,y2,z2; // For line s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case COMMENT: break; case PUSH: push(s); break; case POP: pop(s); break; case MOVE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform = make_scale(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; break; case ROTATE: axis = op[i].op.rotate.axis; measure = op[i].op.rotate.degrees; if(axis == 0){ transform = make_rotX(measure);} if(axis == 1){ transform = make_rotY(measure);} if(axis == 2){ transform = make_rotZ(measure);} matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; break; case BOX: for(j = 0; j < tmp->lastcol; j++){ tmp->m[0][j] = 0; tmp->m[1][j] = 0; tmp->m[2][j] = 0; } tmp->lastcol = 0; xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; width = op[i].op.box.d1[0]; height = op[i].op.box.d1[1]; depth = op[i].op.box.d1[2]; add_box(tmp, xval, yval, zval, width, height, depth); matrix_mult(s->data[s->top], tmp); //matrix_mult(transform, tmp); draw_polygons(tmp, t, g); //free_matrix(tmp); break; case SPHERE: for(j = 0; j < tmp->lastcol; j++){ tmp->m[0][j] = 0; tmp->m[1][j] = 0; tmp->m[2][j] = 0; } tmp->lastcol = 0; xval = op[i].op.sphere.d[0]; yval = op[i].op.sphere.d[1]; zval = op[i].op.sphere.d[2]; radius = op[i].op.sphere.r; add_sphere(tmp, xval, yval, zval, radius, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case TORUS: for(j = 0; j < tmp->lastcol; j++){ tmp->m[0][j] = 0; tmp->m[1][j] = 0; tmp->m[2][j] = 0; } tmp->lastcol = 0; xval = op[i].op.torus.d[0]; yval = op[i].op.torus.d[1]; zval = op[i].op.torus.d[2]; r1 = op[i].op.torus.r0; r2 = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, r1, r2, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case LINE: for(j = 0; j < tmp->lastcol; j++){ tmp->m[0][j] = 0; tmp->m[1][j] = 0; tmp->m[2][j] = 0; } tmp->lastcol = 0; xval = op[i].op.line.p0[0]; yval = op[i].op.line.p0[1]; zval = op[i].op.line.p0[2]; x2 = op[i].op.line.p1[0]; y2 = op[i].op.line.p1[1]; z2 = op[i].op.line.p1[2]; add_edge(tmp, xval, yval, zval, x2, y2, z2); matrix_mult(s->data[s->top], tmp); draw_lines(tmp, t, g); break; case SAVE: save_extension(t, op[i].op.save.p->name); break; case DISPLAY: display(t); break; } } //free_stack(s); //free_matrix(transform); }
void my_main( int polygons ) { int i, ax; double step; double xval, yval, zval, x2, y2, z2; double w, h, d, r1, r2, m; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; g.red = 250; g.blue = 0; g.green = 0; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0; i<lastop; i++) { switch (op[i].opcode) { case COMMENT: break; case PUSH: push(s); break; case POP: pop(s); break; case MOVE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate(xval, yval, zval); matrix_mult(transform, s->data[s->top]); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform = make_scale(xval, yval, zval); matrix_mult(transform, s->data[s->top]); break; case ROTATE: ax = op[i].op.rotate.axis; m = op[i].op.rotate.degrees; if(ax == 0) transform = make_rotX(m); if(ax == 1) transform = make_rotY(m); if(ax ==2) transform = make_rotZ(m); matrix_mult(transform, s->data[s->top]); break; case BOX: xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; w = op[i].op.box.d1[0]; h = op[i].op.box.d1[1]; d = op[i].op.box.d1[2]; add_box(tmp, xval, yval, zval, w, h, d); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case SPHERE: xval = op[i].op.sphere.d[0]; yval = op[i].op.sphere.d[1]; zval = op[i].op.sphere.d[2]; r1 = op[i].op.sphere.r; add_sphere(tmp, xval, yval, zval, r1, .01); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case TORUS: xval = op[i].op.torus.d[0]; yval = op[i].op.torus.d[1]; zval = op[i].op.torus.d[2]; r1 = op[i].op.torus.r0; r2 = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, r1, r2, .01); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case LINE: xval = op[i].op.line.p0[0]; yval = op[i].op.line.p0[1]; zval = op[i].op.line.p0[2]; x2 = op[i].op.line.p1[0]; y2 = op[i].op.line.p1[1]; z2 = op[i].op.line.p1[2]; add_edge(tmp, xval, yval, zval, x2, y2, z2); matrix_mult(s->data[s->top], tmp); draw_lines(tmp, t, g); break; case SAVE: save_extension(t, op[i].op.save.p->name); break; case DISPLAY: display(t); break; } } }
/*======== void my_main() ========== Inputs: Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; struct vary_node **knobs; struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; g.red = 0; g.green = 255; g.blue = 255; first_pass(); if (num_frames>1){ knobs = second_pass(); } int cur_frame; char frame_num_string[4]; for (cur_frame=0;cur_frame<num_frames-1;cur_frame++){ strcpy(frame_name,"animation_frames/"); strcat(frame_name,name); sprintf(frame_num_string,"%03d",cur_frame+1); strcat(frame_name,frame_num_string); s = new_stack(); tmp = new_matrix(4,0); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case SET: vn = knobs[cur_frame]; while (vn != NULL){ if (strcmp(vn->name,op[i].op.set.p->name)==0){ vn->value = op[i].op.set.val; } vn = vn->next; } break; case SETKNOBS: vn = knobs[cur_frame]; while (vn != NULL){ vn->value = op[i].op.setknobs.value; vn = vn->next; } break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; if (op[i].op.move.p != NULL){ vn = knobs[cur_frame]; while (vn != NULL){ if (strcmp(vn->name,op[i].op.scale.p->name)==0){ xval*=vn->value; yval*=vn->value; zval*=vn->value; } vn = vn->next; } } transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; if (op[i].op.scale.p != NULL){ vn = knobs[cur_frame]; while (vn != NULL){ if (strcmp(vn->name,op[i].op.scale.p->name)==0){ xval*=vn->value; yval*=vn->value; zval*=vn->value; } vn = vn->next; } } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); if (op[i].op.scale.p == NULL){ vn = knobs[cur_frame]; while (vn != NULL){ if (strcmp(vn->name,op[i].op.rotate.p->name)==0){ xval*=vn->value; } vn = vn->next; } } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } if (num_frames > 1){ save_extension( t, frame_name ); clear_screen(t); free_stack( s ); free_matrix( tmp ); } } }
void my_main( int polygons ) { int i; double step; double xval, yval, zval, xcor, ycor, zcor, angle, radius; struct matrix *transform; struct matrix *tmp; screen t; color g; g.red = 0; g.green = 255; g.blue = 0; struct stack *s; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { printf("loop %d\n", i); switch (op[i].opcode){ case PUSH: push(s); printf("%s\n", "pop\n"); break; case POP: pop(s); printf("%s\n", "pop\n"); break; case MOVE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; tmp = make_translate(xval, yval, zval); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); printf("%s\n", "move\n"); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; tmp = make_scale(xval, yval, zval); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); printf("%s\n", "scale\n"); break; case ROTATE: angle = op[i].op.rotate.degrees * M_PI / 180; if(op[i].op.rotate.axis == 0){ tmp = make_rotX(angle); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); } else if(op[i].op.rotate.axis == 1){ tmp = make_rotY(angle); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); } else{ tmp = make_rotZ(angle); matrix_mult(s->data[s->top], tmp); copy_matrix(tmp, s->data[s->top]); } printf("%s\n", "rotate\n"); break; case BOX: xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; xcor = op[i].op.box.d0[0]; ycor = op[i].op.box.d0[1]; zcor = op[i].op.box.d0[2]; add_box(tmp, xval, yval, zval, xcor, ycor, zcor); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); printf("box\n"); break; case SPHERE: xval = op[i].op.sphere.d[0]; yval = op[i].op.sphere.d[1]; zval = op[i].op.sphere.d[2]; radius = op[i].op.sphere.r; add_sphere(tmp, xval, yval, zval, radius, 10); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); printf("%s\n", "sphere\n"); break; case TORUS: xval = op[i].op.torus.d[0]; yval = op[i].op.torus.d[1]; zval = op[i].op.torus.d[2]; double inner = op[i].op.torus.r0; double outer = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, inner, outer, 10); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); printf("%s\n", "torus\n"); break; case LINE: xval = op[i].op.line.d[0]; yval = op[i].op.line.d[1]; zval = op[i].op.line.d[2]; xcor = op[i].op.line.d[0]; ycor = op[i].op.line.d[1]; zcor = op[i].op.line.d[2]; add_edge(tmp, xval, yval, zval, xcor, ycor, zcor); matrix_mult(s->data[s->top], tmp); draw_lines(tmp, t, g); printf("%s\n", "line\n"); break; case SAVE: save_extension(t, op[i].op.save.p->name); printf("%s\n", "save\n"); break; case DISPLAY: display(t); printf("%s\n", "display\n"); } } }
/*======== void my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j, n; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; struct vary_node ** knobs; struct vary_node * link; screen t; color g; char q; num_frames = 1; step = 0.05; g.red = 0; g.green = 255; g.blue = 255; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); first_pass(); if(num_frames > 1) { knobs = second_pass(); } int toomanyvariables; for(toomanyvariables = 0; toomanyvariables < num_frames; toomanyvariables++) { s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (j = 0; j < lastsym; j++) { if (symtab[j].type == SYM_VALUE) { link = knobs[toomanyvariables]; while (strcmp(link->name, symtab[j].name) != 0) { link = link->next; } if(link) { (&symtab[j])->s.value = link->value; } } } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SET: n = op[i].op.set.p->s.value; set_value(lookup_symbol(op[i].op.set.p->name),n); break; case SETKNOBS: n = op[i].op.setknobs.value; for (j = 0; j < lastsym; j++) { if (symtab[j].type == SYM_VALUE) { set_value(&(symtab[j]), n); } } break; case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; if (op[i].op.move.p) { SYMTAB * variable = (lookup_symbol(op[i].op.move.p->name)); xval = xval * variable->s.value; yval = yval * variable->s.value; zval = zval * variable->s.value; } transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; if (op[i].op.scale.p) { SYMTAB * variable = (lookup_symbol(op[i].op.scale.p->name)); xval = xval * variable->s.value; yval = yval * variable->s.value; zval = zval * variable->s.value; } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); if (op[i].op.rotate.p) { SYMTAB * variable = (lookup_symbol(op[i].op.rotate.p->name)); xval = xval * variable->s.value; } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } char directoryname[256]; char index[256]; strcpy(directoryname, "animations/"); strcat(directoryname, name); sprintf(index, "%03d", toomanyvariables); strcat(directoryname, index); strcat(directoryname, ".png"); save_extension(t, directoryname); printf("Saved %s\n", directoryname); } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== 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); } } }
void my_main( int polygons ) { int i, axis; double xval, xval1, yval, yval1, zval, zval1, degrees, width, height, depth, r, r1; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; g = change_color(0); s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case PUSH: push(s); break; case POP: pop(s); break; case MOVE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate(xval, yval, zval); matrix_mult(transform, s->data[s->top]); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform = make_scale(xval, yval, zval); matrix_mult(transform, s->data[s->top]); break; case ROTATE: axis = op[i].op.rotate.axis; degrees = op[i].op.rotate.degrees; if(axis == 0) transform = make_rotX(degrees); else if(axis == 1) transform = make_rotY(degrees); else if(axis == 2) transform = make_rotZ(degrees); matrix_mult(transform, s->data[s->top]); break; case BOX: free_matrix(tmp); tmp = new_matrix(4, 1000); xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; width = op[i].op.box.d1[0]; height = op[i].op.box.d1[1]; depth = op[i].op.box.d1[2]; add_box(tmp, xval, yval, zval, width, height, depth); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case SPHERE: free_matrix(tmp); tmp = new_matrix(4, 1000); xval = op[i].op.sphere.d[0]; yval = op[i].op.sphere.d[1]; zval = op[i].op.sphere.d[2]; r = op[i].op.sphere.r; add_sphere(tmp, xval, yval, zval, r, 0.05); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case TORUS: free_matrix(tmp); tmp = new_matrix(4, 1000); xval = op[i].op.torus.d[0]; yval = op[i].op.torus.d[1]; zval = op[i].op.torus.d[2]; r = op[i].op.torus.r0; r1 = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, r, r1, 0.05); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case LINE: free_matrix(tmp); tmp = new_matrix(4, 1000); xval = op[i].op.line.p0[0]; yval = op[i].op.line.p0[1]; zval = op[i].op.line.p0[2]; xval1 = op[i].op.line.p1[0]; yval1 = op[i].op.line.p1[1]; zval1 = op[i].op.line.p1[2]; add_edge(tmp, xval, yval, zval, xval1, yval1, zval1); matrix_mult(s->data[s->top], tmp); draw_lines(tmp, t, g); break; case SAVE: save_extension(t, op[i].op.save.p->name); break; case DISPLAY: display(t); break; } } free_stack(s); free_matrix(tmp); }
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; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); void clear_tmp(){ for(j = 0; j < tmp->lastcol; j++){ tmp->m[0][j] = 0; tmp->m[1][j] = 0; tmp->m[2][j] = 0; } tmp->lastcol = 0; } for (i=0;i<lastop;i++) { clear_tmp(); switch (op[i].opcode) { case COMMENT: break; case PUSH: push(s); break; case POP: pop(s); break; case SAVE: save_extension(t, op[i].op.save.p->name); break; case MOVE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top]=transform; break; case SCALE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_scale(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top]=transform; break; case LINE: xval = op[i].op.line.p0[0]; yval = op[i].op.line.p0[1]; zval = op[i].op.line.p0[2]; x2 = op[i].op.line.p1[0]; y2 = op[i].op.line.p1[1]; z2 = op[i].op.line.p1[2]; add_edge(tmp, xval, yval, zval, x2, y2, z2); matrix_mult(s->data[s->top], tmp); draw_lines(tmp, t, g); break; case BOX: xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; width = op[i].op.box.d1[0]; height = op[i].op.box.d1[1]; depth = op[i].op.box.d1[2]; add_box(tmp, xval, yval, zval, width, height, depth); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case SPHERE: xval = op[i].op.sphere.d[0]; yval = op[i].op.sphere.d[1]; zval = op[i].op.sphere.d[2]; radius = op[i].op.sphere.r; add_sphere(tmp, xval, yval, zval, radius, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case TORUS: xval = op[i].op.torus.d[0]; yval = op[i].op.torus.d[1]; zval = op[i].op.torus.d[2]; r1 = op[i].op.torus.r0; r2 = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, r1, r2, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case DISPLAY: display(t); break; } } }
void my_main( int polygons ) { int i,ROT_AXIS; double step; double xval, yval, zval; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; g.red = 122; g.green = 218; g.blue = 225; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case PUSH: push(s); break; case POP: pop(s); break; case DISPLAY: display(t); break; case SAVE: save_extension(t,op[i].op.save.p->name); break; case MOVE: transform = make_translate(op[i].op.move.d[0], op[i].op.move.d[1], op[i].op.move.d[2]); matrix_mult(transform,s->data[s->top]); break; case SCALE: transform = make_scale(op[i].op.scale.d[0], op[i].op.scale.d[1], op[i].op.scale.d[2]); matrix_mult(transform,s->data[s->top]); break; case ROTATE: ROT_AXIS = op[i].op.rotate.axis; if(ROT_AXIS = 0) transform = make_rotX(op[i].op.rotate.degrees); else if(ROT_AXIS = 1) transform = make_rotY(op[i].op.rotate.degrees); else if(ROT_AXIS = 2) transform = make_rotZ(op[i].op.rotate.degrees); matrix_mult(transform,s->data[s->top]); break; case LINE: add_edge(tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[2], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[2]); matrix_mult(s->data[s->top],tmp); draw_lines(tmp,t,g); break; case SPHERE: add_sphere(tmp,op[i].op.sphere.d[0], op[i].op.sphere.d[1], op[i].op.sphere.d[3], op[i].op.sphere.r, .1); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); break; case BOX: add_box(tmp,op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); break; case TORUS: add_torus(tmp,op[i].op.torus.d[0], op[i].op.torus.d[1], op[i].op.torus.d[2], op[i].op.torus.r0, op[i].op.torus.r1, .1); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); break; } } }
void my_main( int polygons ) { int i; double step = .05; double xval, yval, zval; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; g = change_color(4); s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case COMMENT: break; case PUSH: push(s); break; case POP: pop(s); break; case MOVE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform - make_scale(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; break; case ROTATE: switch ((int)op[i].op.rotate.axis) { double theta = op[i].op.rotate.degrees; case 0: transform = make_rotX(theta); break; case 1: transform = make_rotY(theta); break; case 2: transform = make_rotZ(theta); break; } matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; break; case BOX: empty_matrix(tmp); xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; double width = op[i].op.box.d1[0]; double height = op[i].op.box.d1[1]; double depth = op[i].op.box.d1[2]; add_box(tmp, xval, yval, zval, width, height, depth); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case SPHERE: empty_matrix(tmp); xval = op[i].op.sphere.d[0]; yval = op[i].op.sphere.d[1]; zval = op[i].op.sphere.d[2]; double radius = op[i].op.sphere.r; add_sphere(tmp, xval, yval, zval, radius, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case TORUS: empty_matrix(tmp); xval = op[i].op.torus.d[0]; yval = op[i].op.torus.d[1]; zval = op[i].op.torus.d[2]; double r1 = op[i].op.torus.r0; double r2 = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, r1, r2, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); break; case LINE: empty_matrix(tmp); add_edge(tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[2], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[2]); draw_lines(tmp, t, g); break; case SAVE: save_extension(t, op[i].op.save.p->name); break; case DISPLAY: display(t); break; } } }
void my_main( int polygons ) { int i; double step = 0.01; double xval, yval, zval; double theta; // rotation angle measure int xyz; // rotation axis double w,h,d; // box dimensions double rad; // sphere/torus radius double rad2; // torus radius 2 struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; g = change_color(6); s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0; i<lastop; i++) { switch (op[i].opcode) { case PUSH: //if (i == 0) { tmp = new_matrix(4,4); ident(tmp); //} push(s); continue; case POP: pop(s); continue; case MOVE: xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; continue; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform = make_scale(xval, yval, zval); matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; continue; case ROTATE: xyz = op[i].op.rotate.axis; theta = op[i].op.rotate.degrees; if (xyz == 0) transform = make_rotX(theta); if (xyz == 1) transform = make_rotY(theta); if (xyz == 2) transform = make_rotZ(theta); matrix_mult(s->data[s->top], transform); s->data[s->top] = transform; continue; case BOX: xval = op[i].op.box.d0[0]; yval = op[i].op.box.d0[1]; zval = op[i].op.box.d0[2]; w = op[i].op.box.d1[0]; h = op[i].op.box.d1[1]; d = op[i].op.box.d1[2]; add_box(tmp, xval, yval, zval, w, h, d); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); continue; case SPHERE: xval = op[i].op.sphere.d[0]; yval = op[i].op.sphere.d[1]; zval = op[i].op.sphere.d[2]; rad = op[i].op.sphere.r; add_sphere(tmp, xval, yval, zval, rad, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); continue; case TORUS: xval = op[i].op.torus.d[0]; yval = op[i].op.torus.d[1]; zval = op[i].op.torus.d[2]; rad = op[i].op.torus.r0; rad2 = op[i].op.torus.r1; add_torus(tmp, xval, yval, zval, rad, rad2, step); matrix_mult(s->data[s->top], tmp); draw_polygons(tmp, t, g); continue; case LINE: xval = op[i].op.line.p0[0]; yval = op[i].op.line.p0[1]; zval = op[i].op.line.p0[2]; w = op[i].op.line.p1[0]; h = op[i].op.line.p1[1]; d = op[i].op.line.p1[2]; add_edge(tmp, xval, yval, zval, w, h, d); matrix_mult(s->data[s->top], tmp); draw_lines(tmp, t, g); continue; case SAVE: save_extension(t, op[i].op.save.p->name); continue; case DISPLAY: display(t); continue; case COMMENT: continue; } } }
/*======== void my_main() ========== Inputs: Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; struct vary_node **knobs; struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; f=0; g.red = 0; g.green = 255; g.blue = 255; first_pass(); knobs = second_pass(); for(f = 0;f<num_frames; f++){ struct vary_node *curr = (struct vary_node *)calloc(1, sizeof(struct vary_node)); curr = knobs[f]; while(curr){ printf("%s : %f\n", curr->name, curr->value); curr = curr->next; } } for(f=0;f<num_frames;f++){ s = new_stack(); tmp = new_matrix(4, 4); transform = new_matrix(4, 4); if(num_frames>1){ vn = knobs[f]; while(vn){ SYMTAB *symb = lookup_symbol(vn->name); set_value(symb, vn->value); vn = vn->next; } } //print_knobs(); for (i=0;i<lastop;i++) { SYMTAB *v; switch (op[i].opcode) { case SPHERE: //printf("Add sphere\n"); 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: //printf("Add Torus\n"); 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: //printf("Add box\n"); 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: //printf("Line\n"); 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: //printf("Move\n"); //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; v = op[i].op.move.p; if(v){ xval = xval * v->s.value; yval = yval * v->s.value; zval = zval * v->s.value; } //printf("x: %f y: %f z: %f\n", xval, yval, zval); transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: //printf("Scale\n"); xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; v = op[i].op.scale.p; if(v){ //printf("I'm not null Scale\n"); xval *= v->s.value; yval *= v->s.value; zval *= v->s.value; } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: //printf("Rotate\n"); xval = op[i].op.rotate.degrees * ( M_PI / 180 ); v = op[i].op.rotate.p; if(v){ xval *= v->s.value; } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: //printf("Push\n"); push( s ); break; case POP: //printf("Pop\n"); pop( s ); break; case SAVE: //printf("Save\n"); save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: //printf("Display\n"); display( t ); break; } } if(num_frames>1){ sprintf (frame_name, "%s%03d.png", name, f); save_extension(t, frame_name); } clear_screen(t); free_stack(s); free_matrix(tmp); } //free_stack( s ); //free_matrix( tmp ); //free_matrix( transform ); }
/*======== 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; 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); for (i = 0; i < lastop; i++) { switch (op[i].opcode) { case SPHERE: add_sphere (tmp, op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult (s->data[s->top], tmp); draw_polygons (tmp, t, g); tmp->lastcol = 0; break; case TORUS: add_torus (tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult (s->data[s->top], tmp); draw_polygons (tmp, t, g); tmp->lastcol = 0; break; case BOX: add_box (tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult (s->data[s->top], tmp); draw_polygons (tmp, t, g); tmp->lastcol = 0; break; case LINE: add_edge (tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines (tmp, t, g); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate (xval, yval, zval); //multiply by the existing origin matrix_mult (s->data[s->top], transform); //put the new matrix on the top copy_matrix (transform, s->data[s->top]); free_matrix (transform); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform = make_scale (xval, yval, zval); matrix_mult (s->data[s->top], transform); //put the new matrix on the top copy_matrix (transform, s->data[s->top]); free_matrix (transform); break; case ROTATE: xval = op[i].op.rotate.degrees * (M_PI / 180); //get the axis if (op[i].op.rotate.axis == 0) transform = make_rotX (xval); else if (op[i].op.rotate.axis == 1) transform = make_rotY (xval); else if (op[i].op.rotate.axis == 2) transform = make_rotZ (xval); matrix_mult (s->data[s->top], transform); //put the new matrix on the top copy_matrix (transform, s->data[s->top]); free_matrix (transform); break; case PUSH: push (s); break; case POP: pop (s); break; case SAVE: save_extension (t, op[i].op.save.p->name); break; case DISPLAY: display (t); break; } } 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 arguments (x0, y0, z0, x1, y1, z1) b: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) h: add a bezier to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) c: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) m: add a sphere to the edge matrix - takes 3 arguments (cx, cy, r) d: add a torus to the edge matrix - takes 4 arguments (cx, cy, r1, r2) p: add a rectangular prism to the edge matrix - takes 6 arguments (x, y, z, width, height, depth) w: clear the current edge matrix - takes 0 arguments i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) 03/08/12 16:22:10 jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; g.red = 0; g.green = 255; g.blue = 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, x3, y3, x4, y4; c = line[0]; switch (c) { case 'l': // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); break; case 'p': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); break; case 'm': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 0.05); //printf( "%lf %lf %lf\n", x, y, z); break; case 'd': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 0.05); //printf( "%lf %lf %lf\n", x, y, z); break; case 'c': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); break; case 'b': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); break; case 'h': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); break; case 's': //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 't': //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 'x': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); matrix_mult(tmp, transform); break; case 'y': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); matrix_mult(tmp, transform); break; case 'z': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); matrix_mult(tmp, transform); break; case 'i': ident(transform); break; case 'a': //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); break; case 'v': /* clear_screen(s); draw_lines(pm, s, g); display(s); break; */ // Second version for triangles: clear_screen(s); draw_polygons(pm, s, g); display(s); break; case 'w': pm->lastcol = 0; break; case 'g': fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); draw_polygons(pm, s, g); save_extension(s, line); break; case 'q': return; case '#': break; default: printf("Invalid command\n"); break; } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
void my_main( int polygons ) { int i; double step; double xval, yval, zval; struct matrix *transform; struct matrix *temp; struct stack *stax; screen t; color g; g.red = 225; g.blue = 0; g.green = 0; stax = new_stack(); temp = new_matrix(4, 1000); transform = new_matrix(4, 4); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case BOX: add_box(temp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult(stax->data[stax->top], temp); draw_polygons(temp, t, g); free_matrix(temp); break; case SPHERE: add_sphere(temp, op[i].op.sphere.d[0], op[i].op.sphere.d[1], op[i].op.sphere.d[2], op[i].op.sphere.r, 10); matrix_mult(stax->data[stax->top], temp); draw_polygons(temp, t, g); free_matrix(temp); break; case TORUS: add_torus(temp, op[i].op.torus.d[0], op[i].op.torus.d[1], op[i].op.torus.d[2], op[i].op.torus.r0,op[i].op.torus.r1, 10); matrix_mult(stax->data[stax->top], temp); draw_polygons(temp, t, g); free_matrix(temp); break; case SCALE: transform = make_scale(op[i].op.scale.d[0], op[i].op.scale.d[1], op[i].op.scale.d[2]); matrix_mult(stax->data[stax->top], transform); copy_matrix(transform, stax->data[stax->top]); free_matrix(transform); break; case MOVE: transform = make_translate(op[i].op.move.d[0], op[i].op.move.d[1], op[i].op.move.d[2]); matrix_mult(stax->data[stax->top], transform); copy_matrix(transform, stax->data[stax->top]); free_matrix(transform); break; case PUSH: push(stax); break; case POP: pop(stax); break; case ROTATE: if (op[i].op.rotate.axis == 0) //case X transform = make_rotX(op[i].op.rotate.degrees * M_PI/180 ); else if (op[lastop].op.rotate.axis == 1) //case Y transform = make_rotY(op[i].op.rotate.degrees * M_PI/180 ); else if (op[lastop].op.rotate.axis == 2) //case Z transform = make_rotZ(op[i].op.rotate.degrees * M_PI/180 ); matrix_mult(stax->data[stax->top], transform); copy_matrix(transform, stax->data[stax->top]); free_matrix(transform); break; case LINE: add_edge(temp, op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2], op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]); matrix_mult(stax->data[stax->top], temp); draw_lines(temp, t, g); free_matrix(temp); break; case SAVE: save_extension(t,op[i].op.save.p->name); break; case DISPLAY: display(t); break; } } free_matrix(temp); free_matrix(transform); free_stack(stax); }
/*======== 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 my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, j; zbuf = (double **)malloc(500 * sizeof(double*)); for(i = 0; i < 500; i++){ zbuf[i] = (double*)malloc(500 * sizeof(double)); for (j = 0; j < 500; j++){ zbuf[i][j] = -DBL_MAX; } } i = 0; j = 0; int f; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; char q; clear_screen( t ); num_frames = 1; step = 0.05; g.red = 0; g.green = 125; g.blue = 255; s = new_stack(); tmp = new_matrix(4, 1000); first_pass(); if (num_frames < 0){ printf("ERROR- can't vary knobs when there's only 1 frame.\n"); } if (num_frames >= 1){ struct vary_node ** klist = second_pass(); for (f = 0; f < num_frames; f++){ for ( i = 0; i < 500; i++){ for (j = 0; j < 500; j++){ zbuf[i][j] = -DBL_MAX; } } for (i=0;i<lastop;i++) { struct vary_node* v = klist[f]; SYMTAB * vvv; while ( v != NULL){ //printf("set %s %lf \n",v->name, v->value ); vvv = lookup_symbol(v->name); if (vvv != NULL){ set_value(vvv, v->value); } else{ add_symbol(v->name,SYM_VALUE,(void *)&(v->value)); } v = v->next; } //print_knobs(); switch (op[i].opcode) { case SETKNOBS: xval = 0; double abcdef = op[i].op.setknobs.value; //printf("Setting knobs to %lf.\n",abcdef); for ( i=0; i < lastsym; i++ ) { if ( symtab[i].type == SYM_VALUE ) { set_value(&(symtab[i]), abcdef); } } break; case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; //printf("MOVE %lf %lf %lf\n",xval,yval,zval); if (op[i].op.move.p != NULL){ SYMTAB* thing = (lookup_symbol(op[i].op.move.p->name)); xval *= thing->s.value; yval *= thing->s.value; zval *= thing->s.value; //printf("new MOVE %lf %lf %lf\n",xval,yval,zval); } transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; //printf("scalE %lf %lf %lf\n",xval,yval,zval); if (op[i].op.scale.p != NULL){ SYMTAB* thing = (lookup_symbol(op[i].op.scale.p->name)); xval *= thing->s.value; yval *= thing->s.value; zval *= thing->s.value; //printf("new scale %lf %lf %lf\n",xval,yval,zval); } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); //printf("rotate %lf\n",xval); if (op[i].op.rotate.p != NULL){ xval *= (lookup_symbol(op[i].op.rotate.p->name))->s.value; //printf("new rotate%lf\n",xval); } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } if (num_frames > 1){ char nopq[256]; char rst[256]; strcpy(nopq,"animations/"); strcat(nopq, name); sprintf (rst, "%03d", f ); strcat(nopq,rst); strcat(nopq,".png"); //printf("Saved frame %d to %s\n", (f+1) ,nopq); save_extension( t, nopq ); clear_screen( t ); screen t; g.red = 0; g.green = 255; g.blue = 255; free_stack( s ); free_matrix( tmp ); s = new_stack(); tmp = new_matrix(4, 1000); } //printf("finished frame %d\n",f); } //////////////////////////////////////////////////////////// for (j = 0; j < num_frames; j++){ struct vary_node * v2 = klist[j]; struct vary_node * v = klist[j]; while (v2 != NULL){ v = v2; v2 = v2->next; free(v); } } free(klist); } //////////////////////////////////////////////// free_stack( s ); free_matrix( tmp ); for(i = 0; i < 500; i++){ free(zbuf[i]); } free(zbuf); //free_matrix( transform ); }
/*======== void my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1) then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j, k, frame, num; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; char fname[100]; char number[4]; screen t; color g; char q; step = 0.05; g.red = 0; g.green = 255; g.blue = 255; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); first_pass(); if(num_frames == -1){ printf("Stop being silly and give num_frames a value please!"); return; } if(num_frames == 0){ for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } } else{ struct vary_node **knobs = second_pass(); for(frame = 0; frame < num_frames; frame++){ clear_screen(t); free_stack(s); s = new_stack(); struct vary_node *curr = (struct vary_node *)malloc(sizeof(struct vary_node *)); curr = knobs[frame]; while(curr){ set_value(lookup_symbol(curr->name), curr->value); curr = curr->next; } free(curr); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SET: set_value(lookup_symbol(op[i].op.set.p->name), op[i].op.set.val); break; case SETKNOBS: for(k=0; k<lastsym; k++){ if(symtab[k].type == SYM_VALUE) symtab[k].s.value = op[i].op.setknobs.value; } break; case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; if (op[i].op.scale.p != NULL){ knob_value = lookup_symbol(op[i].op.scale.p->name)->s.value; xval = xval * knob_value; yval = yval * knob_value; zval = zval * knob_value; } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); if (op[i].op.rotate.p != NULL){ knob_value = lookup_symbol(op[i].op.rotate.p->name)->s.value; xval = xval * knob_value; } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } sprintf(number,"%03d",frame); strcpy(fname,name); strcat(fname,number); strcat(fname,".png"); save_extension(t,fname); printf("Saving %s ...\n",fname); } struct vary_node *current; struct vary_node *previous; int index; for(index = 0; index < num_frames; index++){ current = knobs[index]; previous = knobs[index]; while(current && current->next){ current = current->next; free(previous); previous = current; } free(current); } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); } }
/*======== 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 * STACK = new_stack(); g.red = 0; g.green = 255; g.blue = 0; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { 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); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "box", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); matrix_mult( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } 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( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } 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( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); //matrix_mult(tmp, transform); //print_matrix(transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); } else if ( strncmp(line, "print", strlen(line)) == 0) { print_matrix( STACK->data[ STACK->top ] ); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { display(s); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; //clear_screen(s); //draw_polygons(pm, s, g); save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { pm->lastcol = 0; } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } else if ( strncmp(line, "push", strlen(line)) == 0 ) { push( STACK ); //seg fault printf("Pushed\n"); } else if ( strncmp(line, "pop", strlen(line)) == 0 ) { pop( STACK ); printf("Popped\n"); } else if ( line[0] != '#' ) { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== 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++; } }