Exemplo n.º 1
0
void draw_polygons( struct matrix *polygons, screen s, color c, struct matrix* zbuffer ) {
  
  int i;  
  for( i=0; i < polygons->lastcol-2; i+=3 ) {

    if ( calculate_dot( polygons, i ) < 0) {
      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 s, c, zbuffer);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 s, c, zbuffer);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 s, c, zbuffer);
      scan_line( polygons->m[0][i],  polygons->m[1][i],  polygons->m[2][i],
		 polygons->m[0][i+1],polygons->m[1][i+1],polygons->m[2][i+1],
		 polygons->m[0][i+2],polygons->m[1][i+2],polygons->m[2][i+2],
		 s, c, zbuffer);
    }
  }
}
Exemplo n.º 2
0
Arquivo: draw.c Projeto: stuydw/mdl
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
         screen s
         color c  
Returns: 


04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  int i;
  
  if ( polygons->lastcol < 3 ) {
    printf("Need at least 3 points to draw a polygon!\n");
    return;
  }
  printf("\n\n");
  for( i=0; i < polygons->lastcol-2; i+=3 ) {
    
    if ( calculate_dot( polygons, i ) >= 0 ) {
    //      printf("drawing polygon %d\n", i/3);    

      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 s, c);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 s, c);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 s, c);
    }
  }
}
Exemplo n.º 3
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
          screen s
          color c  
Returns: 
Goes through polygons 3 points at a time, drawing 
lines connecting each points to create bounding
triangles

04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c, double vx,double vy, double vz) {
  c.red=100;
  //scanline_conversion(polygons,s,c);
  c.red=255;
  int i;
  double ax,ay,az,bx,by,bz;
  double* normal;
  for( i=0; i < polygons->lastcol-2; i+=3 ) {

    if ( calculate_dot( polygons, i ,vx, vy, vz) < 0 ) {
      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 s, c);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 s, c);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 s, c);      
    }
  }
}
Exemplo n.º 4
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
          screen s
          color c  
Returns: 
Goes through polygons 3 points at a time, drawing 
lines connecting each points to create bounding
triangles

04/16/13 13:13:27
jdyrlandweaver
====================*/
void
draw_polygons (struct matrix *polygons, screen s, color c)
{
  int i = 0;
  int points = polygons->lastcol;
  while (i < points)
    {
      //backface culling here; disable for testing purposes
      if (calculate_dot (polygons, i) < 0)
	{
	  draw_line (polygons->m[0][i],
		     polygons->m[1][i],
		     polygons->m[0][i + 1], polygons->m[1][i + 1], s, c);

	  draw_line (polygons->m[0][i + 1],
		     polygons->m[1][i + 1],
		     polygons->m[0][i + 2], polygons->m[1][i + 2], s, c);

	  draw_line (polygons->m[0][i + 2],
		     polygons->m[1][i + 2],
		     polygons->m[0][i], polygons->m[1][i], s, c);
	}
      i += 3;
    }
//DONE
}
Exemplo n.º 5
0
void draw_polygons_z( struct matrix *polygons, screen s, color c ) {
  int i;
  for( i=0; i < polygons->lastcol-2; i+=3 ) {

    if ( calculate_dot( polygons, i ) < 0 ) {
      /*
      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 s, c);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 s, c);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 s, c);
      */
      //c = calculate_I(polygons, s, c, i);
      scanline_convert_z(polygons, s, c, i);
      //printf("Finished i:%d, total:%d\n",i,polygons->lastcol-2);
    }
  }
}
Exemplo n.º 6
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
          screen s
          color c  
Returns: 
Goes through polygons 3 points at a time, drawing 
lines connecting each points to create bounding
triangles

04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  int i;
 
  if ( polygons->lastcol < 3 ) {
    
    printf("Need at least 3 points to draw a line!\n");
    return;
  }

  for ( i = 0; i < polygons->lastcol-1; i+=3 ) {

    if ( calculate_dot(polygons, i) >= 0 ) {

      draw_line( polygons->m[0][i], polygons->m[1][i], 
		 polygons->m[0][i+1], polygons->m[1][i+1], s, c);
      draw_line( polygons->m[0][i+1], polygons->m[1][i+1], 
		 polygons->m[0][i+2], polygons->m[1][i+2], s, c);
      draw_line( polygons->m[0][i+2], polygons->m[1][i+2], 
		 polygons->m[0][i], polygons->m[1][i], s, c);

    }

  }

}
Exemplo n.º 7
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
          screen s
          color c  
Returns: 
Goes through polygons 3 points at a time, drawing 
lines connecting each points to create bounding
triangles

