示例#1
0
文件: draw.c 项目: akratsios/points3d
/*======== void generate_sphere() ==========
  Inputs:   struct matrix * points
	double cx
	double cy
	double r
	double step  
  Returns: 

  Generates all the points along the surface of a 
  sphere with center (cx, cy) and radius r

  Adds these points to the matrix parameter

  03/22/12 11:30:26
  jdyrlandweaver
  ====================*/
void generate_sphere( struct matrix * points, 
											double cx, double cy, double r, 
											double step ) {
  double i;
  struct matrix * temp;
	/* from notes */
	/*  1     0        0     0   rcos(theta)   x */
	/* 	0 cos(phi) -sin(phi) 0 * rsin(theta) = y */
	/* 	0 sin(phi)  cos(phi) 0       1         z */
	/* 	0     0        0     1       0           */

	
	for (i = 0; i < M_PI / 2; i += step) {
		add_circle(points, cx, cy, r, step);

		temp = make_translate(-1*cx, 0, 0);

		//rotate
		matrix_mult(make_rotX(i), temp);
		matrix_mult(temp, points);

		temp = make_translate(cx, 0, 0);
		matrix_mult(temp, points);
	}
}  
示例#2
0
camera3d_t create_camera3d(scalar dist) {
    camera3d_t camera;
    scalar* data;
    data=calloc(80,sizeof(scalar));
    camera=data;
    camera[POS_DIST]=dist;
    make_translate(data,0,0,0); 	/*init X,Y,Z,T*/
    make_translate(data+PROJECTION,0,0,0); 	 /*init PROJECTION*/
    make_translate(data+VIEW,0,0,0); 	 /*init VIEW*/
    /* init BIAS*/
    data=data+BIAS;
    data[0]=0.5;
    data[1]=0;
    data[2]=0;
    data[3]=0;
    data[4]=0;
    data[5]=0.5;
    data[6]=0;
    data[7]=0;
    data[8]=0;
    data[9]=0;
    data[10]=0.5;
    data[11]=0;
    data[12]=0.5;
    data[13]=0.5;
    data[14]=0.5;
    data[15]=1;
    return camera;
}
示例#3
0
int main() {

  screen s;
  struct matrix *edges;
  struct matrix *transform;
  transform = make_translate(1,2,3);
  printf("Translation Matrix: \n");
  print_matrix(transform);
  printf("\n");
  transform = make_scale(1,2,3);
  printf("Scale Matrix: \n");
  print_matrix(transform);
  printf("\n");
  transform = make_rotX(M_PI);
  printf("RotX Matrix: \n");
  print_matrix(transform);
  printf("\n");
  transform = make_rotY(M_PI);
  printf("RotY Matrix: \n");
  print_matrix(transform);
  printf("\n");
  transform = make_rotZ(M_PI);
  printf("RotZ Matrix: \n");
  print_matrix(transform);
  printf("\n");
 
  edges = new_matrix(4, 4);
  transform = new_matrix(4, 4);
  
  parse_file( "script_c", transform, edges, s );
  
  free_matrix( transform );
  free_matrix( edges );
  return 1;
}  
示例#4
0
文件: draw.c 项目: akratsios/points3d
/*======== void generate_torus() ==========
  Inputs:   struct matrix * points
	double cx
	double cy
	double r
	double step  
  Returns: 

  Generates all the points along the surface of a 
  tarus with center (cx, cy) and radii r1 and r2

  Adds these points to the matrix parameter

  jdyrlandweaver
  ====================*/
void generate_torus( struct matrix * points, 
										 double cx, double cy, double r1, double r2, 
										 double step ) {
  double i;
  struct matrix * temp;
  
  for (i = 0; i < M_PI; i += step) {
    add_circle(points, cx, cy, r2 - r1, step);
    
    temp = make_translate(-1*cx + r1, 0, 0);
    matrix_mult(make_rotZ(i), temp);
    matrix_mult(temp, points);

    temp = make_translate(cx - r1, 0, 0);
    matrix_mult(temp, points);
  }
}
示例#5
0
文件: main.c 项目: young-k/Notes
int main() {

  screen s;
  color c;

  /* Setup color and screen */

  clear_screen(s);
  
  c.red = 255;
  c.green = 150;
  c.blue = 100;

  /* Setup matrices */
  
  struct matrix *edges;
  struct matrix *transform;

  edges = new_matrix(4, 1);

  /* Finally Testing */
  
  add_edge(edges, 80, 80, 0, 80, 120, 0);
  add_edge(edges, 80, 120, 0, 120, 120, 0);
  add_edge(edges, 120, 120, 0, 120, 80, 0);
  add_edge(edges, 120, 80, 0, 80, 80, 0);

  add_edge(edges, 60, 80, 0, 80, 120, 0);
  add_edge(edges, 80, 120, 0, 100, 100, 0);
  add_edge(edges, 100, 100, 0, 80, 60, 0);
  add_edge(edges, 80, 60, 0, 60, 80, 0);

  add_edge(edges, 90, 90, 0, 110, 90, 0);
  add_edge(edges, 110, 90, 0, 110, 110, 0);
  add_edge(edges, 110, 110, 0, 90, 110, 0);
  add_edge(edges, 90, 110, 0, 90, 90, 0);
  
  draw_lines(edges, s, c);

  int i;
  for (i = 0; i < 40; i+=2) {
    transform = make_translate(i, i, i);
    matrix_mult(transform, edges);
    draw_lines(edges, s, c);
  }  
  
  display(s);
  save_extension(s, "matrix.png");

  /* Free Matrices */
  
  free_matrix( transform );
  free_matrix( edges );

  return 0;
}  
示例#6
0
文件: draw.c 项目: kmejia/3-Graphics
/*======== void add_circle() ==========
  Inputs: struct matrix * points
  double cx
  double cy
  double y
  double step
  Returns:
  03/16/12 19:53:52
  jdyrlandweaver
  ====================*/
