Exemple #1
0
void scaleNfit(int argc){
int i,j;
double scale[4][4] ,iscale[4][4];
for(i=0; i<argc-1;i++){
	double xmax, xmin, ymax, ymin, xrad, yrad, xcenter, ycenter, zmax,zmin,zrad,zcenter;
			xmin = x[i][0];
			xmax = x[i][0];
			ymin = y[i][0];
			ymax = y[i][0];
			zmin = z[i][0];
			zmax = z[i][0];
	for(j = 0; j<numpoints[i];j++){
		if(x[i][j] < xmin){
			xmin = x[i][j];
		}
		if(x[i][j] > xmax){
			xmax = x[i][j];
		}
		if(y[i][j] < ymin){
			ymin = y[i][j];
		}
		if(y[i][j] > ymax){
			ymax = y[i][j];
		}
		if(z[i][j] < zmin){
			zmin = z[i][j];
		}
		if(z[i][j] > zmax){
			zmax = z[i][j];
		}
	}
	xrad = (xmax - xmin)/2;
	yrad = (ymax - ymin)/2;
	zrad = (zmax - zmin)/2;
	x[i][numpoints[i]+1] = xmax - xrad;
	y[i][numpoints[i]+1] = ymax - yrad;
	z[i][numpoints[i]+1] = zmax - zrad;
	double s;
	
	if(xrad > yrad){
		s = xrad;
	}else{
		s = yrad;
	}
	if(zrad > s){
		s = zrad;
	}
	s = (WINDOW_WIDTH/2)/s;
	D3d_make_identity(scale);
	D3d_make_identity(iscale);
	D3d_translate(scale, iscale, -xcenter, -ycenter, -zcenter);
	D3d_mat_mult_points(x[i],y[i],z[i],scale,x[i],y[i],z[i], numpoints[i]);
}
}
Exemple #2
0
int D3d_cs_rotate_z (double a[4][4], double b[4][4], double cs, double sn){
    double rotate[4][4], r2[4][4] ;
  D3d_make_identity(rotate);
  D3d_make_identity(r2);
  rotate[0][0] = cs;
  rotate[0][1] = -sn;
  rotate[1][0] = sn;
  rotate[1][1] = cs;
  D3d_mat_mult(a, rotate, a);
  r2[0][0] = -cs;
  r2[0][1] = sn;
  r2[1][0] = -sn;
  r2[1][1] = -cs;
    D3d_mat_mult(b,b,r2 );
  return 1;
}
Exemple #3
0
int D3d_rotate_z (double a[4][4], double b[4][4], double radians){
  double rotate[4][4], r2[4][4] ;
  D3d_make_identity(rotate);
  D3d_make_identity(r2);
  rotate[0][0] = cos(radians);
  rotate[0][1] = -sin(radians);
  rotate[1][0] = sin(radians);
  rotate[1][1] = cos(radians);
  D3d_mat_mult(a, rotate, a);
  r2[0][0] = cos(-radians);
  r2[0][1] = -sin(-radians);
  r2[1][0] = sin(-radians);
  r2[1][1] = cos(-radians);
    D3d_mat_mult(b,b,r2 );
  return 1;
}//finished
Exemple #4
0
int main(int argc, char const *argv[]) {
    double moveback[4][4];
    double distance;
    for (int i=1; i<argc; i++) {
        FILE* f = fopen(argv[i], "r");
        read_object3d_from_file(f, &objs[i-1]);

        obj = &objs[i-1];
        //move the object backwards so that it doesn't intersect with the view
        distance = max(obj->zs, obj->n)+10;
        printf("initial distance is %f\n", distance);
        D3d_make_identity(moveback);
        D3d_translate(moveback, useless, 0, 0, -distance);
        //scale the object to fit the view
        double max_dim = 10;
        double m = max(obj->xs, obj->n);
        if (m > max_dim) max_dim = m;
        m = max(obj->ys, obj->n);
        if (m > max_dim) max_dim = m;
        printf("max dimension is %f\n", max_dim);
        //D3d_scale(moveback, useless, .1, .1, .1);
        transform_object3d(obj, moveback);
    }
    num_objs = argc-1;
    setup_matrices();


    fov = M_PI/8.0;
    G_init_graphics(600, 600);

    key_handler();
    return 0;
}
Exemple #5
0
int D3d_negate_z (double a[4][4], double b[4][4]){
   double neg[4][4] ;
  D3d_make_identity(neg);
  neg[2][2]=-1;
  D3d_mat_mult(a,neg,a);
  D3d_mat_mult(b,b, neg);
return 1;
}//finished
Exemple #6
0
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]);
}
Exemple #7
0
void D3d_cs_rotate_z (double a[4][4], double b[4][4], double sn, double cs){

  double rotate[4][4];
  double rotate_inverse[4][4];

  D3d_make_identity(rotate);
  D3d_make_identity(rotate_inverse);

  rotate[0][0] = cs;
  rotate[0][1] = -sn;
  rotate[1][0] = sn;
  rotate[1][1] = cs;

  rotate_inverse[0][0] = cs;
  rotate_inverse[0][1] = sn;
  rotate_inverse[1][0] = -sn;
  rotate_inverse[1][1] = cs;

  D3d_mat_mult(a, rotate, a);
  D3d_mat_mult(b, b, rotate_inverse);
}
Exemple #8
0
void D3d_rotate_z (double a[4][4], double b[4][4], double radians){

  double rotate[4][4];
  double rotate_inverse[4][4];

  D3d_make_identity(rotate);
  D3d_make_identity(rotate_inverse);

  rotate[0][0] = cos(radians);
  rotate[0][1] = -sin(radians);
  rotate[1][0] = sin(radians);
  rotate[1][1] = cos(radians);

  rotate_inverse[0][0] = cos(radians);
  rotate_inverse[0][1] = sin(radians);
  rotate_inverse[1][0] = -sin(radians);
  rotate_inverse[1][1] = cos(radians);

  D3d_mat_mult(a, rotate, a);
  D3d_mat_mult(b, b, rotate_inverse);
}
Exemple #9
0
int D3d_scale (double a[4][4], double b[4][4], double sx, double sy, double sz){
  double scale[4][4] ;
  D3d_make_identity(scale);
  scale[0][0] = sx;
  scale[1][1] = sy;
  scale[2][2] = sz;
  D3d_mat_mult(a, scale, a);
  scale[0][0] = 1/sx;
  scale[1][1] = 1/sy;
  scale[2][2] = 1/sz;
  D3d_mat_mult(b, b, scale);
return 1;
}//Done
Exemple #10
0
void D3d_scale (double a[4][4], double b[4][4], double sx, double sy, double sz){

  double scale[4][4];
  double scale_inverse[4][4];

  D3d_make_identity(scale);
  D3d_make_identity(scale_inverse);

  scale[0][0] = sx;
  scale[1][1] = sy;
	scale[2][2] = sz;

  scale_inverse[0][0] = 1/sx;
  scale_inverse[1][1] = 1/sy;
	scale_inverse[2][2] = 1/sz;

// a = scale*a
// b = b*scale_inverse

  D3d_mat_mult(a, scale, a);
  D3d_mat_mult(b, b, scale_inverse);

}
Exemple #11
0
void translate(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;
 	}
 	if(axis == 0){
 		x[pnum][numpoints[pnum]+1] = x[pnum][numpoints[pnum]+1] + (sign * 1);
		D3d_translate(a , b, 1 *sign, 0, 0);
 	}else if(axis == 1){

 	y[pnum][numpoints[pnum]+1] = y[pnum][numpoints[pnum]+1] + (sign * 1);
		D3d_translate(a , b, 0,1*sign ,0);
 	}else if(axis == 2){
 		z[pnum][numpoints[pnum]+1] = z[pnum][numpoints[pnum]+1] + (sign * 1);
 		D3d_translate(a , b, 0,0,1*sign );
 	}
 	
 	
 	D3d_mat_mult_points(x[pnum], y[pnum],z[pnum], a, x[pnum], y[pnum],z[pnum], numpoints[pnum]);
}
Exemple #12
0
void D3d_translate (double a[4][4], double b[4][4], double dx, double dy, double dz){

  double translate[4][4];
  double translate_inverse[4][4];

  D3d_make_identity(translate);
  D3d_make_identity(translate_inverse);

  translate[0][3] = dx;
  translate[1][3] = dy;
	translate[2][3] = dz;

  translate_inverse[0][3] = -dx;
  translate_inverse[1][3] = -dy;
	translate_inverse[2][3] = -dz;

  D3d_mat_mult(a, translate, a);
  D3d_mat_mult(b, b, translate_inverse);

// a = translation*a
// b = b*translation_inverse


}
Exemple #13
0
int D3d_translate (double a[4][4], double b[4][4], double dx, double dy, double dz)
// a = translation*a  
// b = b*translation_inverse  
{
  double t[4][4]; 
  D3d_make_identity(t) ;

  t[0][3] =  dx ;  t[1][3] = dy ;  t[2][3] = dz;
  D3d_mat_mult(a,  t,a) ;

  t[0][3] = -dx ;  t[1][3] = -dy ;t[2][3] = -dz;
  D3d_mat_mult(b,  b,t) ;

  return 1 ;
}//Finished
Exemple #14
0
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);
        }
    }
}
Exemple #15
0
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();

  }

}
Exemple #16
0
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);
}