04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
	int x;
	for(x = 0; x < polygons->lastcol - 2; x+= 3) {
		if(calculate_dot(polygons, x) < 0) { 
			draw_line(polygons->m[0][x], polygons->m[1][x], polygons->m[0][x+1], polygons->m[1][x+1], s, c); //connect the points
			draw_line(polygons->m[0][x], polygons->m[1][x], polygons->m[0][x+1], polygons->m[1][x+1], s, c);
			draw_line(polygons->m[0][x+2], polygons->m[1][x+2], polygons->m[0][x+2], polygons->m[1][x+2], s, c);
		}
	}
	
}
Exemplo n.º 8
0
Arquivo: draw.c Projeto: stuydw/final
color flat(double x1, double y1, double z1, double x2, double y2, double z2, color c){
  double * normal = calculate_normal(x1, y1, z1, x2, y2, z2);
  double ka = 0.3;
  double kd = 0.37;
  double ks = 0.13;
  int red = c.red; int blue = c.blue; int green = c.green;
  c.red = red * ka; c.blue = blue * ka; c.green = green * ka;
  int dot = calculate_dot(x1, y1, z1, x2, y2, z2);
  c.red = c.red + (red * dot); c.blue = c.blue + (blue *dot); c. green = c.green + (green * dot);
  return c;
}
Exemplo n.º 9
0
/*======== void draw_polygons() ==========
 Inputs:   struct matrix *polygons
 screen s
 color c
 Returns:
 Goes through polygons 3 points at a time, drawing
 lines connecting each points to create bounding
 triangles
 
 04/16/13 13:13:27
 jdyrlandweaver
 ====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
    int i = 0;
    while (i < polygons->lastcol) {
        if(calculate_dot(polygons, i) >= 0){
            draw_line(polygons->m[0][i],polygons->m[1][i],
                      polygons->m[0][i + 1], polygons->m[1][i + 1],s,c);
            draw_line(polygons->m[0][i + 1], polygons->m[1][i + 1],
                      polygons->m[0][i + 2], polygons->m[1][i + 2],s,c);
            draw_line(polygons->m[0][i + 2], polygons->m[1][i + 2],
                      polygons->m[0][i], polygons->m[1][i],s,c);
        }
        i = i+3;
    }
}
Exemplo n.º 10
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
          screen s
          color c  
Returns: 
Goes through polygons 3 points at a time, drawing 
lines connecting each points to create bounding
triangles

04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  if(polygons->lastcol < 2)
    return;
  int i;
  for(i = 0; i < polygons->lastcol; i += 3){
    if(calculate_dot(polygons, i) < 0){
    draw_line(polygons->m[0][i], polygons->m[1][i], 
	      polygons->m[0][i+1], polygons->m[1][i+1], s, c);
    draw_line(polygons->m[0][i+1], polygons->m[1][i+1], 
	      polygons->m[0][i+2], polygons->m[1][i+2], s, c);
    draw_line(polygons->m[0][i+2], polygons->m[1][i+2], 
	      polygons->m[0][i], polygons->m[1][i], s, c);
    }
  }
}
Exemplo n.º 11
0
/*======== void draw_polygons() ==========
Inputs: struct matrix *polygons
screen s
color c
Returns:
Goes through polygons 3 points at a time, drawing
lines connecting each points to create bounding
triangles

04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  int pos = 0;
  if(polygons->lastcol < 2){
    printf("Need three points to determine a triangle.\n");
    return;
  }
  while(pos < polygons->lastcol){
    if(calculate_dot(polygons, pos) < 0){
      draw_line(polygons->m[0][pos], polygons->m[1][pos],
                polygons->m[0][pos + 1], polygons->m[1][pos + 1],s,c);
      draw_line(polygons->m[0][pos + 1], polygons->m[1][pos + 1],
                polygons->m[0][pos + 2], polygons->m[1][pos + 2],s,c);
      draw_line(polygons->m[0][pos + 2], polygons->m[1][pos + 2],
                polygons->m[0][pos], polygons->m[1][pos],s,c);
    }
    pos += 3;
  }
}
Exemplo n.º 12
0
Arquivo: draw.c Projeto: stuydw/final
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
         screen s
         color c  
Returns: 


04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  int i;
  
  if ( polygons->lastcol < 3 ) {
    printf("Need at least 3 points to draw a polygon!\n");
    return;
  }
  printf("\n\n");
  for( i=0; i < polygons->lastcol-2; i+=3 ) {
    
    if ( calculate_dot( polygons, i ) >= 0 ) {
      //      printf("drawing polygon %d\n", i/3);

      c.red = rand() % 255;
      c.blue = rand() % 255;
      c.green = rand() %255;

      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 s, c);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[2][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 s, c);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[2][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[2][i],
		 s, c);
      //printf("wu tang clan aint nuthin to f**k with\n");
      scan_line(polygons->m, s, c, i);
      //printf("Hello2\n");
    }
  }
}
Exemplo n.º 13
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
          screen s
          color c  
Returns: 
Goes through polygons 3 points at a time, drawing 
lines connecting each points to create bounding
triangles

04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  if(polygons->lastcol <= 3){
    printf("Three points determine a triangle\n");
    return;
  }
  int g;
  g = 0;

  /*
  int x;
  int y;
  x = g + 1;
  y = g + 2;
  */

  while(g<polygons->lastcol){
    if(calculate_dot(polygons, g) < 0 ){
      draw_line(polygons->m[0][g], polygons->m[1][g], polygons->m[0][g+1], polygons->m[1][g+1], s, c);
      draw_line(polygons->m[0][g+1], polygons->m[1][g+1], polygons->m[0][g+2], polygons->m[1][g+2], s, c);
      draw_line(polygons->m[0][g+2], polygons->m[1][g+2], polygons->m[0][g], polygons->m[1][g], s, c);
    }
    g += 3;
  }
}
Exemplo n.º 14
0
void scanline_conversion( struct matrix *polygons, screen s, color c, 
			  struct light* light, struct constants* constants, double vx, double vy, double vz) {
  int i;
  int bot,mid,top,temp;
  double ax,ay,az,bx,by,bz,centerx,centery,centerz;
  double* normal;
  double BT,BM,X1,X2,Y;
  double botX,botY,midX,midY,topX,topY;
  struct cur_light;
  color shaded,temp_l;
  struct light* cur_light;
  for(i = 0; i < polygons->lastcol-2; i+=3){
    if ( calculate_dot( polygons, i ,vx, vy, vz) < 0 ) {
	ax = polygons->m[0][i+1] - polygons->m[0][i];
	ay = polygons->m[1][i+1] - polygons->m[1][i];
	az = polygons->m[2][i+1] - polygons->m[2][i];
	bx = polygons->m[0][i+2] - polygons->m[0][i];
	by = polygons->m[1][i+2] - polygons->m[1][i];
	bz = polygons->m[2][i+2] - polygons->m[2][i];
	centerx=(polygons->m[0][i]+polygons->m[0][i+1]+polygons->m[0][i+2])/3;
	centery=(polygons->m[1][i]+polygons->m[1][i+1]+polygons->m[1][i+2])/3;
	centerz=(polygons->m[2][i]+polygons->m[2][i+1]+polygons->m[2][i+2])/3;
	normal = calculate_normal(ax,ay,az,bx,by,bz);
	shaded.red=0;
	shaded.blue=0;
	shaded.green=0;

	for (cur_light=light;cur_light!=NULL;cur_light=cur_light->next){
	  temp_l=calculate_diffuse(cur_light,c,constants,
				   centerx,centery,centerz,
				   normal[0],normal[1],normal[2]);
	  shaded.red+=temp_l.red;
	  shaded.blue+=temp_l.blue;
	  shaded.green+=temp_l.green;
	}


	shaded.red+=c.red * constants->red;
	shaded.green+=c.green * constants->green;
	shaded.blue+=c.blue * constants->blue;
	
	shaded.red = shaded.red<255? shaded.red : 255;
	shaded.red = shaded.red>0? shaded.red : 0;
	shaded.green = shaded.green<255? shaded.green : 255;
	shaded.green = shaded.green>0? shaded.green : 0;
	shaded.blue = shaded.blue<255? shaded.blue : 255;
	shaded.blue = shaded.blue>0? shaded.blue : 0;
	
      bot=0;
      for (temp=0;temp<3;temp++){
	if (polygons->m[1][i+temp]<polygons->m[1][i+bot]){
	  bot = temp;
	}
      }
      top=0;
      for (temp=0;temp<3;temp++){
	if (polygons->m[1][i+temp]>polygons->m[1][i+top]){
	  top = temp;
	}
      }
      for (temp=0;temp<3;temp++){
	if (temp!=top && temp!=bot){
	  mid=temp;
	}
      }
      //polygons->m[0][i+bot],polygons->m[1][i+bot] is bottom point
      //polygons->m[0][i+mid],polygons->m[1][i+mid] is middle point
      //polygons->m[0][i+top],polygons->m[1][i+top] is top point
      botX = polygons->m[0][i+bot];botY = polygons->m[1][i+bot];
      midX = polygons->m[0][i+mid];midY = polygons->m[1][i+mid];
      topX = polygons->m[0][i+top];topY = polygons->m[1][i+top];
      //initial slopes BT = bot to top BM = bot to Mid
      BT = (topX - botX)/(topY - botY);
      BM = (midX - botX)/(midY - botY);
      //Cooridinates that we'll be working with
      X1 = botX;
      X2 = botY<midY?botX:midX;
      Y = (int)botY;
      while (midY > Y){
	draw_line(X1,Y,X2,Y,s,shaded);
	Y += 1;
	X1 += BT;
	X2 += BM;
	if (fabs(X2-botX)>fabs(midX-botX)){
	  X2=midX;
	}
      }
      BM = (topX - midX)/(topY - midY);
      while (topY > Y){
	draw_line(X1,Y,X2,Y,s,shaded);
	Y += 1;
	X1 += BT;
	X2 += BM;
      }
    }
  }
}
Exemplo n.º 15
0
Arquivo: draw.c Projeto: stuydw/final
/*======== void draw_polygon() ==========
  Inputs:
  Return:

  ==================== */