void add_circle( struct matrix * points,
		 double cx, double cy,
		 double r, double step ) {
  struct matrix *center = make_translate(cx,cy,0);
  //at the end we translate the figure from the center
  //assume that matrix is blank
  double rad,x,y;
  for (rad = 0;rad<(2*M_PI);rad = rad+step){
    x = r*cos(rad);
    y = r*sin(rad);
    add_point(points,x,y,0);
  }
  //loop is finished, move it so the center is correct
  matrix_mult(center ,points);//now points is changed
  
}
示例#7
0
int main() {

  screen s;
  struct matrix *edges;
  struct matrix *transform;

  color c;

  edges = new_matrix(4, 4);
  transform = new_matrix(4, 4);

  // TEST: Translation matrix
  struct matrix *transTest;
  transTest = make_translate(2,1,3.5);
  print_matrix(transTest);

  //TEST: Scale matrix
  struct matrix *scaleTest;
  scaleTest = make_scale(3,8,5);
  print_matrix(scaleTest);

  //TEST: Rotation about x-axis
  struct matrix *rotateX;
  rotateX = make_rotX(30);
  print_matrix(rotateX);

  //TEST: Rotation about y-axis
  struct matrix *rotateY;
  rotateY = make_rotY(60);
  print_matrix(rotateY);

  //TEST: Rotation about z-axis
  struct matrix *rotateZ;
  rotateZ = make_rotZ(45);
  print_matrix(rotateZ);  

  //add_edge(transform,3,3,3,2,2,2);

  /*
  c.red = 170;
  c.green = 240;
  c.blue = 30;
  */

  c.red = 50;
  c.green = 120;
  c.blue = 240;

  int i,j;
  for(i = 0; i < XRES; i++){
    for(j = 0; j < YRES; j++){
      plot(s,c,i,j);
    }
  }

  parse_file( "script_c", transform, edges, s );

  print_matrix(edges);
  print_matrix(transform);

  free_matrix( transform );
  free_matrix( edges );
}  
示例#8
0
文件: my_main.c 项目: stuydw/mdl
void my_main( int polygons ) {

    int i;
    double step = 0.01;
    double xval, yval, zval;
    double theta; // rotation angle measure
    int xyz; // rotation axis
    double w,h,d; // box dimensions
    double rad; // sphere/torus radius
    double rad2; // torus radius 2
    struct matrix *transform;
    struct matrix *tmp;
    struct stack *s;
    screen t;
    color g;

    g = change_color(6);

    s = new_stack();
    tmp = new_matrix(4, 1000);
    clear_screen( t );

    for (i=0; i<lastop; i++) {
        switch (op[i].opcode) {
        case PUSH:
            //if (i == 0) {
            tmp = new_matrix(4,4);
            ident(tmp);
            //}
            push(s);
            continue;
        case POP:
            pop(s);
            continue;
        case MOVE:
            xval = op[i].op.move.d[0];
            yval = op[i].op.move.d[1];
            zval = op[i].op.move.d[2];
            transform = make_translate(xval, yval, zval);
            matrix_mult(s->data[s->top], transform);
            s->data[s->top] = transform;
            continue;
        case SCALE:
            xval = op[i].op.scale.d[0];
            yval = op[i].op.scale.d[1];
            zval = op[i].op.scale.d[2];
            transform = make_scale(xval, yval, zval);
            matrix_mult(s->data[s->top], transform);
            s->data[s->top] = transform;
            continue;
        case ROTATE:
            xyz = op[i].op.rotate.axis;
            theta = op[i].op.rotate.degrees;
            if (xyz == 0)
                transform = make_rotX(theta);
            if (xyz == 1)
                transform = make_rotY(theta);
            if (xyz == 2)
                transform = make_rotZ(theta);
            matrix_mult(s->data[s->top], transform);
            s->data[s->top] = transform;
            continue;
        case BOX:
            xval = op[i].op.box.d0[0];
            yval = op[i].op.box.d0[1];
            zval = op[i].op.box.d0[2];
            w = op[i].op.box.d1[0];
            h = op[i].op.box.d1[1];
            d = op[i].op.box.d1[2];
            add_box(tmp, xval, yval, zval, w, h, d);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            continue;
        case SPHERE:
            xval = op[i].op.sphere.d[0];
            yval = op[i].op.sphere.d[1];
            zval = op[i].op.sphere.d[2];
            rad = op[i].op.sphere.r;
            add_sphere(tmp, xval, yval, zval, rad, step);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            continue;
        case TORUS:
            xval = op[i].op.torus.d[0];
            yval = op[i].op.torus.d[1];
            zval = op[i].op.torus.d[2];
            rad = op[i].op.torus.r0;
            rad2 = op[i].op.torus.r1;
            add_torus(tmp, xval, yval, zval, rad, rad2, step);
            matrix_mult(s->data[s->top], tmp);
            draw_polygons(tmp, t, g);
            continue;
        case LINE:
            xval = op[i].op.line.p0[0];
            yval = op[i].op.line.p0[1];
            zval = op[i].op.line.p0[2];
            w = op[i].op.line.p1[0];
            h = op[i].op.line.p1[1];
            d = op[i].op.line.p1[2];
            add_edge(tmp, xval, yval, zval, w, h, d);
            matrix_mult(s->data[s->top], tmp);
            draw_lines(tmp, t, g);
            continue;
        case SAVE:
            save_extension(t, op[i].op.save.p->name);
            continue;
        case DISPLAY:
            display(t);
            continue;
        case COMMENT:
            continue;
        }
    }
}
示例#9
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f = fopen(filename,"r"); 

  //if there's no such file name, print message and exit
  if(f == NULL)
    {
      printf("You're giving me nothin', boy.");
      exit(1);
    }
  char *boof = (char*)malloc(50*sizeof(char)); //standard buffer to read from
  double *pts = (double*)malloc(6*sizeof(double)); //incase arguments have been indicated that are individual
                 //points
  color c; //felt i had to make a color
  c.red = MAX_COLOR;
  c.green = MAX_COLOR;
  c.blue = MAX_COLOR;
  
  
  while( fgets(boof,50,f) )
    {
      if( boof[0] == 'l') //add a line to edge matrix
	{ 
	  get_args(f,pts);
	  add_edge(pm,pts[0],pts[1],pts[2],pts[3],pts[4],pts[5]);
	}
      else if( boof[0] == 'i' ) //create an identity matrix
	{
	  ident(transform);
	}
      else if( boof[0] == 's' ) //scale matrix
	{
	  get_args(f,pts);
	 struct matrix *m = make_scale(pts[0],pts[1],pts[2]);
	 matrix_mult(m,transform);
	 free_matrix(m);
	 
	}
      else if( boof[0] == 't' ) //translate matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_translate(pts[0],pts[1],pts[2]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	}
       else if( boof[0] == 'x' ) //rotation about x matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_rotX(pts[0]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	} 
       else if( boof[0] == 'y' ) //rotation about y matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_rotY(pts[0]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	} 
       else if( boof[0] == 'z' ) //rotation about z matrix
	{
	  get_args(f,pts);
	  struct matrix *m = make_rotZ(pts[0]);
	  matrix_mult(m,transform);
	  free_matrix(m);
	}
       else if( boof[0] == 'a' )
	 {
	   matrix_mult(transform,pm);
	 }
       else if( boof[0] == 'v')
	 {
	   clear_screen(s);
	   draw_lines(pm,s,c);
	   display(s);
	 }
       else if( boof[0] == 'g')
	 {
	   draw_lines(pm,s,c);
	   fgets(boof,50,f);
	   save_extension(s,boof);
	 }
       else if( boof[0] == 'q')
	 {
	   printf("Now exiting ...");
	   exit(1);
	 }
       else
	 {
	   printf("What the hell does %c mean?\n",boof[0]);
	   return;
	 }
    }
  }
