void rotate(int pnum, int axis, int direction){ double a[4][4], b[4][4]; D3d_make_identity(a); D3d_make_identity(b); int sign = 1; if(direction == 0){ sign = -1; } D3d_translate(a,b,-x[pnum][numpoints[pnum]+1],-y[pnum][numpoints[pnum]+1],-z[pnum][numpoints[pnum]+1]); if(axis == 0){ D3d_rotate_x(a,b, sign* ROTATE_SPEED); }else if(axis == 1){ D3d_rotate_y(a,b, sign*ROTATE_SPEED); }else if(axis == 2){ D3d_rotate_z(a,b,sign*ROTATE_SPEED); } D3d_translate(a,b,x[pnum][numpoints[pnum]+1],y[pnum][numpoints[pnum]+1],z[pnum][numpoints[pnum]+1]); D3d_mat_mult_points(x[pnum], y[pnum],z[pnum], a, x[pnum], y[pnum],z[pnum], numpoints[pnum]); }
void setup_matrices() { D3d_make_identity(x_ccw); D3d_make_identity(x_cw); D3d_rotate_x(x_ccw, x_cw, M_PI/480); D3d_make_identity(y_ccw); D3d_make_identity(y_cw); D3d_rotate_y(y_ccw, y_cw, M_PI/480); D3d_make_identity(z_ccw); D3d_make_identity(z_cw); D3d_rotate_z(z_ccw, z_cw, M_PI/480); D3d_make_identity(x_plus); D3d_make_identity(x_minus); D3d_translate(x_plus, x_minus, -.1, 0, 0); D3d_make_identity(y_plus); D3d_make_identity(y_minus); D3d_translate(y_plus, y_minus, 0, -.1, 0); D3d_make_identity(z_plus); D3d_make_identity(z_minus); D3d_translate(z_plus, z_minus, 0, 0, .1); D3d_make_identity(all_scale_up); D3d_make_identity(all_scale_down); D3d_scale(all_scale_up, all_scale_down, 1.1, 1.1, 1.1); }
int main (int argc, char **argv) { char q, action; double mat[4][4], minv[4][4], scaleFactor; double radians = 3 * (M_PI / 180); FILE *g; int cc, sign, currentObj, k, h; int increment = 15; int xcounter, ycounter, zcounter = 0; for (cc = 1; cc < argc; cc++) { g = fopen(argv[cc], "r"); //opens a file; r = read only if (g == NULL) { //if the file is empty, it will let me know printf("can't open (1)\n"); exit(1); } else { readobject(g, cc); D3d_make_identity(mat); D3d_make_identity(minv); scaleFactor = scale_n_fit(cc); D3d_translate(mat, minv, -centerx, -centery, -centerz); D3d_scale(mat, minv, scaleFactor, scaleFactor, scaleFactor); D3d_mat_mult_points(x[cc], y[cc], z[cc], mat, x[cc], y[cc], z[cc], points[cc]); // printarray(z[cc], points[cc]); } } welcome(argc - 1); scanf("%c", &q); currentObj = q - '0'; sign = 1 ; action = 't' ; if (currentObj < argc && currentObj > 0) { G_init_graphics(WIDTH, HEIGHT); while (1) { G_rgb(0, 0, 0); G_clear(); drawit(currentObj); D3d_make_identity (mat) ; D3d_make_identity (minv) ; q = G_wait_key() ; if (q == 'q') { exit(0) ; } else if (q == 'c') { sign = -sign ; } else if (q == 't') { action = q ; } else if (q == 's') { reverse = -reverse; } else if (q == 'r') { action = q ; } else if (('0' <= q) && (q <= '9')) { k = q - '0' ; if (h != currentObj) { currentObj = k; } } else if ((q == 'x') && (action == 't')) { D3d_translate (mat, minv, sign * increment, 0, 0); xcounter = xcounter + (sign * increment); } else if ((q == 'y') && (action == 't')) { D3d_translate (mat, minv, 0, sign * increment, 0); ycounter = ycounter + (sign * increment); } else if ((q == 'z') && (action == 't')) { D3d_translate(mat, minv, 0, 0, sign * increment); zcounter = zcounter + (sign * increment); } else if ((q == 'x') && (action == 'r')) { D3d_translate(mat, minv, -xcounter, -ycounter, -zcounter); D3d_rotate_x(mat, minv, sign * radians); D3d_translate(mat, minv, xcounter, ycounter, zcounter); } else if ((q == 'y') && (action == 'r')) { D3d_translate(mat, minv, -xcounter, -ycounter, -zcounter); D3d_rotate_y(mat, minv, sign * radians); D3d_translate(mat, minv, xcounter, ycounter, zcounter); } else if ((q == 'z') && (action == 'r')) { D3d_translate(mat, minv, -xcounter, -ycounter, -zcounter); D3d_rotate_z(mat, minv, sign * radians); D3d_translate(mat, minv, xcounter, ycounter, zcounter); } else { printf("no action\n") ; } D3d_mat_mult_points (x[currentObj], y[currentObj], z[currentObj], mat, x[currentObj], y[currentObj], z[currentObj], points[currentObj] + 1) ; //the numpoints[currentObj]+1 is because we have stored //the center of the object at the arrays' end G_rgb(0, 0, 0); G_clear(); drawit(currentObj); } } }
int main(int argc, char **argv){ char t; int i; int j, k; double xmin, ymin, zmin, xmax, ymax, zmax; lightPos[0] = 100; lightPos[1] = 200; FILE *f; numObjects = argc-1; H = tan(M_PI/4); for(thisObj=0; thisObj < numObjects; thisObj++){ f=fopen(argv[thisObj+1], "r"); if(f==NULL){ //printf("Can't open file"); exit(1); } fscanf(f, "%d", &numPoints[thisObj]); fscanf(f, "%lf %lf %lf", &xp[thisObj][0], &yp[thisObj][0], &zp[thisObj][0]); xmin = xp[thisObj][0]; xmax = xmin; ymin = yp[thisObj][0]; ymax = ymin; zmin = zp[thisObj][0]; zmax = zmin; for(i=1;i<numPoints[thisObj];i++){ fscanf(f, "%lf %lf %lf", &xp[thisObj][i], &yp[thisObj][i], &zp[thisObj][i]); if(xp[thisObj][i] > xmax){ xmax = xp[thisObj][i]; }else if(xp[thisObj][i] < xmin){ xmin = xp[thisObj][i]; } if(yp[thisObj][i] > ymax){ ymax = yp[thisObj][i]; }else if(yp[thisObj][i] < ymin){ ymin = yp[thisObj][i]; } if(zp[thisObj][i] > zmax){ zmax = zp[thisObj][i]; }else if(zp[thisObj][i] < zmin){ zmin = zp[thisObj][i]; } } xc[thisObj] = (xmax + xmin)/2; yc[thisObj] = (ymax + ymin)/2; zc[thisObj] = (zmax + zmin)/2; fscanf(f, "%d", &numPolygons[thisObj]); for(i=0;i<numPolygons[thisObj];i++){ fscanf(f, "%d", &shapes[thisObj][i][0]); for(j=1;j<=shapes[thisObj][i][0];j++){ fscanf(f, "%d", &shapes[thisObj][i][j]); } } objectIsReversed[thisObj] = 0; } // end for thisObj thisObj = 0; G_init_graphics(600,600); redraw(); D3d_make_identity(m); D3d_make_identity(rotx); D3d_make_identity(roty); D3d_make_identity(rotz); D3d_make_identity(scaleup); D3d_make_identity(scaledown); D3d_make_identity(moveleft); D3d_make_identity(moveright); D3d_make_identity(moveup); D3d_make_identity(movedown); D3d_make_identity(push); D3d_make_identity(pull); D3d_make_identity(minv); D3d_rotate_y(roty, minv, 3*M_PI/180); D3d_rotate_z(rotz, minv, 3*M_PI/180); D3d_scale(scaleup, minv, 1.02, 1.02,1.02); D3d_scale(scaledown, minv, .98, .98, .98); D3d_translate(moveleft, minv, -.02, 0, 0); D3d_translate(moveright, minv, .02, 0, 0); D3d_translate(moveup, minv, 0, .02, 0); D3d_translate(movedown, minv, 0, -.02, 0); D3d_translate(push, minv, 0, 0, .02); D3d_translate(pull, minv, 0, 0, -.02); while(1){ t = G_wait_key(); if(t>='0' && t<='9'){ thisObj = t-'0'; redraw(); t = G_wait_key(); } switch(t){ case 'w': D3d_copy_mat(m, moveup); yc[thisObj] += .02; break; case 'a': D3d_copy_mat(m, moveleft); xc[thisObj] -= .02; break; case 's': D3d_copy_mat(m, movedown); yc[thisObj] -= .02; break; case 'd': D3d_copy_mat(m, moveright); xc[thisObj] += .02; break; case 'z': D3d_translate(m, minv, -xc[thisObj], -yc[thisObj], -zc[thisObj]); D3d_rotate_x(m, minv, 3*M_PI/180); D3d_translate(m, minv, xc[thisObj], yc[thisObj], zc[thisObj]); break; case 'x': D3d_translate(m, minv, -xc[thisObj], -yc[thisObj], -zc[thisObj]); D3d_rotate_y(m, minv, 3*M_PI/180); D3d_translate(m, minv, xc[thisObj], yc[thisObj], zc[thisObj]); break; case 'c': D3d_translate(m, minv, -xc[thisObj], -yc[thisObj], -zc[thisObj]); D3d_rotate_z(m, minv, 3*M_PI/180); D3d_translate(m, minv, xc[thisObj], yc[thisObj], zc[thisObj]); break; case 'q': D3d_copy_mat(m, push); zc[thisObj] += .02; break; case 'e': D3d_copy_mat(m, pull); zc[thisObj] -= .02; break; case 'r': D3d_copy_mat(m, scaleup); break; case 't': D3d_copy_mat(m, scaledown); break; case '`': objectIsReversed[thisObj] = 1; break; case 'j': lightPos[0] -= .2; break; case 'l': lightPos[0] += .2; break; case 'i': lightPos[1] += .2; break; case 'k': lightPos[1] -= .2; break; case 'u': lightPos[2] += .2; break; case 'o': lightPos[2] -= .2; break; } //printf("lightx: %lf \n lighty: %lf \n lightz: %lf \n\n", lightPos[0], lightPos[1], lightPos[2]); D3d_mat_mult_points(xp[thisObj], yp[thisObj], zp[thisObj], m, xp[thisObj], yp[thisObj], zp[thisObj], numPoints[thisObj]); D3d_make_identity(m); redraw(); } }