void  draw_polygons(struct matrix *polygons, screen s)
{
  int i;
  double x0,x1, x2,
    y0, y1, y2,
    z0, z1, z2,
    tx, mx, bx,
    ty,my, by,
    tz, mz, bz,
    slope_bm, slope_bt, zslope_bm,zslope_bt;

  double Ka, Kd, Ks;

  if (polygons->lastcol < 3) {
    printf("Need at least 3 points to draw a polygon\n");
    return;
  }
  printf("\n\n");
	
  for (i = 0; i < polygons->lastcol - 2; i += 3) {    
    if (calculate_dot(polygons, i)>= 0) {
      
      color c = calculate_color(polygons, i);

      draw_line(polygons->m[0][i],
		polygons->m[1][i],
		polygons->m[2][i],
		polygons->m[0][i+1],
		polygons->m[1][i+1],
		polygons->m[2][i+1],
		s, c);
      draw_line(polygons->m[0][i+1],
		polygons->m[1][i+1],
		polygons->m[2][i+1],
		polygons->m[0][i+2],
		polygons->m[1][i+2],
		polygons->m[2][i+2],
		s, c);
      draw_line(polygons->m[0][i+2],
		polygons->m[1][i+2],
		polygons->m[2][i+2],
		polygons->m[0][i],
		polygons->m[1][i],
		polygons->m[2][i],
		s, c);
     
      //SCANLINE CONVERSION
      //Get Coordinates of polygon
      x0=polygons->m[0][i];
      y0=polygons->m[1][i];
      z0=polygons->m[2][i];
      x1=polygons->m[0][i+1];
      y1=polygons->m[1][i+1];
      z1=polygons->m[2][i+1];
      x2=polygons->m[0][i+2];
      y2=polygons->m[1][i+2];
      z2=polygons->m[2][i+2];
	    
      if (y0>=y1&&y0>y2) {
	//set top coordinates
	tx=x0;
	ty=y0;
	tz=z0;
	//set middle and bottom
	if (y1>=y2) {
	  mx=x1;
	  bx=x2;
	  by=y2;
	  my=y1;
	  mz=z1;
	  bz=z2;
	}
	//set middle and bottom
	else {
	  mx=x2;
	  bx=x1;
	  my=y2;
	  by=y1;
	  mz=z2;
	  bz=z1;
	}
      } else if (y1>y2&&y1>=y0) {
	tx=x1;
	ty=y1;
	tz=z1;
	if (y0>=y2) {
	  mx=x0;
	  bx=x2;
	  my=y0;
	  by=y2;
	  mz=z0;
	  bz=z2;
	} else {
	  mx=x2;
	  bx=x0;
	  my=y2;
	  by=y0;
	  mz=z2;
	  bz=z0;
	}
      } else {
	tx=x2;
	ty=y2;
	tz=z2;
	if (y0 >= y1) {
	  mx=x0;
	  bx=x1;
	  my=y0;
	  by=y1;
	  mz=z0;
	  bz=z1;
	} else {
	  mx=x1;
	  bx=x0;
	  my=y1;
	  by=y0;
	  mz=z1;
	  bz=z0;
	}
      }
	    
      slope_bm=(bx-mx)/(by-my);
      slope_bt=(bx-tx)/(by-ty);
      zslope_bm=(mz-bz)/(my-by);
      zslope_bt=(tz-bz)/(ty- by);
	    
      double X1=bx;
      double X2=bx;
      double Z1=bz;
      double Z2=bz;
      double Y=by;
	    
      while(Y<=my) {
	draw_line(X1,Y,Z1,X2,Y,Z2,s,c);
	X1=X1+slope_bm;
	X2=X2+slope_bt;
	Z1=Z1+zslope_bm;
	Z2=Z2+zslope_bt;
	Y++;
      }
	    
      slope_bm=(mx-tx)/(my-ty);
      zslope_bm=(mz-tz)/(my-ty);
      X1=mx;
      Z1=mz;
	    
      while (Y<=ty) {
	draw_line(X1,Y,Z1,X2,Y,Z2,s,c);
	X1=X1+slope_bm;
	X2=X2+slope_bt;
	Z1=Z1+zslope_bm;
	Z2=Z2+zslope_bt;
	Y++;
      }
    }
  }

}
Exemplo n.º 16
0
void plot1( screen s, int x, int y, int z, struct matrix *zbuf, double * n, struct constants *rcolor, color ambient, struct light ** point) {
  int newy = YRES - 1 - y;
  int i,j;
  color Ia,Id,Is;
  double *light_v = (double *)malloc(3*sizeof(double));
  double * view = (double *)malloc(3*sizeof(double));
  double * reflect = (double *)malloc(3*sizeof(double));
  double * normal = (double *)malloc(3*sizeof(double));
  normal[0]=n[0];
  normal[1]=n[1];
  normal[2]=n[2];
  view[0]=0;
  view[1]=0;
  view[2]=-1;
  color c;
  //printf("%f %f %f\n",n[0],n[1],n[2]);
  c.red = 0;
  c.green = 0;
  c.blue = 0;
  if ( x >= 0 && x < XRES && newy >=0 && newy < YRES && z > (int)zbuf->m[x][newy]){
    //printf("%f %f %f\n",n[0],n[1],n[2]);
    for(i = 0; point[i];i++){
      //ambient
      Ia.red = ambient.red * rcolor->r[0];
      Ia.green = ambient.green *rcolor->r[1];
      Ia.blue = ambient.blue *rcolor->r[2];

      //printf("%f %f %f\n",Ia.red,Ia.green,Ia.blue);
	     
      //LIGHT VECTOR
      light_v[0] = x - point[i]->l[0];
      light_v[1] = y - point[i]->l[1];
      light_v[2] = z - point[i]->l[2];

      //normalize
      normalize(light_v);
      normalize(normal);
      
      //diffuse
      Id.red = point[i]->c[0] * rcolor->g[0]*calculate_dot(light_v,normal)*-1;
      Id.green = point[i]->c[1] * rcolor->g[1]*calculate_dot(light_v,normal)*-1;
      Id.blue = point[i]->c[2] * rcolor->g[2]*calculate_dot(light_v,normal)*-1;

      //printf("%f %f %f\n",normal[0],normal[1],normal[2]);
      //specular
      reflect[0] = 2 * calculate_dot(light_v, normal)*normal[0] - light_v[0];
      reflect[1] = 2 * calculate_dot(light_v, normal)*normal[1] - light_v[1];
      reflect[2] = 2 * calculate_dot(light_v, normal)*normal[2] - light_v[2];
      
      Is.red = point[i]->c[0] * rcolor->b[0] * calculate_dot(reflect,view) * calculate_dot(reflect,view) * calculate_dot(reflect,view);
      Is.green = point[i]->c[1] * rcolor->b[1] * calculate_dot(reflect,view) * calculate_dot(reflect,view) * calculate_dot(reflect,view);
      Is.blue = point[i]->c[2] * rcolor->b[2] * calculate_dot(reflect,view) * calculate_dot(reflect,view) * calculate_dot(reflect,view);
      
      c.red += Ia.red + Id.red + Is.red;
      c.blue += Ia.blue + Id.blue + Is.blue;
      c.green += Ia.green + Id.green + Is.green;
      
      //printf("%f %f %f\n",c.red, c.green, c.blue);
      //printf("lighting\n");
      //printf("%d\n",i);
      /* c.red = c.red>255?255:c.red;
      c.red = c.red<0?0:c.red;
      c.green = c.green>255?255:c.green;
      c.green = c.green<0?0:c.green;
      c.blue = c.blue>255?255:c.blue;
      c.blue = c.blue<0?0:c.blue;*/
    }
    c.red = c.red>255?255:c.red;
      c.red = c.red<0?0:c.red;
      c.green = c.green>255?255:c.green;
      c.green = c.green<0?0:c.green;
      c.blue = c.blue>255?255:c.blue;
      c.blue = c.blue<0?0:c.blue;
      
    s[x][newy]=c;
    zbuf->m[x][newy]=z;
  }
  //printf("plot color: %f, %f, %f\n", c.red, c.green, c.blue);
}
Exemplo n.º 17
0
Arquivo: draw.c Projeto: stuydw/final
/*======== void draw_polygons() ==========
Inputs: struct matrix *polygons
screen s
color c
Returns:


04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c ) {

    // ALL VARIABLES USED:
    int i, j;
    //int moe, curly, larry;
    int isCaseTwo = 0;
    double currentXFor, currentXTemp, currentY, currentZFor, currentZTemp;

    color cc;

    // INITIAL NAMES GIVEN TO COORDINATES OF THREE POINTS BEING EXAMINED
    double x1, x2, x3, y1, y2, y3, z1, z2, z3;

    //double drawX0, drawX1, drawZ0, drawZ1;

    // AMONG THE THREE POINTS, BOTTOM, MIDDLE, TOP
    double whyB, whyM, whyT;
    double exB, exM, exT;
    double zeeB, zeeM, zeeT;

    double delForeverX, delTempX; // delta zero and delta one in x
    double delForeverZ, delTempZ; // delta zero and delta one in z

    int x, z, d, dx, dz;

    double colorR, colorG, colorB;

    double maxY; // FOR LINES BETWEEN VERTICES OF A POLYGON

    // CHECK IF POLYGONS MATRIX IS VALID FOR DRAWING POLYGONS
    if ( polygons->lastcol < 3 ) {
        printf("Need at least 3 points to draw a polygon!\n");
        return;
    }
    printf("\n\n");

    // FOR LOOP TO GO THROUGH POLYGONS MATRIX, 3 POINTS AT A TIME
    for( i=0; i < polygons->lastcol-2; i+=3 ) {

        //MAKES EACH POLYGON A DIFFERENT COLOR, FOR TESTING
        //ambi.red = (ambi.red - 50) % 256;
        //ambi.green = (ambi.green + 75) % 256;
        //ambi.blue = (ambi.blue - 36) % 256;

        // CHECK IF POLYGON FACES VIEWER TO COMPLETE BACK FACE CULLING
        if ( calculate_dot( polygons, i ) >= 0 ) {
            // printf("drawing polygon %d\n", i/3);
            cc = allLight(polygons, i);/////////////////////////////////////////

            // SET COORDINATES TO RESPECTIVE POLYGON MATRIX VALUES
            x1 = polygons->m[0][i];
            y1 = polygons->m[1][i];
            z1 = polygons->m[2][i];

            x2 = polygons->m[0][i+1];
            y2 = polygons->m[1][i+1];
            z2 = polygons->m[2][i+1];

            x3 = polygons->m[0][i+2];
            y3 = polygons->m[1][i+2];
            z3 = polygons->m[2][i+2];


            // DRAW EDGES OF POLYGON MATRIX

            /*
               colorR = ambi.red;
               colorG = ambi.green;
               colorB = ambi.blue;

               ambi.red = 0;
               ambi.green = 0;
               ambi.blue = 0;

               draw_line( polygons->m[0][i],
               polygons->m[1][i],
               polygons->m[0][i+1],
               polygons->m[1][i+1],
               s, ambi);

               draw_line(polygons->m[0][i+1],
               polygons->m[1][i+1],
               polygons->m[0][i+2],
               polygons->m[1][i+2],
               s, ambi);

               draw_line( polygons->m[0][i+2],
               polygons->m[1][i+2],
               polygons->m[0][i],
               polygons->m[1][i],
               s, ambi);

               ambi.red = colorR;
               ambi.green = colorG;
               ambi.blue = colorB;
               */

            /*
               draw_line( polygons->m[0][i],
               polygons->m[1][i],
               polygons->m[0][i+1],
               polygons->m[1][i+1],
               s, c);

               draw_line( polygons->m[0][i+1],
               polygons->m[1][i+1],
               polygons->m[0][i+2],
               polygons->m[1][i+2],
               s, c);

               draw_line( polygons->m[0][i+2],
               polygons->m[1][i+2],
               polygons->m[0][i],
               polygons->m[1][i],
               s, c);
               */

            // DETERMINE IF TRIANGLE IS OF TYPE ONE
            if((y1 != y2) && (y2 != y3) && (y3 != y1)) {
                // Establishes bottom, middle, top y-coordinates in case 1 triangles

                // AS INITIAL STEP, MAKE BOTTOM COORDINATES CORRESPOND TO x1,y1,z1
                whyB = y1;
                exB = x1;
                zeeB = z1;

                // IF y2 IS LESS THAN whyB, SWITCH THE BOTTOM COORDINATES TO x2,y2,z2
                if((y2 < whyB)) {
                    whyB = y2;
                    exB = x2;
                    zeeB = z2;
                }

                // IF y3 IS LESS THAN whyB, SWITCH THE BOTTOM COORDINATES TO x3,y3,z3
                if((y3 < whyB)) {
                    whyB = y3;
                    exB = x3;
                    zeeB = z3;
                }

                // AS INITIAL STEP, MAKE TOP COORDINATES CORRESPOND TO x1,y1,z1
                whyT = y1;
                exT = x1;
                zeeT = z1;

                // IF y2 IS GREATER THAN whyT, SWITCH THE TOP COORDINATES TO x2,y2,z2
                if((y2 > whyT)) {
                    whyT = y2;
                    exT = x2;
                    zeeT = z2;
                }

                // IF y3 IS GREATER THAN whyT, SWITCH THE TOP COORDINATES TO x3,y3,z3
                if((y3 > whyT)) {
                    whyT = y3;
                    exT = x3;
                    zeeT = z3;
                }

                // IF y1 IS BETWEEN whyB AND whyT, MAKE IT whyM
                if((y1 > whyB) && (y1 < whyT)) {
                    whyM = y1;
                    exM = x1;
                    zeeM = z1;
                }

                // IF y2 IS BETWEEN whyB AND whyT, MAKE IT whyM
                if((y2 > whyB) && (y2 < whyT)) {
                    whyM = y2;
                    exM = x2;
                    zeeM = z2;
                }

                // IF y3 IS BETWEEN whyB AND whyT, MAKE IT whyM
                if((y3 > whyB) && (y3 < whyT)) {
                    whyM = y3;
                    exM = x3;
                    zeeM = z3;
                }

                // SET THE SLOPE FOR THE x0 AND THE TEMPORARY x1
                delForeverX = (exT - exB) / (whyT - whyB);
                delTempX = (exM - exB) / (whyM - whyB);

                delForeverZ = (zeeT - zeeB) / (whyT - whyB);
                delTempZ = (zeeM - zeeB) / (whyM - whyB);

                // SET THE STARTING y VALUE (currentY) TO whyB
                currentY = whyB;

                // WHILE LOOP THAT STOPS AT whyM, THE MIDDLE VERTEX
                while(currentY <= whyM) {

                    // SET THE TWO xS USING POINT-SLOPE FORM
                    currentXFor = exB + delForeverX * (currentY - whyB);
                    currentXTemp = exB + delTempX * (currentY - whyB);

                    currentZFor = zeeB + delForeverZ * (currentY - whyB);
                    currentZTemp = zeeB + delTempZ * (currentY - whyB);

                    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!

                    //x = (int)currentXFor;
                    //z = (int)currentZFor;
                    x = currentXFor;
                    z = currentZFor;

                    //swap points so we're always drawing left to right
                    if ( currentXFor > currentXTemp ) {
                        //x = (int)currentXTemp;
                        x = currentXTemp;
                        //z = (int)currentZTemp;
                        z = currentZTemp;
                        //currentXTemp = (int)currentXFor; // signifies x1
                        currentXTemp = currentXFor;
                        //currentZTemp = (int)currentZFor; // signifies y1
                        currentZTemp = currentZFor;
                    }

                    draw_scanline(x, z, currentXTemp, currentY, currentZTemp, s, cc);

                    //draw_line(currentXFor, currentY, currentXTemp, currentY, s, c);

                    currentY++;
                }

                while((currentY > whyM) && (currentY <= whyT)) {
                    delTempX = (exT - exM) / (whyT - whyM);
                    currentXFor = exB + delForeverX * (currentY - whyB);
                    currentXTemp = exM + delTempX * (currentY - whyM);

                    delTempZ = (zeeT - zeeM) / (whyT - whyM);
                    currentZFor = zeeB + delForeverZ * (currentY - whyB);
                    currentZTemp = zeeM + delTempZ * (currentY - whyM);


                    x = currentXFor;
                    z = currentZFor;

                    //swap points so we're always drawing left to right
                    if ( currentXFor > currentXTemp ) {
                        //x = (int)currentXTemp;
                        x = currentXTemp;
                        //z = (int)currentZTemp;
                        z = currentZTemp;
                        //currentXTemp = (int)currentXFor; // signifies x1
                        currentXTemp = currentXFor;
                        //currentZTemp = (int)currentZFor; // signifies y1
                        currentZTemp = currentZFor;
                    }

                    draw_scanline(x, z, currentXTemp, currentY, currentZTemp, s, cc);

                    //draw_line(currentXFor, currentY, currentXTemp, currentY, s, c);

                    currentY++;
                }
            }

            if((y1 == y2) || (y2 == y3) || (y3 == y1)) {

                if(y1 == y2) {
                    if(y1 >= y3) {
                        isCaseTwo = 1;
                        whyB = y3;
                        exB = x3;
                        whyT = y1;
                        exT = x1;
                        whyM = y2;
                        exM = x2;
                    }

                    if(y1 < y3) {
                        isCaseTwo = 0;
                        whyT = y3;
                        whyB = y1;
                        whyM = y2;
                        exT = x3;
                        exB = x1;
                        exM = x2;
                    }
                }

                if(y2 == y3) {
                    if(y2 >= y1) {
                        isCaseTwo = 1;
                        whyB = y1;
                        whyT = y2;
                        whyM = y3;
                        exB = x1;
                        exT = x2;
                        exM = x3;
                    }
                    if(y2 < y1) {
                        isCaseTwo = 0;
                        whyB = y2;
                        whyT = y1;
                        whyM = y3;
                        exB = x2;
                        exT = x1;
                        exM = x3;
                    }
                }

                if(y3 == y1) {
                    if(y2 > y3) {
                        isCaseTwo = 0;
                        whyT = y2;
                        whyB = y3;
                        whyM = y1;
                        exT = x2;
                        exB = x3;
                        exM = x1;
                    }
                    if(y2 <= y3) {
                        isCaseTwo = 1;
                        whyT = y3;
                        whyM = y1;
                        whyB = y2;
                        exT = x3;
                        exM = x1;
                        exB = x2;
                    }

                }

                // CASES THAT HAVE NOT BEEN CHANGED YET:

                if(isCaseTwo == 0) {
                    currentY = whyB;
                    delForeverX = (exT - exB) / (whyT - whyB);
                    delTempX = (exT - exM) / (whyT - whyM);
                    while(currentY <= whyT) {
                        currentXFor = exB + delForeverX * (currentY - whyB);
                        currentXTemp = exM + delTempX * (currentY - whyM);


                        // IN THE ZONE AGAIN


                        x = currentXFor;
                        z = currentZFor;

                        //swap points so we're always drawing left to right
                        if ( currentXFor > currentXTemp ) {
                            //x = (int)currentXTemp;
                            x = currentXTemp;
                            //z = (int)currentZTemp;
                            z = currentZTemp;
                            //currentXTemp = (int)currentXFor; // signifies x1
                            currentXTemp = currentXFor;
                            //currentZTemp = (int)currentZFor; // signifies y1
                            currentZTemp = currentZFor;
                        }

                        draw_scanline(x, z, currentXTemp, currentY, currentZTemp, s, cc);

                        //draw_line(currentXFor, currentY, currentXTemp, currentY, s, c);

                        currentY++;
                    }
                }

                if(isCaseTwo == 1) {
                    currentY = whyB;
                    delForeverX = (exT - exB) / (whyT - whyB);
                    delTempX = (exM - exB) / (whyM - whyB);
                    while(currentY <= whyT) {
                        currentXFor = exB + delForeverX * (currentY - whyB);
                        currentXTemp = exB + delTempX * (currentY - whyB);


                        // MORE OF THE ZONE

                        x = currentXFor;
                        z = currentZFor;

                        //swap points so we're always drawing left to right
                        if ( currentXFor > currentXTemp ) {
                            //x = (int)currentXTemp;
                            x = currentXTemp;
                            //z = (int)currentZTemp;
                            z = currentZTemp;
                            //currentXTemp = (int)currentXFor; // signifies x1
                            currentXTemp = currentXFor;
                            //currentZTemp = (int)currentZFor; // signifies y1
                            currentZTemp = currentZFor;
                        }

                        draw_scanline(x, z, currentXTemp, currentY, currentZTemp, s, cc);

                        //draw_line(currentXFor, currentY, currentXTemp, currentY, s, c);
                        currentY++;
                    }
                }
            }

            colorR = cc.red;
            colorG = cc.green;
            colorB = cc.blue;
            /*
            cc.red = 0;
            cc.green = 0;
            cc.blue = 0;
            */
            draw_zL( polygons->m[0][i],
                     polygons->m[1][i],
                     polygons->m[2][i],
                     polygons->m[0][i+1],
                     polygons->m[1][i+1],
                     polygons->m[2][i+1],
                     s, cc);

            draw_zL( polygons->m[0][i+1],
                     polygons->m[1][i+1],
                     polygons->m[2][i+1],
                     polygons->m[0][i+2],
                     polygons->m[1][i+2],
                     polygons->m[2][i+2],
                     s, cc);

            draw_zL( polygons->m[0][i+2],
                     polygons->m[1][i+2],
                     polygons->m[2][i+2],
                     polygons->m[0][i],
                     polygons->m[1][i],
                     polygons->m[2][i],
                     s, cc);

            cc.red = colorR;
            cc.green = colorG;
            cc.blue = colorB;

            /*
            cc.red = ambiRed;
            cc.green = ambiGreen;
            cc.blue = ambiBlue;
            */
        }
    }

}
Exemplo n.º 18
0
Arquivo: draw.c Projeto: stuydw/final
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
         screen s
         color c  