示例#10
0
/*
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
        //   l: add a line to the edge matrix - 
	    takes 6 arguments (x0, y0, z0, x1, y1, z1)
	//   i: set the transform matrix to the identity matrix - 
	//   s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	//   t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	//    x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	//    y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	//    z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	//   a: apply the current transformation matrix to the 
	    edge matrix
	//   v: draw the lines of the edge matrix to the screen
	    display the screen
	//   g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	//    q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian measure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant///////////////////////did this in matrix.c
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

int fd = open(filename, O_RDONLY, 0666);
FILE * f;
f = fopen(filename,"r");
printf("Opened %s\n", filename);
int ln = 1;
int ran = 0;
char input[128];
char dummy[128];
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
color c;
int iiiii,jjjjj;
  for( iiiii=0; iiiii<XRES; iiiii++)
    for ( jjjjj=0; jjjjj<YRES; jjjjj++) {

      c.red = 0;//random() % (MAX_COLOR + 1);
      c.green = 0;//random() % (MAX_COLOR + 1);
      c.blue = 0;//random() % (MAX_COLOR + 1);

      plot( s, c, iiiii, jjjjj);
    }
c.red = 0;
c.green = 255;
c.blue = 0;
long end = lseek(fd,0,SEEK_END) - 1;
lseek(fd,0,SEEK_SET);
while( (lseek(fd,0, SEEK_CUR) < end) && (strcmp(input, "q") != 0)){
ran = 0;


//printf("Running %s\n", input);


if (ran == 0 && strcmp(input, "v") == 0){///////////////////////////////////v
ran = 1;
draw_lines(pm,s,c);
display(s);
//print_matrix(pm);
//print_matrix(transform);
}

if (ran == 0 && strcmp(input, "g") == 0){///////////////////////////////////g
ran = 1;
draw_lines(pm,s,c);
display(s);
ln++;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
if(input[strlen(input) - 1] == '\n'){
input[strlen(input) - 1] = '\0';
}
save_extension(s, input);
printf("Saved to %s\n",input);
}

if (ran == 0 && strcmp(input, "l") == 0){////////////////////////////////////////////////////////////////l
ran = 1;
ln++;
double pts[6];
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
int neg = 0;
double pow = 1;
double num = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 6; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
pts[p++] = num;
}
}

if (p == 6 ){
//printf("Added (%lf,%lf,%lf) to (%lf,%lf,%lf).\n",pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
add_edge(pm,pts[0], pts[1], pts[2], pts[3], pts[4], pts[5] );
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "i") == 0){///////////////////////////////////i
ran = 1;
ident(transform);
}




if (ran == 0 && strcmp(input, "a") == 0){///////////////////////////////////a
ran = 1;
matrix_mult(transform, pm);
}




if (ran == 0 && strcmp(input, "s") == 0){///////////////////////////////////s
ran = 1;
ln++;
double factors[3];
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 3; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
factors[p++] = num;
}
}
if (p == 3 ){
//printf("Scaled by %lf,%lf,and %lf.\n",factors[0], factors[1], factors[2]);
struct matrix* scm = make_scale(factors[0], factors[1], factors[2]);
matrix_mult(scm,transform);
free_matrix(scm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}



if (ran == 0 && strcmp(input, "t") == 0){///////////////////////////////////t
ran = 1;
ln++;
double factors[3];
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 3; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
factors[p++] = num;
}
}
if (p == 3 ){
//printf("Moved by %lf,%lf,and %lf.\n",factors[0], factors[1], factors[2]);
struct matrix* trm = make_translate(factors[0], factors[1], factors[2]);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "x") == 0){///////////////////////////////////x
ran = 1;
ln++;
double theta;
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 1; i++){
len = 0;
neg = 0;;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
theta = num;
p++;
}
}
if (p == 1 ){
//printf("Rotated by %lf degrees.\n",theta);
struct matrix* trm = make_rotX(theta);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "y") == 0){///////////////////////////////////y
ran = 1;
ln++;
double theta;
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 1; i++){
len = 0;
neg = 0;
dloc = -1;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len - 1){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
theta = num;
p++;
}
}
if (p == 1 ){
//printf("Rotated by %lf degrees.\n",theta);
struct matrix* trm = make_rotY(theta);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0 && strcmp(input, "z") == 0){///////////////////////////////////z
ran = 1;
ln++;
double theta;
double digits[17];
int  dloc = 0;
int p = 0;
int i = 0;
int j = 0;
int len = 0;
double pow = 1;
double num = 0;
int neg = 0;
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
for ( i = 0; i < strlen(input) && p < 1; i++){
len = 0;
dloc = -1;
neg = 0;
while(len < 17 &&  (i < strlen(input)) && ((input[i] <= 57 && input[i] >= 48)||( input[i] == 46) ||( input[i] == 45)) ){
if (input[i] == 45){
neg = 1;
}
else{
digits[len++] = input[i] -48;
if (input[i] == 46){
dloc = len - 1;
}
}
i++;
}
num = 0; 
pow = 1;
int d2 = dloc;
if (dloc > -1){
while (dloc < len){
pow = pow / 10;
dloc++;
}
}
for ( j = 0; j < len; j++){
if (d2 != len - 1 - j){
num += digits[len - 1 - j] * pow;
pow *= 10;
}
}
if (len > 0){
if (neg == 1){
num *= -1;
}
theta = num;
p++;
}
}
if (p == 1 ){
//printf("Rotated by %lf degrees.\n",theta);
struct matrix* trm = make_rotZ(theta);
matrix_mult(trm,transform);
free_matrix(trm);
}
else{
printf("Error in file- line number %d: %s\n", ln, input);
}
}




if (ran == 0){///////////////////////////////////////////////////////////////////////ERROR
printf("Error in file- line number %d: %s\n", ln, input);
}
fgets(input, sizeof(char)*128, f);
read(fd, dummy, strlen(input) * sizeof(char));
input[strlen(input) - 1] = '\0';
ln++;
}
close(fd);
}
示例#11
0
文件: parser.c 项目: stuydw/3d
/*======== void parse_file () ==========
Inputs: char * filename
struct matrix * transform,
struct matrix * pm,
screen s
Returns:

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
Every command is a single character that takes up a line
Any command that requires arguments must have those arguments in the second line.
The commands are as follows:
l: add a line to the edge matrix -
takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
i: set the transform matrix to the identity matrix -
s: create a scale matrix,
then multiply the transform matrix by the scale matrix -
takes 3 arguments (sx, sy, sz)
t: create a translation matrix,
then multiply the transform matrix by the translation matrix -
takes 3 arguments (tx, ty, tz)
x: create an x-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
y: create an y-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
z: create an z-axis rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 1 argument (theta)
a: apply the current transformation matrix to the
edge matrix
v: draw the lines of the edge matrix to the screen
display the screen
g: draw the lines of the edge matrix to the screen
save the screen to a file -
takes 1 argument (file name)
q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename,
                  struct matrix * transform,
                  struct matrix * pm,
                  screen s) {
  char temp;
  float a,b,c,d,e,f,g,h;
  int i,j;
  char * ImgName;
  FILE * file = fopen(filename, "r");
  while(fscanf(file,"%c", &temp) ==1){
    //add torus
    if(temp == 'd'){
      fscanf(file,"%c",&temp);
    if(fscanf(file,"%f %f %f %f",&a,&b,&c,&d) == 4)
      add_torus(pm,a,b,c,d,0.01);
    else
      printf("somethings wrong that need to be fix in d");
    }
    //add sphere
    if(temp == 'm'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f", &a,&b,&c) == 3)
	add_sphere(pm,a,b,c,0.01);
      else
	printf("somethings wrong that need to be fix in m");
    }
    //add box point
    if(temp == 'p'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f) ==6)
	add_box(pm,a,b,c,d,e,f);
      else
	printf("somethings wrong that need to be fix in p\n");
    }
    //add edge
    if(temp == 'l'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f) == 6)
	add_edge(pm,a,b,c,d,e,f);
      else
	printf("SomeThings wrong that need to be fix in l\n");
    }
    //add circle
    if(temp == 'c'){
      fscanf(file,"%c", &temp);
      if(fscanf(file,"%f %f %f",&a,&b,&c) == 3)
	add_circle(pm,a,b,c,M_PI/20);
      else
	printf("fix in c\n");
    }
    //add hermite
    if(temp == 'h'){
      fscanf(file,"%c", &temp);
      if(fscanf(file,"%f %f %f %f %f %f %f %f", &a, &b,&c,&d,&e,&f,&g,&h) == 8)
	add_curve(pm,a,b,c,d,e,f,g,h,0.001,0);
      else
	printf("fix in h\n");
    }
    //add bezier
    if(temp == 'b'){
      fscanf(file,"%c", &temp);
      if(fscanf(file, "%f %f %f %f %f %f %f %f", &a, &b,&c,&d,&e,&f,&g,&h) == 8)
	add_curve(pm,a,b,c,d,e,f,g,h,0.001,1);
      else
	printf("fix in b\n");
    }
    //ident transform
    else if(temp == 'i'){
      ident(transform);
    }
    //scaling
    else if(temp == 's'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f", &a,&b,&c) == 3){
	struct matrix * scalar = make_scale(a,b,c);
	matrix_mult(scalar,transform);
      }
      else
	printf("Somethings wrong that need to be fix in s\n");
    }
    //Translating
    else if (temp == 't'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f %f %f", &d,&e,&f) == 3){
	struct matrix * tranlator = make_translate(d,e,f);
	matrix_mult(tranlator, transform);
      }
      else
	printf("somethings wrong that need to be fix in t\n");
    }
    //Rotating with x
    else if(temp == 'x'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f",&a) ==1){
	struct matrix * rotX = make_rotX(a);
	matrix_mult(rotX,transform);
      }
      else{
	printf("something wrong that need to be fix in x\n");
      }
    }
    //Rotating with Y
    else if(temp == 'y'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f",&b) ==1){
	struct matrix * rotY = make_rotY(b);
	matrix_mult(rotY,transform);
      }
      else{
	printf("something wrong that need to be fix in y\n");
      }
    }
    //Rotating with Z
    else if(temp == 'z'){
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%f",&c) ==1){
	struct matrix * rotZ = make_rotZ(a);
	matrix_mult(rotZ,transform);
      }
      else{
	printf("something wrong that need to be fix in z\n");
      }
    }
    //Applying Transformation
    else if(temp == 'a'){
      matrix_mult(transform,pm);
    }
    else if(temp == 'v'){
      color c;
      c.red = 122;
      c.green = 218;
      c.blue = 225;
      
      clear_screen(s);
      for( i=0; i<XRES; i++){
	for ( j=0; j<YRES; j++){
	  
	  //c.red = random() % (MAX_COLOR + 1);
	  //c.green = random() % (MAX_COLOR + 1);
	  //c.blue = random() % (MAX_COLOR + 1);
	  plot( s, c, i, j);
	}
      }
      c.green = 40;
      draw_lines(pm,s,c);
      display(s);
    }
    else if(temp == 'g'){
      color c;
      c.red = 122;
      c.green = 218;
      c.blue = 225;
      
      clear_screen(s);
      for( i=0; i<XRES; i++){
	for ( j=0; j<YRES; j++){
	  
	  //c.red = random() % (MAX_COLOR + 1);
	  //c.green = random() % (MAX_COLOR + 1);
	  //c.blue = random() % (MAX_COLOR + 1);
	  plot( s, c, i, j);
	}
      }
      c.green = 40;
      draw_lines(pm,s,c);
      ImgName = (char *)malloc(256);
      
      fscanf(file,"%c",&temp);
      if(fscanf(file,"%s",ImgName) ==1){
	ImgName = strsep(&ImgName, ".");
	save_ppm(s,ImgName);
	ImgName = strncat(ImgName,".png",4);
	save_extension(s,ImgName);
	free(ImgName);
      }
      else
	printf("Something wrong that need to be fix in g\n");
    }
    else if (temp == 'q'){
      return;
    }
    }
    }
示例#12
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 );
}
示例#13
0
/*======== void my_main() ==========
  Inputs:   int polygons  
  Returns: 

  This is the main engine of the interpreter, it should
  handle most of the commadns in mdl.

  If frames is not present in the source (and therefore 
  num_frames is 1, then process_knobs should be called.

  If frames is present, the enitre op array must be
  applied frames time. At the end of each frame iteration
  save the current screen to a file named the
  provided basename plus a numeric string such that the
  files will be listed in order, then clear the screen and
  reset any other data structures that need it.

  Important note: you cannot just name your files in 
  regular sequence, like pic0, pic1, pic2, pic3... if that
  is done, then pic1, pic10, pic11... will come before pic2
  and so on. In order to keep things clear, add leading 0s
  to the numeric portion of the name. If you use sprintf, 
  you can use "%0xd" for this purpose. It will add at most
  x 0s in front of a number, if needed, so if used correctly,
  and x = 4, you would get numbers like 0001, 0002, 0011,
  0487

  05/17/12 09:41:35
  jdyrlandweaver
  ====================*/
