// **************************************************************************** // Method: RenderTranslucent // // Purpose: Renders translucent geometry within a VisWindow. // Expects that opaque geometry has already been rendered. In the // IceT case, our work is a lot simpler because we don't need to // read the image back from the framebuffer (we'll read it back from // IceT later anyway). // // Programmer: Tom Fogal // Creation: August 4, 2008 // // Modifications: // // **************************************************************************** avtDataObject_p IceTNetworkManager::RenderTranslucent(int windowID, const avtImage_p& input) { VisWindow *viswin = viswinMap.find(windowID)->second.viswin; CallProgressCallback("IceTNetworkManager", "Transparent rendering", 0, 1); { StackTimer second_pass("Second-pass screen capture for SR"); // // We have to disable any gradient background before // rendering, as those will overwrite the first pass // AnnotationAttributes::BackgroundMode bm = viswin->GetBackgroundMode(); viswin->SetBackgroundMode(AnnotationAttributes::Solid); viswin->ScreenRender( this->r_mgmt.viewportedMode, true, // Z buffer false, // opaque geometry true, // translucent geometry input // image to composite with ); // Restore the background mode for next time viswin->SetBackgroundMode(bm); } CallProgressCallback("IceTNetworkManager", "Transparent rendering", 1, 1); // // In this implementation, the user should never use the return value -- // read it back from IceT instead! // return NULL; }
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) { vector<vector<int>> adjacency_list(n); vector<summary> summaries(n); for (size_t e = 0; e < edges.size(); e++) { adjacency_list[edges[e].first].push_back(edges[e].second); adjacency_list[edges[e].second].push_back(edges[e].first); } first_pass(-1, 0, adjacency_list, summaries); second_pass(-1, 0, 0, adjacency_list, summaries); vector<int> result; int min_max = 100000; for (int i = 0; i < n; i++) { min_max = min(min_max, summaries[i].get_max()); } for (int i = 0; i < n; i++) { if (summaries[i].get_max() == min_max) { result.push_back(i); } } return result; }
list *parse_file_to_list(FILE *file) { L1 = create_list(); dictionary *D = create_dict(MAX_LABEL_NUM); /* list *L2 = first_pass(L1, D, file); // 1.parse all instructions, remove junk, keep labels to // a dictionary and save branching instructions to L2 second_pass(L2, D); // 2.parse branching instructions to fill their pointers assert(L1->size == sublist_size(L1->head)); */ /* Much uglier alternative due to static list declarations (It even ignores the list "returned" by the first_pass() function and obscures the control flow): */ L2 = create_list(); first_pass(file, D); second_pass(L2, D); //Print dictionary fprintf(stdout, "\n\nPrinting Labels from the dictionary.\n"); fprintf(stdout, "There are %d entries:\n", D->entries); for(int i = 0; i < D->entries; ++i) fprintf(stdout, "Label: Hash Code %20lu. Next instruction @%d %5s %2d\n", D->e[i].hashcode, D->e[i].goto_node, getCmdStr(D->e[i].goto_node->instr.instr.r.cmd), D->e[i].goto_node->instr.instr.r.dstReg); free(D); fprintf(stderr,"\n\nStarting the issuing of commands:"); return L1; }
int fill_opstruct(t_opc *op, char **args, t_htb *lbl, int pos) { if (op == NULL || args == NULL) return (1); if (!my_strcmp(op->keyword, ".code")) return (compile_raw_code(op, args)); else if (lbl == NULL) return (first_pass(op, args)); else return (second_pass(op, args, lbl, pos)); }
void second_pass(int parent, int node, int parent_cost, vector<vector<int>>& adjacency_list, vector<summary>& summaries) { summaries[node].add(parent, parent_cost); for (size_t i = 0; i < adjacency_list[node].size(); i++) { int neighbor = adjacency_list[node][i]; if (neighbor != parent) { second_pass(node, neighbor, 1 + summaries[node].get_max_but_id(neighbor), adjacency_list, summaries); } } }
void compile (ast root , FILE* out) { if (root == NULL) return; queue fifo = queue_create(); queue lilo = queue_create(); initialize_code_gen(out); get_queue_command(root , fifo); get_queue_command(root , lilo); first_pass(root); second_pass(fifo); ast_to_code_gen(lilo , out); finalize_code_gen(out); queue_destroy(fifo); queue_destroy(lilo); }
/* Function Implementation */ int assembler_compile(FILE* flProgram, symbol_table_arr_t o_arrSymbols, code_section_t *o_pCode, data_section_t *o_pData) { /* fixme: Should this be placed elsewhere? */ static statement_t arrProgramStatements[MAX_STATEMENTS_IN_PROGRAM]; size_t nNumOfStatements = 0; /* First of all, init the given output-bound array types */ memset(o_arrSymbols, 0, sizeof(o_arrSymbols)); memset(o_pCode, 0, sizeof(*o_pCode)); memset(o_pData, 0, sizeof(*o_pData)); /* Make a first pass that completes parsing * of the code, without resolving addresses * yet */ if (first_pass(flProgram, arrProgramStatements, sizeof(arrProgramStatements) / sizeof(arrProgramStatements[0]), &nNumOfStatements, o_arrSymbols, o_pData, o_pCode) != 0) return -1; /* First pass completed, resolve addresses */ if (second_pass(arrProgramStatements, nNumOfStatements, o_arrSymbols, o_pCode) != 0) return -2; return 0; }
/*======== void my_main() ========== Inputs: Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; struct vary_node **knobs; struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; 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 commands in mdl. If frames is not present in the source (and therefore num_frames is 1) then process_knobs should be called. If frames is present, the entire op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; char *q; int k; char *p; p = name; int m; struct vary_node* caller; knob_value = 1; double carry; num_frames = 1; step = 0.05; g.red = 0; g.green = 255; g.blue = 255; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); first_pass(); if(num_frames < 0){ printf("AH...NO, NOT WITHOUT FRAMES CAN YOU VARY.\n"); } struct vary_node ** kn = second_pass(); for(k = 0; k < num_frames; k++){ for(i = 0; i < XRES; i++){ for(j = 0; j < YRES; j++){ zBuffer[i][j] = -2147483647; } } for ( i=0; i < lastsym; i++ ) { if ( symtab[i].type == SYM_VALUE ) { caller = kn[k]; while((caller) && (strcmp(caller->name,symtab[i].name) != 0)){ caller = caller->next; } if(caller){ (&symtab[i])->s.value = caller->value; } } } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SETKNOBS: //carry = op[i].op.setknobs.value; for( i = 0; i < lastsym; i++){ if(symtab[i].type == SYM_VALUE){ set_value(&(symtab[i]), op[i].op.setknobs.value); } } break; case SET: //carry = op[i].op.set.p->s.value; set_value(lookup_symbol(op[i].op.set.p->name), op[i].op.set.p->s.value); break; case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin //pointInfo(op[i].op.sphere.constants->name); matrix_mult( s->data[ s->top ], tmp ); pointInfo(op[i].op.sphere.constants->name); ambi.red = ambiRed; ambi.green = ambiGreen; ambi.blue = ambiBlue; draw_polygons( tmp, t, g ); //printf("%f %f %f\n", ambiRed, ambiGreen, ambiBlue); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); pointInfo(op[i].op.torus.constants->name); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); pointInfo(op[i].op.box.constants->name); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: if(op[i].op.move.p != NULL){ caller = kn[k]; while((caller) && (strcmp(caller->name, op[i].op.move.p->name) != 0)){ caller = caller->next; } if(caller){ knob_value = caller->value; } } //get the factors xval = op[i].op.move.d[0] * knob_value; yval = op[i].op.move.d[1] * knob_value; zval = op[i].op.move.d[2] * knob_value; transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: if(op[i].op.scale.p != NULL){ caller = kn[k]; while((caller) && (strcmp(caller->name, op[i].op.scale.p->name) != 0)){ caller = caller->next; } if(caller){ knob_value = caller->value; } } xval = op[i].op.scale.d[0] * knob_value; yval = op[i].op.scale.d[1] * knob_value; zval = op[i].op.scale.d[2] * knob_value; transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: if(op[i].op.rotate.p != NULL){ caller = kn[k]; while((caller) && (strcmp(caller->name, op[i].op.rotate.p->name) != 0)){ caller = caller->next; } if(caller){ knob_value = caller->value; } } xval = op[i].op.rotate.degrees * ( M_PI / 180 ) * knob_value; //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } strcpy(name, op[refer].op.basename.p->name); char value[256]; char adder[256]; strcpy(value, "hermano/"); strcat(value, name); sprintf(adder, "%04d", k); strcat(value, adder); strcat(value, ".png"); save_extension(t, value); clear_screen(t); s = new_stack(); tmp = new_matrix(4,1000); } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== void 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() ========== 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 red,green,blue; double vx,vy,vz; vx = 0; vy = 0; vz = -1; int i, f, j; double* vector; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct matrix *zbuffer = new_Zmatrix(XRES,YRES); //print_matrix(zbuffer); struct stack *s; screen t; color g; struct light* light=(struct light*)malloc(sizeof(struct light)); light->l[0]=0;light->l[1]=0;light->l[2]=0; light->c[0]=0;light->c[0]=0;light->c[0]=0; light->next=NULL; struct light* cur_light; int ind; struct constants* constants=(struct constants*)malloc(sizeof(struct constants)); constants->red=1.0;constants->blue=1.0;constants->green=1.0; struct vary_node **knobs; struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; g.red = 0; g.green = 0; g.blue = 0; first_pass(); if (num_frames == 1) process_knobs(); else knobs = second_pass(); for (i=0;i<lastop;i++) { switch (op[i].opcode) { case LIGHT: cur_light = light; for (;cur_light->next!=NULL;cur_light=cur_light->next){} cur_light->next=lookup_symbol(op[i].op.light.p->name)->s.l; break; case AMBIENT: g.red += op[i].op.ambient.c[0]; g.green += op[i].op.ambient.c[1]; g.blue += op[i].op.ambient.c[2]; break; } } for ( f=0; f < num_frames; f++ ) { s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); //if there are multiple frames, set the knobs if ( num_frames > 1 ) { vn = knobs[f]; while ( vn ) { printf("knob: %s value:%lf\n", vn->name, vn->value); set_value( lookup_symbol( vn->name ), vn->value ); vn = vn-> next; } } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SET: set_value( lookup_symbol( op[i].op.set.p->name ), op[i].op.set.p->s.value ); break; case SETKNOBS: for ( j=0; j < lastsym; j++ ) if ( symtab[j].type == SYM_VALUE ) symtab[j].s.value = op[i].op.setknobs.value; break; case CONSTANTS: //constants=lookup_symbol(op[i].op.constants.p->name)->s.c; break; /* case LIGHT: cur_light = light; for (;light->next!=NULL;light=light->next){} light->next=lookup_symbol(op[i].op.light.p->name)->s.l; break; */ case CAMERA: vx = op[i].op.camera.eye[0] - op[i].op.camera.aim[0]; vy = op[i].op.camera.eye[1] - op[i].op.camera.aim[1]; vz = op[i].op.camera.eye[2] - op[i].op.camera.aim[2]; vector = normalize(vx,vy,vz); vx = vector[0]; vy = vector[1]; vz = vector[2]; break; case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); Zdraw_polygons( tmp, t, g ,zbuffer, light, constants,vx,vy,vz); tmp->lastcol = 0; //printf("%s\n","drawing sphere"); break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); Zdraw_polygons( tmp, t, g ,zbuffer, light, constants,vx,vy,vz); tmp->lastcol = 0; printf("%s\n","drawing torus"); break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); Zdraw_polygons( tmp, t, g ,zbuffer,light, constants,vx,vy,vz); tmp->lastcol = 0; printf("%s\n","drawing box"); break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); Zdraw_lines( tmp, t, g ,zbuffer); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; //get knob if it exists if ( op[i].op.move.p != NULL ) { knob_value = lookup_symbol( op[i].op.move.p->name )->s.value; xval = xval * knob_value; yval = yval * knob_value; zval = zval * knob_value; } transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; //get knob if it exists if ( op[i].op.scale.p != NULL ) { knob_value = lookup_symbol( op[i].op.scale.p->name )->s.value; xval = xval * knob_value; yval = yval * knob_value; zval = zval * knob_value; } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); //get knob if it exists if ( op[i].op.rotate.p != NULL ) { knob_value = lookup_symbol( op[i].op.rotate.p->name )->s.value; xval = xval * knob_value; } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; /* case AMBIENT: g.red += op[i].op.ambient.c[0]; g.green += op[i].op.ambient.c[1]; g.blue += op[i].op.ambient.c[2]; break; */ case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); //save the image with the correct filename if ( num_frames > 1 ) { printf("Drawing frome: %d\n", f ); sprintf( frame_name, "anim/%s%03d.png", name, f ); save_extension( t, frame_name ); } } //end frame loop }
int dir_foreach( const char* dirname, void (*first_pass)(const char*, int, void*), int (*process)(void*), void (*second_pass)(const char*, int, void*), void* data) { #ifndef WINDOWS DIR* dir; struct dirent* rem, entry; int err; if((dir = opendir(dirname)) == NULL) return 0; if(first_pass) { for(err = readdir_r(dir, &entry, &rem) ; rem != NULL && err == 0 ; err = readdir_r(dir, &entry, &rem)) { // Only list regular filed and directories. if(entry.d_type & DT_REG) first_pass(entry.d_name, 1, data); else if(entry.d_type & DT_DIR) first_pass(entry.d_name, 0, data); } rewinddir(dir); } if(process) process(data); if(second_pass) { for(err = readdir_r(dir, &entry, &rem) ; rem != NULL && err == 0 ; err = readdir_r(dir, &entry, &rem)) { // Only list regular filed and directories. if(entry.d_type & DT_REG) second_pass(entry.d_name, 1, data); else if(entry.d_type & DT_DIR) second_pass(entry.d_name, 0, data); } } closedir(dir); return 1; #else HANDLE h; WIN32_FIND_DATA fd; char buffer[MAX_BUFFER]; // We need to add a * to get all files in the // current directory. m_sprintf( buffer, MAX_BUFFER, "%s*", dirname); if(first_pass) { if((h = FindFirstFileA(buffer, &fd)) == INVALID_HANDLE_VALUE) return 0; do { int is_file = !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); first_pass(fd.cFileName, is_file, data); } while(FindNextFileA(h, &fd)); FindClose(h); } if(process) process(data); // Apparently there's no way to rewind with Windows :( if(second_pass) { if((h = FindFirstFileA(buffer, &fd)) == INVALID_HANDLE_VALUE) return 0; do { int is_file = !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); second_pass(fd.cFileName, is_file, data); } while(FindNextFileA(h, &fd)); FindClose(h); } return 1; #endif }
/*======== void my_main() ========== Inputs: Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; struct vary_node **knobs; struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; g.red = 0; g.green = 255; g.blue = 255; first_pass(); knobs=second_pass(); puts("hi"); /* int swag; for (i=0;i<lastop;i++){ if (op[i].opcode == FRAMES) num_frames = op[i].op.frames.num_frames; } */ //for (swag=0;swag<num_frames;swag++){ for (i=0;i<lastop;i++) { puts("flag"); switch (op[i].opcode) { case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: xval = op[i].op.rotate.degrees * ( M_PI / 180 ); //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } //} free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== 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 <F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3><F3>front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { struct vary_node ** knob_vals; int i, f, j, k; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; char q; struct vary_node * temp_node; step = 0.05; g.red = 0; g.green = 255; g.blue = 255; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); first_pass(); if(num_frames == -1) exit(1); knob_vals= second_pass(); // print_knobs(); for(k = 0; k < num_frames; k++){ //print_knobs(); //printf("%lu %lu\n", knob_vals, knob_vals[0]); //printf("first node: %s %f\n", knob_vals[0]->name, knob_vals[1]->value); for(temp_node = knob_vals[k]; temp_node; temp_node = temp_node->next){ //print_knobs(); set_value(lookup_symbol(temp_node->name), temp_node -> value); } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SPHERE: add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factors knob_value = 1; if(op[i].op.move.p) knob_value = (double)op[i].op.move.p->s.value; xval = op[i].op.move.d[0] * knob_value; yval = op[i].op.move.d[1] * knob_value; zval = op[i].op.move.d[2] * knob_value; transform = make_translate( xval , yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: knob_value = 1; if(op[i].op.scale.p) knob_value = op[i].op.scale.p->s.value; xval = (op[i].op.scale.d[0]) * knob_value; yval = (op[i].op.scale.d[1]) * knob_value; zval = (op[i].op.scale.d[2]) * knob_value; transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: knob_value = 1; if(op[i].op.rotate.p) knob_value = op[i].op.rotate.p->s.value; xval = (op[i].op.rotate.degrees * ( M_PI / 180 )) * knob_value; //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } //strcpy(name, op[].op.basename.p->name); char value[256]; char adder[256]; // strcpy(value, "pic"); // strcat(value, name); strcpy(value, name); sprintf(adder, "%04d", k); //k++; strcat(value, adder); strcat(value, ".png"); save_extension(t, value); //} clear_screen(t); s = new_stack(); } free_stack( s ); free_matrix( tmp ); //free_matrix( transform ); }
/*======== void 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, 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; 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() ========== 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 generate( int polygons ) { double knob_value; vary_node *knobs; vary_node vn; char frame_name[128]; if (first_pass()) { knobs = second_pass(); } else { num_frames = 1; } // Default values char color_type = PNG_RGB, shading = GOROUD; int width = 1000, height = 1000, segments = 20; screen s = make_screen(width, height); vertex_list vlist = new_vlist(100); face_list flist = new_flist(100); edge_list elist = new_elist(100); stack coord_systems = new_stack(5); vector center = new_vector(0, 0, 0); vector eye = new_vector(0, 0, 200); double distance = 200; vertex text0, text1; text0.x = -100; text0.y = 0; text0.z = 0; text1.x = 100; text1.y = 0; text1.z = 0; text0.nx = text1.x - text0.x; text0.ny = text1.y - text0.y; text0.nz = text1.z - text0.z; text1.nx = text0.nx * text0.nx + text0.ny * text0.ny + text0.nz * text0.nz; text0.color = rgb(255, 255, 255); text0.shine = 3; text1.color = rgb(255, 0, 255); text1.shine = 3; uint32_t amb_color = rgb(255, 255, 255), light_color = rgb(0, 255, 255), wire_color = rgb(255, 255, 255); vector light_source = new_vector(0, 100, 200); double amb_ref_constant = 0.1, diff_ref_constant = 1, spec_ref_constant = 1; int frame, i, j; double temp; for (frame = 0; frame < num_frames; frame++) { if (num_frames > 1) { for (vn = knobs[frame]; vn != NULL; vn = vn->next) { set_value(lookup_symbol(vn->name), vn->value); } } for (i = 0; i < lastop; i++) { switch (op[i].opcode) { case TEXTURE: text0.x = op[lastop].op.texture.d0[0]; text0.y = op[lastop].op.texture.d0[1]; text0.z = op[lastop].op.texture.d0[2]; text1.x = op[lastop].op.texture.d1[0]; text1.y = op[lastop].op.texture.d1[1]; text1.z = op[lastop].op.texture.d1[2]; text0.nx = text1.x - text0.x; text0.ny = text1.y - text0.y; text0.nz = text1.z - text0.z; text1.nx = text0.nx * text0.nx + text0.ny * text0.ny + text0.nz * text0.nz; text0.color = rgb( (uint8_t) max(0, min(0xFF, op[lastop].op.texture.d2[0])), (uint8_t) max(0, min(0xFF, op[lastop].op.texture.d2[1])), (uint8_t) max(0, min(0xFF, op[lastop].op.texture.d2[2])) ); text0.shine = op[lastop].op.texture.d2[3]; text1.color = rgb( (uint8_t) max(0, min(0xFF, op[lastop].op.texture.d3[0])), (uint8_t) max(0, min(0xFF, op[lastop].op.texture.d3[1])), (uint8_t) max(0, min(0xFF, op[lastop].op.texture.d3[2])) ); text1.shine = op[lastop].op.texture.d3[3]; break; case CAMERA: eye.x = op[lastop].op.camera.eye[0]; eye.y = op[lastop].op.camera.eye[1]; eye.z = op[lastop].op.camera.eye[2]; center.x = eye.x + op[lastop].op.camera.aim[0]; center.y = eye.y + op[lastop].op.camera.aim[1]; center.z = eye.z + op[lastop].op.camera.aim[2]; break; case FOCAL: distance = op[lastop].op.focal.value; break; case AMBIENT: amb_color = rgb( (uint8_t) max(0, min(0xFF, op[lastop].op.ambient.c[0])), (uint8_t) max(0, min(0xFF, op[lastop].op.ambient.c[1])), (uint8_t) max(0, min(0xFF, op[lastop].op.ambient.c[2])) ); break; case LIGHT: light_color = rgb( (uint8_t) max(0, min(0xFF, op[lastop].op.light.c[0])), (uint8_t) max(0, min(0xFF, op[lastop].op.light.c[1])), (uint8_t) max(0, min(0xFF, op[lastop].op.light.c[2])) ); light_source = new_vector( op[lastop].op.light.l[0], op[lastop].op.light.l[1], op[lastop].op.light.l[2] ); break; case SHADING: if (strcmp(op[i].op.shading.p->name, "GOROUD") == 0) { shading = GOROUD; } else { shading = WIRE; } break; case SPHERE: add_sphere( vlist, elist, flist, op[i].op.sphere.d[0], op[i].op.sphere.d[1], op[i].op.sphere.d[2], op[i].op.sphere.r, segments, text0, text1 ); transform(vlist, peek(coord_systems)); if (shading == GOROUD) { draw_faces( s, vlist, flist, center, eye, distance, amb_color, light_color, light_source, amb_ref_constant, diff_ref_constant, spec_ref_constant ); } else { draw_edges( s, vlist, elist, center, eye, distance, wire_color ); } clear_vlist(vlist); clear_elist(elist); clear_flist(flist); break; case BOX: add_box( vlist, elist, flist, 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], text0, text1 ); transform(vlist, peek(coord_systems)); if (shading == GOROUD) { draw_faces( s, vlist, flist, center, eye, distance, amb_color, light_color, light_source, amb_ref_constant, diff_ref_constant, spec_ref_constant ); } else { draw_edges( s, vlist, elist, center, eye, distance, wire_color ); } clear_vlist(vlist); clear_elist(elist); clear_flist(flist); break; case LINE: add_line( vlist, elist, 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], text0, text1 ); transform(vlist, peek(coord_systems)); draw_edges(s, vlist, elist, center, eye, distance, wire_color); clear_vlist(vlist); clear_elist(elist); break; case MOVE: if (op[i].op.move.p == NULL){ knob_value = 1; } else { knob_value = op[i].op.move.p->s.value; } translate( op[i].op.move.d[0] * knob_value, op[i].op.move.d[1] * knob_value, op[i].op.move.d[2] * knob_value, peek(coord_systems) ); break; case SCALE: if (op[i].op.scale.p == NULL){ knob_value = 1; } else { knob_value = op[i].op.scale.p->s.value; } scale( op[i].op.scale.d[0] * knob_value, op[i].op.scale.d[1] * knob_value, op[i].op.scale.d[2] * knob_value, peek(coord_systems) ); break; case ROTATE: if (op[i].op.rotate.p == NULL){ knob_value = 1; } else { knob_value = op[i].op.rotate.p->s.value; } temp = op[i].op.rotate.axis; if (temp == 0) { rotate_x( op[i].op.rotate.degrees * knob_value, peek(coord_systems) ); } else if (temp == 1) { rotate_y( op[i].op.rotate.degrees * knob_value, peek(coord_systems) ); } else { rotate_z( op[i].op.rotate.degrees * knob_value, peek(coord_systems) ); } break; case SET: set_value( lookup_symbol(op[i].op.set.p->name), op[i].op.set.val ); break; case SETKNOBS: for (j = 0; j < lastsym; j++) { set_value(&symtab[j], op[j].op.setknobs.value); } break; case PUSH: push(coord_systems); break; case POP: pop(coord_systems); break; case SAVE: make_png(op[i].op.save.p->name, s, color_type); break; case DISPLAY: display_png(s, color_type); break; } } if (num_frames > 1){ sprintf(frame_name, "anim/%s%03d.png", name, frame); make_png(frame_name, s, color_type); clear_screen(s); clear_stack(coord_systems); } } free_screen(s); free_vlist(vlist); free_flist(flist); free_elist(elist); free(coord_systems); }
/*======== void my_main() ========== Inputs: Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s = new_stack(); screen t; clear_screen(t); color g; double factor; //struct vary_node **knobs; //struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; int animated = 0; g.red = 0; g.green = 255; g.blue = 255; first_pass(&num_frames, frame_name); if (num_frames!=1) animated=1; struct vary_node ** knobs=second_pass(num_frames); struct vary_node * current; j=0; while(j<num_frames){ tmp=new_matrix(4, 4); struct stack *s=new_stack(); if (animated){ current=knobs[j]; while (current->next){ set_value(lookup_symbol(current->name), current->value); current=current->next; } set_value(lookup_symbol(current->name), current->value); } for (i=0;i<lastop;i++) { switch (op[i].opcode) { case SPHERE: add_sphere( tmp, op[i].op.sphere.d[0], //x op[i].op.sphere.d[1], //y op[i].op.sphere.d[2], //z op[i].op.sphere.r, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: add_torus( tmp, op[i].op.torus.d[0], //x op[i].op.torus.d[1], //y op[i].op.torus.d[2], //z op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); matrix_mult( s->data[ s->top ], tmp ); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //get the factor if (op[i].op.move.p) factor=op[i].op.move.p->s.value; else factor=1; if (factor != 0){ xval = op[i].op.move.d[0]*factor; yval = op[i].op.move.d[1]*factor; zval = op[i].op.move.d[2]*factor; transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); } break; case SCALE: if (op[i].op.scale.p) factor=op[i].op.scale.p->s.value; else factor=1; if (factor!=0){ xval = op[i].op.scale.d[0]*factor; yval = op[i].op.scale.d[1]*factor; zval = op[i].op.scale.d[2]*factor; transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); } break; case ROTATE: if (op[i].op.rotate.p) factor=op[i].op.rotate.p->s.value; else factor=1; if (factor!=0){ xval = op[i].op.rotate.degrees * ( M_PI / 180 )*factor; //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); } break; case PUSH: push( s ); break; case POP: pop( s ); break; case SAVE: save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: display( t ); break; } } if (animated){ print_knobs(); char * name[150]; printf("frame number: %d\n", j); printf("name: %s\n", frame_name); if (num_frames < 10) sprintf(name, "animate/%s%0d.png", frame_name, j); else if (10 < num_frames < 100) sprintf(name, "animate/%s%03d.png", frame_name, j); // else if (100 < num_frames < 1000) //sprintf(name, "animate/%s%04d.png", frame_name, j); printf("Saving %s\n\n", name); save_extension(t, name); clear_screen(t); } free_stack( s ); free_matrix( tmp ); j++; } }
/*======== void my_main() ========== Inputs: int polygons Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1) then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j, 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 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); } } }