Returns: 


04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c2 ) {
  int i = 0;

double kar, kag, kab, kdr,kdg,kdb,ksb,ksr,ksg;
lcons* curcons= lclist->next;
if(curcons != NULL){
//printf("Not null.\n");
//kar = curcons->kar;
//kag = curcons->kag;
//kab = curcons->kab;
//printf("Object %d ambient constants -> %lf, %lf, %lf\n",i, kar,kag,kab);
while (i < obj){
if (curcons->next != NULL){
curcons = curcons->next;
//printf("shift\n");
}
i++;
}
}
if (curcons != NULL){
kar = curcons->kar;
kag = curcons->kag;
kab = curcons->kab;
kdr = curcons->kdr;
kdg = curcons->kdg;
kdb = curcons->kdb;
ksr = curcons->ksr;
ksg = curcons->ksg;
ksb = curcons->ksb;
}


//printf("Object %d ambient constants -> %lf, %lf, %lf\n",obj, kar,kag,kab);
//printf("Object %d diffuse constants -> %lf, %lf, %lf\n",obj, kdr,kdg,kdb);
//printf("Object %d specular constants -> %lf, %lf, %lf\n\n",obj, ksr,ksg,ksb);


color c;
int j = 0;

//c.red = c2.red * kar;
//c.green = c2.green * kag;
//c.blue = c2.blue * kab;


  if ( polygons->lastcol < 3 ) {
    printf("Need at least 3 points to draw a polygon!\n");
    return;
  }
  //printf("\n\n");
  for( i=0; i < polygons->lastcol-2; i+=3 ) {


//  c.red = (c.red + 17)%255;
//  c.green =(c.green + 3)%255;
//  c.blue = (c.blue + 5)%255;


    if ( calculate_dot( polygons, i ) >= 0 ) {
    //      printf("drawing polygon %d\n", i/3);   


double b, m, t;
double uo, ut, ur;
int oo, ot, or,ob, ott, om;
int bbb, mmm, ttt;
oo = 0;
ot = 0;
or = 0;
ob = 0;
om = 0;
ott = 0;
uo = polygons->m[1][i];
ut = polygons->m[1][i+1];
ur = polygons->m[1][i+2];
double x1, x2, x3;
double z1, z2, z3;
//
x1 = polygons->m[0][i];
x2 = polygons->m[0][i+1];
x3 = polygons->m[0][i+2];
z1 = polygons->m[2][i];
z2 = polygons->m[2][i+1];
z3 = polygons->m[2][i+2];
//printf("points->(%lf,%lf,%lf);(%lf,%lf,%lf);(%lf,%lf,%lf)\n",x1,uo,z1,x2,ut,z2,x3,ur,z3);
//
if(ott == 0 && uo >= ut && uo>= ur){
t = uo;
oo++;
ott++;
x3 = polygons->m[0][i];
z3 = polygons->m[2][i];
ttt = 0;
}
if(ott == 0 && ut >= uo && ut>= ur){
t = ut;
ot++;
ott++;
x3 = polygons->m[0][i+1];
z3 = polygons->m[2][i+1];
ttt = 1;
}
if(ott == 0 && ur >= ut && ur>= uo){
t = ur;
or++;
ott++;
x3 = polygons->m[0][i+2];
z3 = polygons->m[2][i+2];
ttt = 2;
}

if(ob == 0 && oo == 0 && uo <= ut && uo<= ur){
b = uo;
oo++;
ob++;
x1 = polygons->m[0][i];
z1 = polygons->m[2][i];
bbb = 0;
}
if(ob == 0 && ot == 0 && ut <= uo && ut<= ur){
b = ut;
ot++;
ob++;
x1 = polygons->m[0][i+1];
z1 = polygons->m[2][i+1];
bbb = 1;
}
if(ob == 0 && or == 0 && ur <= ut && ur<= uo){
b = ur;
or++;
ob++;
x1 = polygons->m[0][i+2];
z1 = polygons->m[2][i+2];
bbb = 2;
}

if (om == 0 && oo == 0){
m = uo;
om++;
x2 = polygons->m[0][i];
z2 = polygons->m[2][i];
mmm = 0;
}
if (om == 0 && ot == 0){
m = ut;
om++;
x2 = polygons->m[0][i+1];
z2 = polygons->m[2][i+1];
mmm = 1;
}
if (om == 0 && or == 0){
m = ur;
om++;
x2 = polygons->m[0][i+2];
z2 = polygons->m[2][i+2];
mmm = 2;
}
//printf("bmt -> (%lf,%lf,%lf);(%lf,%lf,%lf);(%lf,%lf,%lf)\n\n",x1,b,z1,x2,m,z2,x3,t,z3);
double bt, bm, mt;
if (t == b){
bt = DBL_MAX;//9999999;//DBL_MAX;//
if (x3 < x1){
bt = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bt = 1.0 * (x3 - x1)/(t - b);
}
if (m == b){
bm = DBL_MAX;//9999999;//DBL_MAX;//
if (x2 < x1){
bm = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bm = 1.0 * (x2 - x1)/(m - b);
}
if (t == m){
mt = DBL_MAX;//9999999;//DBL_MAX;//
if (x3 < x2){
mt = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
mt = 1.0 * (x3 - x2)/(t - m);
}
//////zzzzzzzz
double btz, bmz, mtz;
if (t == b){
btz = DBL_MAX;//9999999;//DBL_MAX;//
if (z3 < z1){
btz = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
btz = 1.0 * (z3 - z1)/(t - b);
}
if (m == b){
bmz = DBL_MAX;//9999999;//DBL_MAX;//
if (z2 < z1){
bmz = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bmz = 1.0 * (z2 - z1)/(m - b);
}
if (t == m){
mtz = DBL_MAX;//9999999;//DBL_MAX;//
if (z3 < z2){
mtz = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
mtz = 1.0 * (z3 - z2)/(t - m);
}
//printf("inv slopes %lf %lf %lf\n",bt,bm,mt);
//printf("inv z slopes %lf %lf %lf\n",btz,bmz,mtz);
double m2 = bm;
double mz2 = bmz;
double drx, drx2, drx3;
double drz, drz2, drz3;
drx2 = x1;
drx = x1;
drx3 = x2;
drz2 = z1;
drz = z1;
drz3 = z2;
int abcdef = 0;





c.red = c2.red * kar;
c.green = c2.green * kag;
c.blue = c2.blue * kab; 
///*//////////////////////////////////////FLAT
double ax, ay, az, bx, by, bz, cx, cy, cz;
  double *normal;
  double vx, vy, vz;
  double dot, mag;

for (j = 0; j < ptl; j++){

  //get as and bs to calculate the normal
  ax = polygons->m[0][i + 1] - polygons->m[0][ i ];
  ay = polygons->m[1][i + 1] - polygons->m[1][ i ];
  az = polygons->m[2][i + 1] - polygons->m[2][ i ];

  bx = polygons->m[0][ i ] - polygons->m[0][ i + 2 ];
  by = polygons->m[1][ i ] - polygons->m[1][ i + 2 ];
  bz = polygons->m[2][ i ] - polygons->m[2][ i + 2 ];

  cx = (polygons->m[0][ i ] + polygons->m[0][i + 1] + polygons->m[0][ i + 2 ])/3;
  cy = (polygons->m[1][ i ] + polygons->m[1][i + 1] +  polygons->m[1][ i + 2 ])/3;
  cz = (polygons->m[2][ i ] + polygons->m[2][i + 1] +  polygons->m[2][ i + 2 ])/3;

  normal = calculate_normal( ax, ay, az, bx, by, bz );

  //normalize the surface normal
  mag = sqrt( (normal[0] * normal[0]) + 
	      (normal[1] * normal[1]) + 
	      (normal[2] * normal[2]) );

  normal[0] = normal[0] / mag;
  normal[1] = normal[1] / mag;
  normal[2] = normal[2] / mag;

  //set up the view vector values
  vx = ptlights[j][0] - cx;
  vy = ptlights[j][1] - cy;
  vz = ptlights[j][2] - cz;

  mag = sqrt( (vx * vx) + 
	      (vy * vy) + 
	      (vz * vz) );

  vx = vx / mag;
  vy = vy / mag;
  vz = vz / mag;

  //calculate the dot product
  dot = normal[0] * vx + normal[1] * vy + normal[2] * vz;

/////////////////////diffuse

if (c.red + ptlights[j][3] * kdr * dot >= 255){
c.red = 255;
}
else{
if (ptlights[j][3] * kdr * dot >= 0){
c.red+= ptlights[j][3] * kdr * dot;
}
}
if (c.green + ptlights[j][4] * kdg * dot >= 255){
c.green = 255;
}
else{
if (ptlights[j][4] * kdg * dot >= 0){
c.green+= ptlights[j][4] * kdg * dot;
}
}
if (c.blue + ptlights[j][5] * kdb * dot >= 255){
c.blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot >= 0){
c.blue+= ptlights[j][5] * kdb * dot;
}
}
/////////////////////specular
normal[0]*= 2 * dot; 
normal[1]*= 2 * dot; 
normal[2]*= 2 * dot; 
	
normal[0]-= vx; 
normal[1]-= vy; 
normal[2]-= vz;

dot = -1 * normal[2];

if (c.red + ptlights[j][3] * kdr * dot >= 255){
c.red = 255;
}
else{
if (ptlights[j][3] * kdr * dot >= 0){
c.red+= ptlights[j][3] * kdr * dot;
}
}
if (c.green + ptlights[j][4] * kdg * dot >= 255){
c.green = 255;
}
else{
if (ptlights[j][4] * kdg * dot >= 0){
c.green+= ptlights[j][4] * kdg * dot;
}
}
if (c.blue + ptlights[j][5] * kdb * dot >= 255){
c.blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot >= 0){
c.blue+= ptlights[j][5] * kdb * dot;
}
}

  free( normal );
}
ib.red = c.red;
ib.green = c.green;
ib.blue = c.blue;
im.red = c.red;
im.green = c.green;
im.blue = c.blue;
it.red = c.red;
it.green = c.green;
it.blue = c.blue;
//*///////////////////////////////////////\FLAT


/*//////////////////////////////////////GOROUD
setc = 1;

ib.red = c.red;
ib.green = c.green;
ib.blue = c.blue;
im.red = c.red;
im.green = c.green;
im.blue = c.blue;
it.red = c.red;
it.green = c.green;
it.blue = c.blue;

  double *normal;
  double *normal2;
  double *normal3;
  double vx, vy, vz;
  double vx1, vy1, vz1;
  double vx2, vy2, vz2;
  double dot, mag, dot2, dot3;

for (j = 0; j < ptl; j++){


normal = vertex_normal(polygons,i);
normal2 = vertex_normal(polygons,i+1);
normal3 = vertex_normal(polygons,i+2);

  //normalize the surface normal
  mag = sqrt( (normal[0] * normal[0]) + 
	      (normal[1] * normal[1]) + 
	      (normal[2] * normal[2]) );

  normal[0] = normal[0] / mag;
  normal[1] = normal[1] / mag;
  normal[2] = normal[2] / mag;
mag = sqrt( (normal2[0] * normal2[0]) + 
	      (normal2[1] * normal2[1]) + 
	      (normal2[2] * normal2[2]) );

  normal2[0] = normal2[0] / mag;
  normal2[1] = normal2[1] / mag;
  normal2[2] = normal2[2] / mag;
mag = sqrt( (normal3[0] * normal3[0]) + 
	      (normal3[1] * normal3[1]) + 
	      (normal3[2] * normal3[2]) );

  normal3[0] = normal3[0] / mag;
  normal3[1] = normal3[1] / mag;
  normal3[2] = normal3[2] / mag;

  //set up the view vector values
  vx = -ptlights[j][0] + polygons->m[0][ i ];
  vy = -ptlights[j][1] + polygons->m[1][ i ];
  vz = -ptlights[j][2] + polygons->m[2][ i ];
  vx1 = -ptlights[j][0] + polygons->m[0][ i+1];
  vy1 = -ptlights[j][1] + polygons->m[1][ i+1 ];
  vz1 = -ptlights[j][2] + polygons->m[2][ i+1 ];
  vx2 = -ptlights[j][0] + polygons->m[0][ i+2 ];
  vy2 = -ptlights[j][1] + polygons->m[1][ i+2 ];
  vz2 = -ptlights[j][2] + polygons->m[2][ i+2 ];

  mag = sqrt( (vx * vx) + 
	      (vy * vy) + 
	      (vz * vz) );

  vx = vx / mag;
  vy = vy / mag;
  vz = vz / mag;

  mag = sqrt( (vx1 * vx1) + 
	      (vy1 * vy1) + 
	      (vz1 * vz1) );

  vx1 = vx1 / mag;
  vy1 = vy1 / mag;
  vz1 = vz1 / mag;

  mag = sqrt( (vx2 * vx2) + 
	      (vy2 * vy2) + 
	      (vz2 * vz2) );

  vx2 = vx2 / mag;
  vy2 = vy2 / mag;
  vz2 = vz2 / mag;

  //calculate the dot product
  dot = normal[0] * vx + normal[1] * vy + normal[2] * vz;
dot2 = normal2[0] * vx1 + normal2[1] * vy1 + normal2[2] * vz1;
dot3 = normal3[0] * vx2 + normal3[1] * vy2 + normal3[2] * vz2;
///////////////////////////////////
int ooo, www, rrr;
if (bbb == 0){
ooo = 0;
}
if (bbb == 1){
www = 0;
}
if (bbb == 2){
rrr = 0;
}
if (mmm == 0){
ooo = 1;
}
if (mmm == 1){
www = 1;
}
if (mmm == 2){
rrr = 1;
}
if (ttt == 0){
ooo = 2;
}
if (ttt == 1){
www = 2;
}
if (ttt == 2){
rrr = 2;
}


/////////////////////diffuse

if (cbmt[ooo].red + ptlights[j][3] * kdr * dot >= 255){
cbmt[ooo].red = 255;
}
else{
if (ptlights[j][3] * kdr * dot >= 0){
cbmt[ooo].red += ptlights[j][3] * kdr * dot;
}
}
if (cbmt[ooo].green + ptlights[j][4] * kdg * dot >= 255){
cbmt[ooo].green = 255;
}
else{
if (ptlights[j][4] * kdg * dot >= 0){
cbmt[ooo].green += ptlights[j][4] * kdg * dot;
}
}
if (cbmt[ooo].blue + ptlights[j][5] * kdb * dot >= 255){
cbmt[ooo].blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot >= 0){
cbmt[ooo].blue += ptlights[j][5] * kdb * dot;
}
}

if (cbmt[www].red + ptlights[j][3] * kdr * dot2 >= 255){
cbmt[www].red = 255;
}
else{
if (ptlights[j][3] * kdr * dot2 >= 0){
cbmt[www].red += ptlights[j][3] * kdr * dot2;
}
}
if (cbmt[www].green + ptlights[j][4] * kdg * dot2 >= 255){
cbmt[www].green = 255;
}
else{
if (ptlights[j][4] * kdg * dot2 >= 0){
cbmt[www].green += ptlights[j][4] * kdg * dot2;
}
}
if (cbmt[www].blue + ptlights[j][5] * kdb * dot2 >= 255){
cbmt[www].blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot2 >= 0){
cbmt[www].blue += ptlights[j][5] * kdb * dot2;
}
}


if (cbmt[rrr].red + ptlights[j][3] * kdr * dot3 >= 255){
cbmt[rrr].red = 255;
}
else{
if (ptlights[j][3] * kdr * dot3 >= 0){
cbmt[rrr].red += ptlights[j][3] * kdr * dot3;
}
}
if (cbmt[rrr].green + ptlights[j][4] * kdg * dot3 >= 255){
cbmt[rrr].green = 255;
}
else{
if (ptlights[j][4] * kdg * dot3 >= 0){
cbmt[rrr].green += ptlights[j][4] * kdg * dot3;
}
}
if (cbmt[rrr].blue + ptlights[j][5] * kdb * dot3 >= 255){
cbmt[rrr].blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot3 >= 0){
cbmt[rrr].blue += ptlights[j][5] * kdb * dot3;
}
}
ib.red = cbmt[0].red;
ib.green = cbmt[0].green;
ib.blue = cbmt[0].blue;
im.red = cbmt[1].red;
im.green = cbmt[1].green;
im.blue = cbmt[1].blue;
it.red = cbmt[2].red;
it.green = cbmt[2].green;
it.blue = cbmt[2].blue;
/////////////////////specular
normal[0]*= 2 * dot; 
normal[1]*= 2 * dot; 
normal[2]*= 2 * dot; 
	
normal[0]-= vx; 
normal[1]-= vy; 
normal[2]-= vz;

normal2[0]*= 2 * dot2; 
normal2[1]*= 2 * dot2; 
normal2[2]*= 2 * dot2; 
	
normal2[0]-= vx1; 
normal2[1]-= vy1; 
normal2[2]-= vz1;

normal3[0]*= 2 * dot3; 
normal3[1]*= 2 * dot3; 
normal3[2]*= 2 * dot3; 
	
normal3[0]-= vx2; 
normal3[1]-= vy2; 
normal3[2]-= vz2;

dot = -1 * normal[2];
dot2 = -1 * normal2[2];
dot3 = -1 * normal3[2];

if (cbmt[ooo].red + ptlights[j][3] * ksr * dot >= 255){
cbmt[ooo].red = 255;
}
else{
if (ptlights[j][3] * ksr * dot >= 0){
cbmt[ooo].red += ptlights[j][3] * ksr * dot;
}
}
if (cbmt[ooo].green + ptlights[j][4] * ksg * dot >= 255){
cbmt[ooo].green = 255;
}
else{
if (ptlights[j][4] * ksg * dot >= 0){
cbmt[ooo].green += ptlights[j][4] * ksg * dot;
}
}
if (cbmt[ooo].blue + ptlights[j][5] * ksb * dot >= 255){
cbmt[ooo].blue = 255;
}
else{
if (ptlights[j][5] * ksb * dot >= 0){
cbmt[ooo].blue += ptlights[j][5] * ksb * dot;
}
}

if (cbmt[www].red + ptlights[j][3] * ksr * dot2 >= 255){
cbmt[www].red = 255;
}
else{
if (ptlights[j][3] * ksr * dot2 >= 0){
cbmt[www].red += ptlights[j][3] * ksr * dot2;
}
}
if (cbmt[www].green + ptlights[j][4] * ksg * dot2 >= 255){
cbmt[www].green = 255;
}
else{
if (ptlights[j][4] * ksg * dot2 >= 0){
cbmt[www].green += ptlights[j][4] * ksg * dot2;
}
}
if (cbmt[www].blue + ptlights[j][5] * ksb * dot2 >= 255){
cbmt[www].blue = 255;
}
else{
if (ptlights[j][5] * ksb * dot2 >= 0){
cbmt[www].blue += ptlights[j][5] * ksb * dot2;
}
}


if (cbmt[rrr].red + ptlights[j][3] * ksr * dot3 >= 255){
cbmt[rrr].red = 255;
}
else{
if (ptlights[j][3] * ksr * dot3 >= 0){
cbmt[rrr].red += ptlights[j][3] * ksr * dot3;
}
}
if (cbmt[rrr].green + ptlights[j][4] * ksg * dot3 >= 255){
cbmt[rrr].green = 255;
}
else{
if (ptlights[j][4] * ksg * dot3 >= 0){
cbmt[rrr].green += ptlights[j][4] * ksg * dot3;
}
}
if (cbmt[rrr].blue + ptlights[j][5] * ksb * dot3 >= 255){
cbmt[rrr].blue = 255;
}
else{
if (ptlights[j][5] * ksb * dot3 >= 0){
cbmt[rrr].blue += ptlights[j][5] * ksb * dot3;
}
}
ib.red = cbmt[0].red;
ib.green = cbmt[0].green;
ib.blue = cbmt[0].blue;
im.red = cbmt[1].red;
im.green = cbmt[1].green;
im.blue = cbmt[1].blue;
it.red = cbmt[2].red;
it.green = cbmt[2].green;
it.blue = cbmt[2].blue;
  free( normal );
  free( normal2 );
  free( normal3 );
}
//*////////////////////////////////////////////////\\GOROUD(End of excludable portion)

double btcr, bmcr, mtcr;
double btcg, bmcg, mtcg;
double btcb, bmcb, mtcb;
if (t == b){
btcr = DBL_MAX;//9999999;//DBL_MAX;//
btcg = DBL_MAX;//9999999;//DBL_MAX;//
btcb = DBL_MAX;//9999999;//DBL_MAX;//
if (it.red < ib.red){
btcr = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.green < ib.green){
btcg = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.blue < ib.blue){
btcb = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
btcg = 1.0 * (it.green - ib.green)/(t - b);
btcr = 1.0 * (it.red - ib.red)/(t - b);
btcb = 1.0 * (it.blue - ib.blue)/(t - b);
}
if (m == b){
bmcr = DBL_MAX;//9999999;//DBL_MAX;//
bmcg = DBL_MAX;//9999999;//DBL_MAX;//
bmcb = DBL_MAX;//9999999;//DBL_MAX;//
if (im.red < ib.red){
bmcr = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (im.green < ib.green){
bmcg = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (im.blue < ib.blue){
bmcb = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bmcr = 1.0 * (im.red - ib.red)/(m - b);
bmcg = 1.0 * (im.green - ib.green)/(m - b);
bmcb = 1.0 * (im.blue - ib.blue)/(m - b);
}
if (t == m){
mtcr = DBL_MAX;//9999999;//DBL_MAX;//
mtcg = DBL_MAX;//9999999;//DBL_MAX;//
mtcb = DBL_MAX;//9999999;//DBL_MAX;//
if (it.red < im.red){
mtcr = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.green < im.green){
mtcg = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.blue < im.blue){
mtcb = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
mtcr = 1.0 * (it.red - im.red)/(t - m);
mtcg = 1.0 * (it.green - im.green)/(t - m);
mtcb = 1.0 * (it.blue - im.blue)/(t - m);
}
double mr2 = bmcr;
double mg2 = bmcg;
double mb2 = bmcb;
double drr, drr2, drr3;
double drg, drg2, drg3;
double drb, drb2, drb3;
drr2 = ib.red + 0;
drr = ib.red + 0;
drr3 = im.red + 0;
drg2 = ib.green + 0;
drg = ib.green + 0;
drg3 = im.green + 0;
drb2 = ib.blue + 0;
drb = ib.blue + 0;
drb3 = im.blue + 0;

//*///////////////////////////////////////\GOROUD


///*//////////////////////////////////////PHONG


//*///////////////////////////////////////PHONG
      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 s, c,
		polygons->m[2][i],
		 polygons->m[2][i+1]);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 s, c,
		polygons->m[2][i+1],
		 polygons->m[2][i+2]);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 s, c,
		polygons->m[2][i+2],
		 polygons->m[2][i]);

while(b < t){
if (abcdef == 0 && b >= m){
m2 = mt;
mr2 = mtcr;
mg2 = mtcg;
mb2 = mtcb;
mz2 = mtz;
drx2 = x2;
drr2 = im.red + 0;
drg2 = im.green + 0;
drb2 = im.blue + 0;
drx3 = x3;
drr2 = it.red + 0;
drg2 = it.green + 0;
drb2 = it.blue + 0;
drz2 = z2;
drz3 = z3;
abcdef++;
}
drx += bt;
drx2 += m2;
drr += btcr;
drg += btcg;
drb += btcb;
drr2 += mr2;
drg2 += mg2;
drb2 += mb2;
drz += btz;
drz2 += mz2;
if ((bt < 0 && drx < x3) || (bt >= 0 && drx > x3)){
drx = x3;
}
if ((m2 < 0 && drx2 < drx3) || (m2 >= 0 && drx2 > drx3)){
drx2 = drx3;
}///
if ((btcr < 0 && drr < it.red) || (btcr >= 0 && drr > it.red)){
drr = it.red + 0;
}
if ((mr2 < 0 && drr2 < drr3) || (mr2 >= 0 && drr2 > drr3)){
drr2 = drr3 + 0;
}//////
if ((btcg < 0 && drg < it.green) || (btcg >= 0 && drg > it.green)){
drg = it.green + 0;
}
if ((mg2 < 0 && drg2 < drg3) || (mg2 >= 0 && drg2 > drg3)){
drg2 = drg3 + 0;
}//////
if ((btcb < 0 && drb < it.blue) || (btcb >= 0 && drb > it.blue)){
drb = it.blue + 0;
}
if ((mb2 < 0 && drb2 < drb3) || (mb2 >= 0 && drb2 > drb3)){
drb2 = drb3 + 0;
}///
if ((btz < 0 && drz < z3) || (btz >= 0 && drz > z3)){
drz = z3 + 0;
}
if ((mz2 < 0 && drz2 < drz3) || (mz2 >= 0 && drz2 > drz3)){
drz2 = drz3 + 0;
}
//printf("drawing (%lf,%lf,%lf) to (%lf,%lf,%lf)\n",drx,b,drz,drx2,b,drz2);
//printf("\tbmt -> (%lf,%lf,%lf);(%lf,%lf,%lf);(%lf,%lf,%lf)\n",x1,b,z1,x2,m,z2,x3,t,z3);
cl.red = drr;
cl.green = drg;
cl.blue = drb;
cr.red = drr2;
cr.green = drg2;
cr.blue = drb2;
draw_line(drx,b,drx2,b,s,c, drz, drz2);
b++;
}
//printf("polygon colors:b%d,%d,%d;;        m%d,%d,%d;;             t%d,%d,%d;;\n\tl%d,%d,%d;;        r%d,%d,%d;;\n",ib.red,ib.green,ib.blue,im.red,im.green,im.blue,it.red,it.green,it.blue,cl.red,cl.green,cl.blue,cr.red,cr.green,cr.blue);
//printf("line colors:l%d,%d,%d;;        r%d,%d,%d;;\n",cl.red,cl.green,cl.blue,cr.red,cr.green,cr.blue);

    }
  }
setc = 0;
//printf("polygon colors:b%d,%d,%d;;        m%d,%d,%d;;             t%d,%d,%d;;\n\tl%d,%d,%d;;        r%d,%d,%d;;\n",ib.red,ib.green,ib.blue,im.red,im.green,im.blue,it.red,it.green,it.blue,cl.red,cl.green,cl.blue,cr.red,cr.green,cr.blue);
}
Exemplo n.º 19
0
Arquivo: draw.c Projeto: stuydw/final
void draw_polygons( struct matrix *polygons, screen s, color c ) {
  int i;
  double x1, x2, x3, y1, y2, y3, z1, z2 ,z3;
  double Bx, By, Bz, Mx, My, Mz, Tx, Ty, Tz;
  double d0, d1, y, Lx, Rx, dz0, dz1, Lz, Rz;
  double n; //normal
  if ( polygons->lastcol < 3 ) {
    printf("Need at least 3 points to draw a polygon!\n");
    return;
  }
  printf("\n\n");
  for( i=0; i < polygons->lastcol-2; i+=3 ) {
    if ( calculate_dot( polygons, i ) >= 0 ) {
      x1 = polygons->m[0][i];
      y1 = polygons->m[1][i];
      z1 = polygons->m[2][i];
      x2 = polygons->m[0][i+1];
      y2 = polygons->m[1][i+1];
      z2 = polygons->m[2][i+1];
      x3 = polygons->m[0][i+2];
      y3 = polygons->m[1][i+2];
      z3 = polygons->m[2][i+2];
      //Type 1 T != M != B
      if((y1 != y2) && (y2 != y3) && (y3 != y1)){
	//T = 1
	if((y1 > y2) && (y1 > y3)){
	  Tx = x1; Ty = y1; Tz = z1;
	  if(y2 > y3){
	    Mx = x2; My = y2; Mz = z2;
	    Bx = x3; By = y3; Bz = z3;
	  }
	  else{
	    Mx = x3; My = y3; Mz = z3;
	    Bx = x2; By = y2; Bz = z2;
	  }
	}
	//T = 2
	if((y2 > y1) && (y2 > y3)){
	  Tx = x2; Ty = y2; Tz = z2;
	  if(y1 > y3){
	    Mx = x1; My = y1; Mz = z1;
	    Bx = x3; By = y3; Bz = z3;
	  }
	  else{
	    Mx = x3; My = y3; Mz = z3;
	    Bx = x1; By = y1; Bz = z1;
	  }
	}
	//T = 3
	if((y3 > y2) && (y3 > y1)){
	  Tx = x3; Ty = y3; Tz = z3;
	  if(y2 > y1){
	    Mx = x2; My = y2; Mz = z2;
	    Bx = x1; By = y1; Bz = z1;
	  }
	  else{
	    Mx = x1; My = y1; Mz = z1;
	    Bx = x2; By = y2; Bz = z2;
	  }
	}
	d0 = ((Bx - Tx)/(By - Ty));
	d1 = ((Bx - Mx)/(By - My));
	dz0 = ((Bz - Tz)/(By - Ty));
	dz1 = ((Bz - Mz)/(By - My));
	y = By;
	while(y <= My){
	  Lx = Bx + (d0 * (y - By));
	  Rx = Bx + (d1 * (y - By));
	  Lz = Bz + (dz0 * (y - By));
	  Rz = Bz + (dz0 * (y - By));
	  draw_line(Lx, y, Rx, y, s, flat(Lx, y, Lz, Rx, y, Rz, c));
	  y++;
	}
	d1 = ((Mx - Tx)/(My - Ty));
	dz1 = ((Mz - Tz)/(My - Ty));
	while(y <= Ty){
	  Lx = Mx + (d0 * (y - My));
	  Rx = Mx + (d1 * (y - My));
	  Lz = Mz + (dz0 * (y - My));
	  Rz = Mz + (dz1 * (y - My));
	  draw_line(Lx, y, Rx, y, s, flat(Lx, y, Lz, Rx, y, Rz, c));
	  y++;
	}
      }
      //Case 2
      else{
	//1 & 2 are M & B        T = 3
	if((y1 == y2) && (y1 < y3)){
	  Tx = x3; Ty = y3; Tz = z3;
	  if(x1 < x2){
	    Bx = x1; By = y1; Bz = z1;
	    Mx = x2; My = y2; Mz = z2;
	  }
	  else{
	    Bx = x2; By = y2; Bz = z2;
	    Mx = x1; My = y1; Mz = z1;
	  }
	}
	else{
	  //2 & 3 are M & B       T = 1
	  if((y3 == y2) && (y1 > y3)){
	    Tx = x1; Ty = y1; Tz = z1;
	    if(x3 < x2){
	      Bx = x3; By = y3; Bz = z3;
	      Mx = x2; My = y2; Mz = z2;
	    }
	    else{
	      Bx = x2; By = y2; Bz = z2;
	      Mx = x3; My = y3; Mz = z3;
	    }
	  }
	  //1 & 3 are M & B       T = 2
	  if((y1 == y3) && (y1 < y2)){
	    Tx = x2; Ty = y2; Tz = z2;
	    if(x1 < x3){
	      Bx = x1; By = y1; Bz = z1;
	      Mx = x3; My = y3; Mz = z3;
	    }
	    else{
	      Bx = x3; By = y3; Bz = z3;
	      Mx = x1; My = y1; Mz = z1;
	    }
	  }
	}
	d0 = ((Bx-Tx)/(By-Ty));
	d1 = ((Mx-Tx)/(My-Ty));
	dz0 = ((Bz-Tz)/(By-Ty));
	dz1 = (Mz-Tx)/(My-Ty));
	y = By;
	while(y <= Ty){
	  Lx = Bx + (d0 * (y - By));
	  Rx = Bx + (d1 * (y - By));
	  Lz = Bz + (dz0 * (y - By));
	  Rz  = Bz + (dz1 * (y - By));
	  draw_line(Lx, y, Rx, y, s, flat(Lx, y, Lz, Rx, y, Rz, c));
	  y++;
	}
      }
      //Case 3
      //1 & 2 are M & T        B = 3
      if((y1 == y2) && (y1 > y3)){
	Bx = x3; By = y3; Bz = z3;
	if(x1 > x2){
	  Tx = x1; Ty = y1; Tz = z1;
	  Mx = x2; My = y2; Mz = z2;
	}
	else{
	  Tx = x2; Ty = y2; Tz = z2;
	  Mx = x1; My = y1; Mz = z1;
	}
      }
      else{
	//2 & 3 are M & T       B = 1
	if((y3 == y2) && (y1 < y3)){
	  Bx = x1; By = y1; Bz = z1;
	  if(x3 > x2){
	    Tx = x3; Ty = y3; Tz = z3;
	    Mx = x2; My = y2; Mz = z2;
	  }
	  else{
	    Tx = x2; Ty = y2; Tz = z2;
	    Mx = x3; My = y3; Mz = z3;
	  }
	}
	//1 & 3 are M & T       B = 2
	if((y1 == y3) && (y1 > y2)){
	  Bx = x2; By = y2; Bz = z2;
	  if(x1 > x3){
	    Tx = x1; Ty = y1; Tz = z1;
	    Mx = x3; My = y3; Mz = z3;
	  }
	  else{
	    Tx = x3; Ty = y3; Tz = z3;
	    Mx = x1; My = y1; Mz = z1;
	  }
	}
      }
      d0 = ((Bx - Tx)/(By - Ty));
      d1 = ((Bx - Mx)/(By - My));
      dz0 = ((Bz - Tz)/(By - Ty));
      dz1 = ((Bz - Mz)/(By - My));
      y = By;
      while(y <= Ty){
	Lx = Bx + (d0 * (y - By));
	Rx = Bx + (d1 * (y - By));
	Lz = Bz + (dz0 * (y - By));
	Rz = Bz + (dz1 * (y - By));
	draw_line(Lx, y, Rx, y, s, flat(Lx, y, Lz, Rx, y, Rz, c));
	y++;
      }
    }
  }
Exemplo n.º 20
0
/*======== void draw_polygons() ==========
  Inputs:   struct matrix *polygons
  screen s
  color c  
  Returns: 
  Goes through polygons 3 points at a time, drawing 
  lines connecting each points to create bounding
  triangles

  04/16/13 13:13:27
  jdyrlandweaver
  ====================*/
void draw_polygons( struct matrix *polygons, screen s, color c, struct constants K, struct light * lights, int num_lights ) {

  int i;

  for( i=0; i < polygons->lastcol-2; i+=3 ) {
    c.red = 0; c.green = 0; c.blue = 0;

    double xB=0,yB=0,zB=0,xT=0,yT=0,zT=0,xM=0,yM=0,zM=0; 
    double Mxy1=0, Mzy1=0, Mxy2=0, Mzy2=0, Mxy3=0, Mzy3=0; // slopes of x and z w.r.t y
    /* M1 is between B and T, M2 is between B and M, M3 is between M and T */
    double x0=0,y0=0,z0=0,x1=0,z1=0;

    if ( calculate_dot( polygons, i ) < 0 ) {
      if(polygons->m[1][i] <= polygons->m[1][i+1]){
	if(polygons->m[1][i+2] >= polygons->m[1][i+1]){
	  xB = polygons->m[0][i]; yB = polygons->m[1][i]; zB = polygons->m[2][i];
	  xM = polygons->m[0][i+1]; yM = polygons->m[1][i+1]; zM = polygons->m[2][i+1];
	  xT = polygons->m[0][i+2]; yT = polygons->m[1][i+2]; zT = polygons->m[2][i+2];
	}else if(polygons->m[1][i+2] >= polygons->m[1][i]){
	  xB = polygons->m[0][i]; yB = polygons->m[1][i]; zB = polygons->m[2][i];
	  xT = polygons->m[0][i+1]; yT = polygons->m[1][i+1]; zT = polygons->m[2][i+1];
	  xM = polygons->m[0][i+2]; yM = polygons->m[1][i+2]; zM = polygons->m[2][i+2];	  
	}else{	  
	  xM = polygons->m[0][i]; yM = polygons->m[1][i]; zM = polygons->m[2][i];
	  xT = polygons->m[0][i+1]; yT = polygons->m[1][i+1]; zT = polygons->m[2][i+1];
	  xB = polygons->m[0][i+2]; yB = polygons->m[1][i+2]; zB = polygons->m[2][i+2];	  
	}
      }else{
	if(polygons->m[1][i+2] >= polygons->m[1][i]){
	  xM = polygons->m[0][i]; yM = polygons->m[1][i]; zM = polygons->m[2][i];
	  xB = polygons->m[0][i+1]; yB = polygons->m[1][i+1]; zB = polygons->m[2][i+1];
	  xT = polygons->m[0][i+2]; yT = polygons->m[1][i+2]; zT = polygons->m[2][i+2];
	}else if(polygons->m[1][i+2] >= polygons->m[1][i+1]){
	  xT = polygons->m[0][i]; yT = polygons->m[1][i]; zT = polygons->m[2][i];
	  xB = polygons->m[0][i+1]; yB = polygons->m[1][i+1]; zB = polygons->m[2][i+1];
	  xM = polygons->m[0][i+2]; yM = polygons->m[1][i+2]; zM = polygons->m[2][i+2];	  
	}else{	  
	  xT = polygons->m[0][i]; yT = polygons->m[1][i]; zT = polygons->m[2][i];
	  xM = polygons->m[0][i+1]; yM = polygons->m[1][i+1]; zM = polygons->m[2][i+1];
	  xB = polygons->m[0][i+2]; yB = polygons->m[1][i+2]; zB = polygons->m[2][i+2];	  
	}
      }

      //printf("%f, %f, %f, %f, %f, %f, %f, %f, %f\n",xB,yB,zB,xM,yM,zM,xT,yT,zT);
      
      if (yB != yT){
	Mxy1 = (xB - xT) / (yB - yT);
	Mzy1 = (zB - zT) / (yB - yT);
      }
      if (yB != yM){
	Mxy2 = (xB - xM) / (yB - yM);
	Mzy2 = (zB - zM) / (yB - yM);
      }
      if (yM != yT){
	Mxy3 = (xM - xT) / (yM - yT);
	Mzy3 = (zM - zT) / (yM - yT);
      }

      c.red += lights[0].c[RED]*K.r[amb];
      c.green += lights[0].c[GREEN]*K.g[amb];
      c.blue += lights[0].c[BLUE]*K.b[amb];


      // we run x0 along B->T
      x0 = xB; y0 = yB; z0 = zB;
      x1 = xB; z1 = zB;
      while(y0 <= yT){
	if (y0 >= yM){
	  x1 = xM + Mxy3 * (y0 - yM);
	  z1 = zM + Mzy3 * (y0 - yM);
	}
	draw_horz_line(ceil(x0),floor(x1),y0,z0,z1,s,c);
	x0 += Mxy1; y0 += 1; z0 += Mzy1;
	if(y0 < yM){
	  x1 += Mxy2; z1 += Mzy2;
	}else{
	  x1 += Mxy3; z1 += Mzy3;
	}
      }     
    }
  }
}