void
my_main (int polygons)
{

  int i, f, j;
  double step;
  double xval, yval, zval, knob_value;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  char q;

  num_frames = 1;
  step = 0.05;

  g.red = 0;
  g.green = 255;
  g.blue = 255;

  s = new_stack ();
  tmp = new_matrix (4, 1000);
  clear_screen (t);

  for (i = 0; i < lastop; i++)
    {

      switch (op[i].opcode)
	{

	case SPHERE:
	  add_sphere (tmp, op[i].op.sphere.d[0],	//cx
		      op[i].op.sphere.d[1],	//cy
		      op[i].op.sphere.d[2],	//cz
		      op[i].op.sphere.r, step);
	  //apply the current top origin
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case TORUS:
	  add_torus (tmp, op[i].op.torus.d[0],	//cx
		     op[i].op.torus.d[1],	//cy
		     op[i].op.torus.d[2],	//cz
		     op[i].op.torus.r0, op[i].op.torus.r1, step);
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case BOX:
	  add_box (tmp, op[i].op.box.d0[0],
		   op[i].op.box.d0[1],
		   op[i].op.box.d0[2],
		   op[i].op.box.d1[0],
		   op[i].op.box.d1[1], op[i].op.box.d1[2]);
	  matrix_mult (s->data[s->top], tmp);
	  draw_polygons (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case LINE:
	  add_edge (tmp, op[i].op.line.p0[0],
		    op[i].op.line.p0[1],
		    op[i].op.line.p0[1],
		    op[i].op.line.p1[0],
		    op[i].op.line.p1[1], op[i].op.line.p1[1]);
	  draw_lines (tmp, t, g);
	  tmp->lastcol = 0;
	  break;

	case MOVE:
	  //get the factors
	  xval = op[i].op.move.d[0];
	  yval = op[i].op.move.d[1];
	  zval = op[i].op.move.d[2];

	  transform = make_translate (xval, yval, zval);
	  //multiply by the existing origin
	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case SCALE:
	  xval = op[i].op.scale.d[0];
	  yval = op[i].op.scale.d[1];
	  zval = op[i].op.scale.d[2];

	  transform = make_scale (xval, yval, zval);
	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case ROTATE:
	  xval = op[i].op.rotate.degrees * (M_PI / 180);

	  //get the axis
	  if (op[i].op.rotate.axis == 0)
	    transform = make_rotX (xval);
	  else if (op[i].op.rotate.axis == 1)
	    transform = make_rotY (xval);
	  else if (op[i].op.rotate.axis == 2)
	    transform = make_rotZ (xval);

	  matrix_mult (s->data[s->top], transform);
	  //put the new matrix on the top
	  copy_matrix (transform, s->data[s->top]);
	  free_matrix (transform);
	  break;

	case PUSH:
	  push (s);
	  break;
	case POP:
	  pop (s);
	  break;
	case SAVE:
	  save_extension (t, op[i].op.save.p->name);
	  break;
	case DISPLAY:
	  display (t);
	  break;
	}
    }

  free_stack (s);
  free_matrix (tmp);
  //free_matrix( transform );    
}
示例#14
0
文件: my_main.c 项目: stuydw/mdl
void my_main( int polygons ) {

  int i, axis;
  double xval, xval1, yval, yval1, zval, zval1, degrees, width, height, depth, r, r1;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  
  g = change_color(0);

  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
    case PUSH:
      push(s);
      break;
    case POP:
      pop(s);
      break;
    case MOVE:
      xval = op[i].op.move.d[0];
      yval = op[i].op.move.d[1];
      zval = op[i].op.move.d[2];
      transform = make_translate(xval, yval, zval);
      matrix_mult(transform, s->data[s->top]);
      break;
    case SCALE:
      xval = op[i].op.scale.d[0];
      yval = op[i].op.scale.d[1];
      zval = op[i].op.scale.d[2];
      transform = make_scale(xval, yval, zval);
      matrix_mult(transform, s->data[s->top]);
      break;
    case ROTATE:
      axis = op[i].op.rotate.axis;
      degrees = op[i].op.rotate.degrees;
      if(axis == 0)
	transform = make_rotX(degrees);
      else if(axis == 1)
	transform = make_rotY(degrees);
      else if(axis == 2)
	transform = make_rotZ(degrees);
      matrix_mult(transform, s->data[s->top]);
      break;
    case BOX:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.box.d0[0];
      yval = op[i].op.box.d0[1];
      zval = op[i].op.box.d0[2];
      width = op[i].op.box.d1[0];
      height = op[i].op.box.d1[1];
      depth = op[i].op.box.d1[2];
      add_box(tmp, xval, yval, zval, width, height, depth);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;
    case SPHERE:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.sphere.d[0];
      yval = op[i].op.sphere.d[1];
      zval = op[i].op.sphere.d[2];
      r = op[i].op.sphere.r;
      add_sphere(tmp, xval, yval, zval, r, 0.05);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;
    case TORUS:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.torus.d[0];
      yval = op[i].op.torus.d[1];
      zval = op[i].op.torus.d[2];
      r = op[i].op.torus.r0;
      r1 = op[i].op.torus.r1;
      add_torus(tmp, xval, yval, zval, r, r1, 0.05);
      matrix_mult(s->data[s->top], tmp);
      draw_polygons(tmp, t, g);
      break;
    case LINE:
      free_matrix(tmp);
      tmp = new_matrix(4, 1000);
      xval = op[i].op.line.p0[0];
      yval = op[i].op.line.p0[1];
      zval = op[i].op.line.p0[2];
      xval1 = op[i].op.line.p1[0];
      yval1 = op[i].op.line.p1[1];
      zval1 = op[i].op.line.p1[2];
      add_edge(tmp, xval, yval, zval, xval1, yval1, zval1);
      matrix_mult(s->data[s->top], tmp);
      draw_lines(tmp, t, g);
      break;
    case SAVE:
      save_extension(t, op[i].op.save.p->name);
      break;
    case DISPLAY:
      display(t);
      break;
    }
  }
  free_stack(s);
  free_matrix(tmp);
}
示例#15
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         sphere: add a sphere to the edge matrix - 
	    takes 3 arguemnts (cx, cy, r)
         torus: add a torus to the edge matrix - 
	    takes 4 arguemnts (cx, cy, r1, r2)
         box: add a rectangular prism to the edge matrix - 
	    takes 6 arguemnts (x, y, z, width, height, depth)
	 clear: clear the currnt edge matrix -
	    takes 0 arguments
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;
  struct stack* shakestack;  //relative coordinate system stack
  int draw_mode=-1; //equals 0 if draw_edges, equals 1 if draw_polygons
  
  shakestack = new_stack();
  
  g.red = 0;
  g.green = 255;
  g.blue = 0;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
    
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
	  draw_mode = 0;
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 0;
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      //printf("BEZIER\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 0;
    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 0;
    }
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
	  draw_mode = 1;
    }
    else if (strncmp(line, "sphere", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 10);
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 1;
    }
    else if (strncmp(line, "torus", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 10);
      //printf( "%lf %lf %lf\n", x, y, z);
	  draw_mode = 1;
    }
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      //matrix_mult(tmp, transform);
      //print_matrix(transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      //matrix_mult(tmp, transform);
      //print_matrix(transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      //matrix_mult(tmp, transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      //matrix_mult(tmp, transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      //matrix_mult(tmp, transform);
      matrix_mult(tmp, shakestack->data[ shakestack->top ]);
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      //matrix_mult(transform, pm);
	  matrix_mult(shakestack->data[ shakestack->top ], pm);
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      clear_screen(s);
	  if( draw_mode==0 ) {
		draw_lines(pm, s, g);
	  }
	  else {
		draw_polygons(pm, s, g);
	  }
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      if( draw_mode==0 ) {
		draw_lines(pm, s, g);
	  }
	  else {
		draw_polygons(pm, s, g);
	  }
	  save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    //==================STACK COMMANDS=================================
    else if( strncmp(line, "push", strlen(line)) == 0 ) {
      push(shakestack);
      print_stack(shakestack);
    }
    else if( strncmp(line, "pop", strlen(line)) == 0 ) {
      pop(shakestack);
      print_stack(shakestack);
    }
    else if ( line[0] == '#' ) {
      printf("Invalid command\n");
    }
    //adding color coommand==================
    else if( strncmp(line, "color", strlen(line)) == 0 ) {
    	
    	fgets(line, 255, f);
    	sscanf(line, "%i %i %i", &x, &y, &z);
    	
   		g.red = x;
   		g.green = y;
   		g.blue = z;
   		
    }
	else {
		  printf("Invalid command\n");
    
	}
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
示例#16
0
文件: parser.c 项目: stuydw/curves
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 c: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 h: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 b: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {
    FILE *f = fopen(filename, "r");
    if (f == NULL) {
        printf("%s: %s\n", filename, strerror(errno));
    }

    color c;
    c.red = 255;
    c.green = 255;
    c.blue = 255;

    char buffer[100];
    while (fgets(buffer, 100, f)) {
        /*printf("%c\n", buffer[0]);*/
        switch (buffer[0]) {
            case 'l':
                {
                    int x0, y0, z0, x1, y1, z1;
                    fscanf(f, "%d %d %d %d %d %d\n", &x0, &y0, &z0, &x1, &y1, &z1);
                    add_edge(pm, x0, y0, z0, x1, y1, z1);
                    break;
                }
            case 'c':
                {
                    int cx, cy, r;
                    fscanf(f, "%d %d %d\n", &cx, &cy, &r);
                    add_circle(pm, cx, cy, r, .001);
                    break;
                }
            case 'h':
                {
                    double x0, y0, x1, y1, x2, y2, x3, y3;
                    fscanf(f, "%lf %lf %lf %lf %lf %lf %lf %lf\n", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3);
                    add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, .001, HERMITE_MODE);
                    break;
                }
            case 'b':
                {
                    double x0, y0, x1, y1, x2, y2, x3, y3;
                    fscanf(f, "%lf %lf %lf %lf %lf %lf %lf %lf\n", &x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3);
                    add_curve(pm, x0, y0, x1, y1, x2, y2, x3, y3, .001, BEZIER_MODE);
                    break;
                }
            case 'i':
                transform = new_matrix(4, 4);
                ident(transform);
                break;
            case 's':
                {
                    double sx, sy, sz;
                    fscanf(f, "%lf %lf %lf\n", &sx, &sy, &sz);
                    struct matrix *scale = make_scale(sx, sy, sz);
                    matrix_mult(scale, transform);
                    break;
                }
            case 't':
                {
                    double tx, ty, tz;
                    fscanf(f, "%lf %lf %lf\n", &tx, &ty, &tz);
                    struct matrix *translate = make_translate(tx, ty, tz);
                    matrix_mult(translate, transform);
                    break;
                }
                break;
            case 'x':
                {
                    double theta;
                    fscanf(f, "%lf\n", &theta);
                    struct matrix *rotX = make_rotX(rad(theta));
                    matrix_mult(rotX, transform);
                    break;
                }
                break;
            case 'y':
                {
                    double theta;
                    fscanf(f, "%lf\n", &theta);
                    struct matrix *rotY = make_rotY(rad(theta));
                    matrix_mult(rotY, transform);
                    break;
                }
                break;
            case 'z':
                {
                    double theta;
                    fscanf(f, "%lf\n", &theta);
                    struct matrix *rotZ = make_rotZ(rad(theta));
                    matrix_mult(rotZ, transform);
                    break;
                }
                break;
            case 'a':
                matrix_mult(transform, pm);
                break;
            case 'v':
                clear_screen(s);
                draw_lines(pm, s, c);
                display( s );
                break;
            case 'g':
                {
                    clear_screen(s);
                    draw_lines(pm, s, c);
                    char filename_buffer[100];
                    fgets(filename_buffer, 100, f);
                    save_extension(s, filename_buffer);
                    break;
                }
            case 'q':
                return;
        }
    }
}
示例#17
0
int main() {

  screen s;
  
  color c;
  c.red = 20;
  c.green = 45;
  c.blue = 187;
  
  clear_screen(s);

  struct matrix *points;
  struct matrix *edges;
  struct matrix *transform;
  /*
  printf("Creating points matrix...\n");
  points = new_matrix(4, 4);
  add_point(points, 100, 200, 300);
  add_point(points, 100, 200, 300);
  add_point(points, 100, 200, 300);
  add_point(points, 100, 200, 300);
  print_matrix(points);
  printf("\n");
  
  //
  struct matrix *point;
  point = new_matrix(4, 4);
  add_point(point, 100, 200, 300);
  add_point(point, 100, 200, 300);
  add_point(point, 100, 200, 300);
  add_point(point, 100, 200, 300);
  */

  printf("Creating edges matrix...\n");
  edges = new_matrix(4, 8);
  add_edge(edges, 0, 0, 1, 0, 100, 1); //(x0, y0, z0) = (1, 2, 3), (x1, y1, z1) = (4, 5, 6)
  add_edge(edges, 0, 100, 1, 100, 100, 1);
  add_edge(edges, 100, 100, 1, 100, 0, 1);
  add_edge(edges, 100, 0, 1, 0, 0, 1); 
  print_matrix(edges);
  
  //draw_lines(edges, s, c);
  /*
  struct matrix *rotX_matrix = new_matrix(4, 4);
  copy_matrix(make_rotX(60.0), rotX_matrix);
  print_matrix(make_rotX(60.0));
  for (int i = 0; i < 6; i++) {
    matrix_mult(rotX_matrix, edges);
    print_matrix(edges);
    draw_lines(edges, s, c);
    }
  printf("printing points matrix post-scalar...\n");
  double d = 2.0;
  scalar_mult(d, edges);
  print_matrix(edges);
  draw_lines(edges, s, c);
  //display(s);
  printf("\n");
    printf("printing points matrix post-ident...\n");
  ident(points);
  print_matrix(points);
  
  printf("printing points matrix post-matrix_mult...\n");
  matrix_mult(points, point);
  print_matrix(point);

  printf("Printing transform matrix...\n");
  c.red = 220;
  c.green = 45;
  c.blue = 87;
  
  transform = new_matrix(4, 4);
  add_edge(transform, 150, 100, 1, 150, 300, 1);
  add_edge(transform, 150, 300, 1, 350, 300, 1);
  //print_matrix(transform);
  
  draw_lines(transform, s, c);
  
  display(s);
  */
  draw_lines(edges, s, c);
  
  print_matrix(make_translate(100, 100, 0));
  struct matrix *translater = new_matrix(4, 4);
  copy_matrix(make_translate(30, 30, 0), translater);
  for (int i = 0; i < 13; i++) {
    matrix_mult(translater, edges);
    print_matrix(edges);
    draw_lines(edges, s, c);
  }

  c.red = 30;
  c.green = 100;
  c.blue = 190;

  struct matrix *translater_2 = new_matrix(4, 4);
  copy_matrix(make_translate(-400, -350, 0), translater_2);
  matrix_mult(translater_2, edges);
  

  struct matrix *scaler = new_matrix(4, 4);
  copy_matrix(make_scale(1.5, 2, 0), scaler);
  
  matrix_mult(scaler, edges);
  draw_lines(edges, s, c);

  for (int i = 0; i < 8; i++) {
    matrix_mult(translater, edges);
    //print_matrix(edges);
    draw_lines(edges, s, c);
  }

  struct matrix *translater_3 = new_matrix(4, 4);
  copy_matrix(make_translate(-150, -300, 0), translater_3);
  matrix_mult(translater_3, edges);
  draw_lines(edges, s, c);
  for (int i = 0; i < 8; i++) {
    matrix_mult(translater, edges);
    draw_lines(edges, s, c);
  }
  
  c.red = 200;
  c.green = 190;
  c.blue = 59;
  matrix_mult(translater_3, edges);
  matrix_mult(scaler, edges);
  
  for (int i = 0; i < 7; i++) {
    matrix_mult(translater, edges);
    draw_lines(edges, s, c);
  }
  c.red = 150;
  c.green = 90;
  c.blue = 29;
  struct matrix *translater_4 = new_matrix(4, 4);
  copy_matrix(make_translate(-70, -50, 0), translater_4);

  matrix_mult(translater_4, edges);
  matrix_mult(scaler, edges);

  for (int i = 0; i < 5; i++) {
    matrix_mult(translater, edges);
    draw_lines(edges, s, c);
  }
  display(s);
  save_extension(s, "matrix.png");
  free_matrix( edges );
}  
示例#18
0
文件: my_main.c 项目: stuydw/final
  /*======== 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 );    
}
示例#19
0
int main() {

  screen s;
  color c;

  c.red = 255;
  c.blue = 0;
  c.green = 0;
  clear_screen(s);
  
  struct matrix *edges;
  struct matrix *transform;
  struct matrix *rot;
  struct matrix *scale;
  
  edges = new_matrix(4, 4);
  transform = new_matrix(4, 4);
  
  /* Temporary stuff
  struct matrix* rotx = new_matrix(4,4);
  struct matrix* roty = new_matrix(4,4);
  struct matrix* rotz = new_matrix(4,4);
  */
  
  /* Testing Code
  print_matrix(edges);
  ident(edges);
  printf("Edges - Lastcol = %d\n",edges->lastcol);
  print_matrix(edges);
  scalar_mult(0.5,edges);
  print_matrix(edges);
  edges = make_translate(0.5,0.5,0.5);
  print_matrix(edges);
  printf("Transform:\n");
  copy_matrix(edges,transform);
  print_matrix(transform);
  printf("Transform - Lastcol = %d\n",transform->lastcol);
  grow_matrix(transform,8);
  print_matrix(transform);
  printf("%g\n",transform->m[0][transform->lastcol]);
  printf("\n");
  //ident(edges);
  ident(transform);
  matrix_mult(transform,edges);
  print_matrix(edges);
  add_point(edges,1,1,1);
  add_point(edges,1,1,1);
  add_point(edges,1,1,1);
  add_point(edges,1,1,1);
  add_point(edges,1,1,1);
  int lastcol = edges->lastcol;
  //printf("%d\n",!(edges->m[lastcol][0]));
  print_matrix(edges);
  printf("%s\n","RotX:");
  rotx = make_rotX(90.0);
  print_matrix(rotx);
  printf("%s\n","RotY:");
  roty = make_rotY(90.0);
  print_matrix(roty);
  printf("%s\n","RotZ:");
  rotz = make_rotZ(90.0);
  print_matrix(rotz);
  */

  transform = make_translate(-50,0,0);
  scale = make_scale(1.1,1.1,0);
  rot = make_rotZ(10);

  /*
  int count = 0;
  while(count < 15){
    add_point(edges,count*20,count*20,0);
    count ++;
    }*/
  add_edge(edges,200,200,0,400,200,0);
  add_edge(edges,400,200,0,400,400,0);
  add_edge(edges,400,400,0,200,400,0);
  add_edge(edges,200,400,0,200,200,0);
  draw_lines(edges,s,c);
  matrix_mult(scale,edges);
  print_matrix(edges);
  draw_lines(edges,s,c);
  matrix_mult(transform,edges);
  print_matrix(edges);
  draw_lines(edges,s,c);
  matrix_mult(rot,edges);
  draw_lines(edges,s,c);
  
  /*
  while(count < 36){
    print_matrix(edges);
    draw_lines(edges,s,c);
    matrix_mult(rot,edges);
    count ++;
    }*/
  
  free_matrix( transform );
  free_matrix( edges );
  display(s);
   
  
  save_extension(s, "matrix.png");
}  
示例#20
0
文件: parser.c 项目: stuydw/polygons
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguments (x0, y0, z0, x1, y1, z1)
         b: add a hermite curve to the edge matrix - 
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         h: add a bezier to the edge matrix - 
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         c: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
         m: add a sphere to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
         d: add a torus to the edge matrix - 
	    takes 4 arguments (cx, cy, r1, r2)
         p: add a rectangular prism to the edge matrix - 
	    takes 6 arguments (x, y, z, width, height, depth)
	 w: clear the current edge matrix -
	    takes 0 arguments
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

03/08/12 16:22:10
jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  g.red = 0;
  g.green = 255;
  g.blue = 255;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    char c;
    double x, y, z, x1, y1, z1, x2, y2, x3, y3, x4, y4;
   
    c = line[0];

    switch (c) {
    case 'l':
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      break;
    case 'p':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      break;
    case 'm':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 0.05);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'd':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 0.05);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'c':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'b':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 'h':
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      break;
    case 's':
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
      break;
    case 't':
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
      break;
    case 'x':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(tmp, transform);
      break;
    case 'y':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(tmp, transform);
      break;
    case 'z':
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(tmp, transform);
      break;
    case 'i':
      ident(transform);
      break;
    case 'a':
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
      break;
    case 'v':
 
      /*
      clear_screen(s);
      draw_lines(pm, s, g);
      display(s);
      break;
      */
      
      // Second version for triangles:
      clear_screen(s);
      draw_polygons(pm, s, g);
      display(s);
      break;
      
    case 'w':
      pm->lastcol = 0;
      break;
    case 'g':
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      draw_polygons(pm, s, g);
      save_extension(s, line);
      break;
    case 'q':
      return;
    case '#':
      break;
    default:
      printf("Invalid command\n");
      break;
    }
  }

  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
示例#21
0
文件: my_main.c 项目: stuydw/final
/*======== 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 );    
 


}
示例#22
0
文件: parser.c 项目: nspektor/3dHW
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  g.red = 255;
  g.green = 0;
  g.blue = 255;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
    double width, height, depth, radius, radius1, radius2;
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      //printf("BEZIER\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
    }
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      //printf("BOX\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf",
	     &x, &y, &z, &width, &height, &depth);
      add_box(pm, x, y, z, width, height, depth);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "sphere", strlen(line)) == 0 ) {
      //printf("SPHERE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &radius);
      add_sphere(pm, x, y, radius, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "torus", strlen(line)) == 0 ) {
      //printf("TORUS\n");ds
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &radius1, &radius2 );
      add_torus(pm, x, y, radius1, radius2, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
    }    
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      //printf("SCALE\n");
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f);
      //      line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      matrix_mult(tmp, transform);
      //print_matrix(transform);
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      matrix_mult(tmp, transform);
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      clear_screen(s);
      draw_lines(pm, s, g);
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
      draw_lines(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      clear_screen(s);
    }
    
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if (strncmp(line, "#", strlen(1)) == 0){
    }
    else {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
示例#23
0
文件: my_main.c 项目: stuydw/mdl
void my_main( int polygons ) {

  int i;
  double step;
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  
  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
      //simple cases
      //i realized that you already defined these constants  in another 
      //file after looking at this for 30 min -_-
    case POP:
      pop(s);
      break;
    case PUSH:
      push(s);
      break;

      //move,scale,rotate
    case MOVE:
      transform = make_translate(op[i].op.move.d[0],op[i].op.move.d[1],
				 op[i].op.move.d[2]);
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    case ROTATE:
      //there are 3 rotations possible, SO SWITCH-CEPTION!
      switch((int)op[i].op.rotate.axis)
	{
	case ROT_X:
	  transform = make_rotX(op[i].op.rotate.degrees);
	  break;
	case ROT_Y:
	  transform = make_rotY(op[i].op.rotate.degrees);
	  break;
	case ROT_Z:
	  transform = make_rotZ(op[i].op.rotate.degrees);
	  break;
	}
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    case SCALE:
      transform = make_scale(op[i].op.scale.d[0],op[i].op.scale.d[1],
			     op[i].op.scale.d[2]);
      matrix_mult(transform,s->data[s->top]);
      free_matrix(transform);
      break;
    
      //box,sphere,torus
    case BOX:
      add_box(tmp,op[i].op.box.d0[0],op[i].op.box.d0[1],op[i].op.box.d0[2],
	      op[i].op.box.d1[0],op[i].op.box.d1[1],op[i].op.box.d1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
    case SPHERE:
      add_sphere(tmp,op[i].op.sphere.d[0],op[i].op.sphere.d[1],
		 op[i].op.sphere.d[2],op[i].op.sphere.r,0.01);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
    case TORUS:
      add_torus(tmp,op[i].op.torus.d[0],op[i].op.torus.d[1],op[i].op.torus.d[2],
		op[i].op.torus.r0,op[i].op.torus.r1,0.01);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      free_matrix(tmp);
      //reset
      tmp=new_matrix(4,1000);
      break;
      
      //line
    case LINE:
      add_edge(tmp,op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2],
	       op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_lines(tmp,t,g);
      free_matrix(tmp);
      tmp=new_matrix(4,1000);//RESET
      break;
     
      //EVERYTIN else
    case SAVE:
      save_extension(t,op[i].op.save.p->name);
      break;
    case DISPLAY:
      display(t);
      break;
    default:
      break;
    }
  }
}
示例#24
0
文件: my_main.c 项目: NJacob/stuff
/*======== 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 );    
}
示例#25
0
文件: my_main.c 项目: stuydw/mdl
void my_main( int polygons ) {

  int i,ROT_AXIS;
  double step;
  double xval, yval, zval;
  struct matrix *transform;
  struct matrix *tmp;
  struct stack *s;
  screen t;
  color g;
  g.red = 122;
  g.green = 218;
  g.blue = 225;
  
  s = new_stack();
  tmp = new_matrix(4, 1000);
  clear_screen( t );

  for (i=0;i<lastop;i++) {  
    switch (op[i].opcode) {
    case PUSH:
      push(s);
      break;
    case POP:
      pop(s);
      break;
    case DISPLAY:
      display(t);
      break;
    case SAVE:
      save_extension(t,op[i].op.save.p->name);
      break;
    case MOVE:
      transform = make_translate(op[i].op.move.d[0],
			         op[i].op.move.d[1],
			         op[i].op.move.d[2]);
      matrix_mult(transform,s->data[s->top]);
      break;
    case SCALE:
      transform = make_scale(op[i].op.scale.d[0],
	    		     op[i].op.scale.d[1],
			     op[i].op.scale.d[2]);
      matrix_mult(transform,s->data[s->top]);
      break;
    case ROTATE:
      ROT_AXIS = op[i].op.rotate.axis;
      if(ROT_AXIS = 0)
	transform = make_rotX(op[i].op.rotate.degrees);
      else if(ROT_AXIS = 1)
	transform = make_rotY(op[i].op.rotate.degrees);
      else if(ROT_AXIS = 2)
	transform = make_rotZ(op[i].op.rotate.degrees);
      matrix_mult(transform,s->data[s->top]);
      break;
    case LINE:
      add_edge(tmp, op[i].op.line.p0[0],
	            op[i].op.line.p0[1],
	            op[i].op.line.p0[2],
	            op[i].op.line.p1[0], 
	            op[i].op.line.p1[1],
	            op[i].op.line.p1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_lines(tmp,t,g);
      break;
    case SPHERE:
      add_sphere(tmp,op[i].op.sphere.d[0],
  	             op[i].op.sphere.d[1],
	             op[i].op.sphere.d[3],
	             op[i].op.sphere.r,
	             .1);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      break;
    case BOX:
      add_box(tmp,op[i].op.box.d0[0],
	          op[i].op.box.d0[1],
	          op[i].op.box.d0[2],
	          op[i].op.box.d1[0],
	          op[i].op.box.d1[1],
	          op[i].op.box.d1[2]);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      break;
    case TORUS:
      add_torus(tmp,op[i].op.torus.d[0],
		    op[i].op.torus.d[1],
		    op[i].op.torus.d[2],
		    op[i].op.torus.r0,
		    op[i].op.torus.r1,
	  	    .1);
      matrix_mult(s->data[s->top],tmp);
      draw_polygons(tmp,t,g);
      break;
    }
  }
}
示例#26
0
/*======== 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 );
      
  }  
}
示例#27
0
文件: parser.c 项目: RongYu98/MDL
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         line: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 circle: add a circle to the edge matrix - 
	    takes 3 arguments (cx, cy, r)
	 hermite: add a hermite curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
	 bezier: add a bezier curve to the edge matrix -
	    takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
         sphere: add a sphere to the edge matrix - 
	    takes 3 arguemnts (cx, cy, r)
         torus: add a torus to the edge matrix - 
	    takes 4 arguemnts (cx, cy, r1, r2)
         box: add a rectangular prism to the edge matrix - 
	    takes 6 arguemnts (x, y, z, width, height, depth)
	 clear: clear the currnt edge matrix -
	    takes 0 arguments
	 ident: set the transform matrix to the identity matrix - 
	 scale: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 translate: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 xrotate: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 yrotate: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 zrotate: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 apply: apply the current transformation matrix to the 
	    edge matrix
	 display: draw the lines of the edge matrix to the screen
	    display the screen
	 save: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 quit: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {

  FILE *f;
  char line[256];
  struct matrix * tmp;
  double angle;
  color g;

  struct stack * STACK = new_stack();
  
  g.red = 0;
  g.green = 255;
  g.blue = 0;
  
  clear_screen(s);

  if ( strcmp(filename, "stdin") == 0 ) 
    f = stdin;
  else
    f = fopen(filename, "r");
  
  while ( fgets(line, 255, f) != NULL ) {
    line[strlen(line)-1]='\0';
    //printf(":%s:\n",line);
    double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
   
    
    if ( strncmp(line, "line", strlen(line)) == 0 ) {
      //      printf("LINE!\n");
      fgets(line, 255, f);
      //      printf("\t%s", line);
      //line[strlen(line)-1]='\0';
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_edge(pm, x, y, z, x1, y1, z1);
      // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "circle", strlen(line)) == 0 ) {
      //printf("CIRCLE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_circle(pm, x, y, z, 0.01);
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }    
    else if ( strncmp(line, "bezier", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;

    }    
    else if ( strncmp(line, "hermite", strlen(line)) == 0 ) {
      //printf("HERMITE\n");
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf",
	     &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
      add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE );
      //printf( "%lf %lf %lf\n", x, y, z);
      matrix_mult( STACK->data[ STACK->top], pm);
      draw_lines( pm, s, g);
      pm->lastcol = 0;
    }
    
    else if ( strncmp(line, "box", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1);
      add_box(pm, x, y, z, x1, y1, z1);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    
    else if (strncmp(line, "sphere", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      add_sphere(pm, x, y, z, 10);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    else if (strncmp(line, "torus", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1);
      add_torus(pm, x, y, z, z1, 10);
      matrix_mult( STACK->data[ STACK->top ], pm);
      draw_polygons( pm, s, g );
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "scale", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      //line[strlen(line)-1]='\0';      
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_scale(x, y, z);
      //matrix_mult(tmp, transform);
      //print_matrix(transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "translate", strlen(line)) == 0 ) {
      //printf("TRANSLATE\n");
      fgets(line, 255, f); 
      sscanf(line, "%lf %lf %lf", &x, &y, &z);
      tmp = make_translate(x, y, z);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotX( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotY( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) {
      //printf("ROTATE!\n");
      fgets(line, 255, f);
      sscanf(line, "%lf", &angle);
      angle = angle * (M_PI / 180);
      tmp = make_rotZ( angle);
      //matrix_mult(tmp, transform);
      //matrix_mult( tmp, STACK->data[ STACK->top ] );
      matrix_mult( STACK->data[ STACK->top ], tmp );
      copy_matrix( tmp, STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "ident", strlen(line)) == 0 ) {
      ident(transform);
    }
    else if ( strncmp(line, "apply", strlen(line)) == 0 ) {
      //printf("APPLY!\n");
      //print_matrix( transform );
      //      print_matrix(pm);
      matrix_mult(transform, pm);
    }
    else if ( strncmp(line, "print", strlen(line)) == 0) {
      print_matrix( STACK->data[ STACK->top ] );
    }
    else if ( strncmp(line, "display", strlen(line)) == 0 ) {
      display(s);
    }
    else if ( strncmp(line, "save", strlen(line)) == 0 ) {
      fgets(line, 255, f);
      // line[strlen(line)-1] = '\0';
      //clear_screen(s);
      //draw_polygons(pm, s, g);
      save_extension(s, line);
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ) {
      pm->lastcol = 0;
    }
    else if ( strncmp(line, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if ( strncmp(line, "push", strlen(line)) == 0 ) {
      push( STACK ); //seg fault
      printf("Pushed\n");
    }
    else if ( strncmp(line, "pop", strlen(line)) == 0 ) {
      pop( STACK );
      printf("Popped\n");
    } 
    else if ( line[0] != '#' ) {
      printf("Invalid command\n");
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}
示例#28
0
文件: my_main.c 项目: stuydw/final
/*======== 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 );    
}
示例#29
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
      takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,bv    
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {


  double args [6];
  char command [25];

  color c;
  c.red = 255;
  c.blue = 0;
  c.green = 0;

  FILE *f = fopen(filename, "r");


  if(f == NULL) {
    printf("cannot find file\n");
    exit(1);
  }

  while(fgets(command, 25, f)){
    if(command[0] == 'l') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf %lf %lf %lf", &args[0], &args[1], &args[2], &args[3], &args[4], &args[5]);
      add_edge(pm, args[0], args[1], args[2], args[3], args[4], args[5]);
    }

    else if (command[0] == 'i') {
      ident(transform);
    }

    else if (command[0] == 's') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]);
      matrix_mult(make_scale(args[0], args[1], args[2]), transform);
    }

    else if (command[0] == 't') {
      fgets(command, 25, f);
      sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]);
      struct matrix *m;
      m = make_translate(args[0], args[1], args[2]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'x') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotX(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'y') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotY(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'z') {
      fgets(command, 25, f);
      sscanf(command, "%lf", &args[0]);
      struct matrix *m;
      m = make_rotZ(args[0]);
      matrix_mult(m, transform);
    }

    else if (command[0] == 'a') {
      matrix_mult(transform, pm);
    }

    else if (command[0] == 'v') {
      draw_lines(pm, s, c);
    }

    else if (command[0] == 'g') {
      draw_lines(pm, s, c);
      fgets(command, 25, f);
      int i = strlen(command);
      command[i] = '\0';
      save_extension(s, command);
    }

    else if (command[0] == 'q') {
      exit(1);
    }
  }
  
}
示例#30
0
/*======== void parse_file () ==========
Inputs:   char * filename 
          struct matrix * transform, 
          struct matrix * pm,
          screen s
Returns: 

Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
     Every command is a single character that takes up a line
     Any command that requires arguments must have those arguments in the second line.
     The commands are as follows:
         l: add a line to the edge matrix - 
	    takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
	 i: set the transform matrix to the identity matrix - 
	 s: create a scale matrix, 
	    then multiply the transform matrix by the scale matrix - 
	    takes 3 arguments (sx, sy, sz)
	 t: create a translation matrix, 
	    then multiply the transform matrix by the translation matrix - 
	    takes 3 arguments (tx, ty, tz)
	 x: create an x-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 y: create an y-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 z: create an z-axis rotation matrix,
	    then multiply the transform matrix by the rotation matrix -
	    takes 1 argument (theta)
	 a: apply the current transformation matrix to the 
	    edge matrix
	 v: draw the lines of the edge matrix to the screen
	    display the screen
	 g: draw the lines of the edge matrix to the screen
	    save the screen to a file -
	    takes 1 argument (file name)
	 q: end parsing

See the file script for an example of the file format


IMPORTANT MATH NOTE:
the trig functions int math.h use radian mesure, but us normal
humans use degrees, so the file will contain degrees for rotations,
be sure to conver those degrees to radians (M_PI is the constant
for PI)

jdyrlandweaver
====================*/
void parse_file ( char * filename, 
                  struct matrix * transform, 
                  struct matrix * pm,
                  screen s) {
  int f = open(filename,O_RDONLY | O_CREAT,0);
  char *buf1 = (char *)malloc(sizeof(char) * 1000);
  //int i=0;   
  read(f,buf1,1000);
  //printf("%s \n",buf1);
  //char **buf = buf1;
  //  strsep(&buf1,"\n");
  char *tmp = (char *)malloc(sizeof(char) * 1000);
  char *tmp1 =(char *)malloc(sizeof(char) * 1000);
  //  printf("This is strlen:%d\n",strlen(buf1));
  int u,v,w,x,y,z;

  while(buf1 && strlen(buf1) > 0){
    //printf("This is strlen:%d\n",strlen(buf1));
    tmp = strsep(&buf1,"\n");
    if(tmp[0] == 'l'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';
      v=strsep(&tmp," ")-'0';
      w=strsep(&tmp," ")-'0';
      x=strsep(&tmp," ")-'0';
      y=strsep(&tmp," ")-'0';
      z=strsep(&tmp," ")-'0';

      printf("%d %d %d %d %d %d\n\n\n\n",u,v,w,x,y,z);
      
      add_edge(pm,u,v,w,x,y,z);
      print_matrix(pm);
    }
    if(tmp[0] == 'i'){
      ident(transform);
    }
    if(tmp[0] == 'q'){
      return;
    }
    if(tmp[0] == 's'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';
      v=strsep(&tmp," ")-'0';
      w=strsep(&tmp," ")-'0';
      // x=strsep(&tmp," ")-'0';
      //y=strsep(&tmp," ")-'0';
      //z=strsep(&tmp," ")-'0';
      
      printf("%d %d %d\n\n\n\n",u,v,w);
      
      //make_scale(u,v,w);
      matrix_mult(transform,make_scale(u,v,w));
    }
    if(tmp[0] == 'x'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';

      tmp = make_rotX(u);
      matrix_mult(tmp,transform);
    }
    if(tmp[0] == 'y'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';

      tmp = make_rotY(u);
      matrix_mult(tmp,transform);
    }
    if(tmp[0] == 'z'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';

      tmp = make_rotZ(u);
      matrix_mult(tmp,transform);
    }
    if(tmp[0] == 't'){
      tmp=strsep(&buf1,"\n");
      u=strsep(&tmp," ")-'0';
      v=strsep(&tmp," ")-'0';
      w=strsep(&tmp," ")-'0';
      printf("%d %d %d\n\n\n\n",u,v,w);
      
      tmp = make_translate(u,v,w);
      matrix_mult(tmp,tranform);
    }
    if(tmp[0] == 'a'){
      matrix_mult(transform,pm);
    }
    if(tmp[0] == 'v'){
      draw_lines(pm,s,c);
      display(s);
    }
    if(tmp[0] == 'g'){
      clear_screen();
      draw_lines();
      display(s);
      u=strsep(&buf1,"1");
      save_extension(s,u);
    }
  close(f);

}