Archived

This topic is now archived and is closed to further replies.

Chaoslab

Help turning my tri filler into a textured tri filler

Recommended Posts

I have coded a triangle filler (in java) and now would like to add texturing. I am kind of a little lost on where to go next. Heres the text for the tri filler.... (data is the pixel array, and xsize / ysize are it''s dimensions)


 public void fill_triangle(int xi1, int yi1, int xi2, int yi2, int xi3, int yi3, int color)
 {
  double x0 = xi1;
  double x1 = xi2;
  double x2 = xi3;

  double y0 = yi1;
  double y1 = yi2;
  double y2 = yi3;
  
  double d0  = 0;	
  double d1  = 0;	
  double d2  = 0;	
  
  double dx0 = 0;	
  double dx1 = 0;	
  double dx2 = 0;	
  
  double dy0 = 0;	
  double dy1 = 0;	
  double dy2 = 0;	
  
  double dt;
  
  int i;

  if(y1 < y0) { dt = y0; y0 = y1; y1 = dt; dt = x0; x0 = x1; x1 = dt;}
  if(y2 < y1) { dt = y1; y1 = y2; y2 = dt; dt = x1; x1 = x2; x2 = dt;}
  if(y2 < y0) { dt = y0; y0 = y2; y2 = dt; dt = x0; x0 = x2; x2 = dt;}

  dx0 = x1 - x0;
  dy0 = y1 - y0;

  dx1 = x2 - x1;
  dy1 = y2 - y1;
  
  dx2 = x0 - x2;
  dy2 = y0 - y2;

  if(dy0 != 0) d0 = dx0 / dy0;
  else         d0 = 0;
  
  if(dy1 != 0) d1 = dx1 / dy1;
  else         d1 = 0;
  
  if(dy2 != 0) d2 = dx2 / dy2;
  else         d2 = 0;
  
  int sx,ex;
  
  double id;
  
  qplot_color = color;
  
  for(i = (int) y0; i < y1; i++)
  {
   id = (double) i;
	  
   sx = (int) (x0 + ((id - y0) * d2));
   ex = (int) (x0 + ((id - y0) * d0));
  
   if(sx < ex) qscan(sx,i,ex - sx);
   else        qscan(ex,i,sx - ex);
  }
  
  for(i = (int) y1; i < y2; i++)
  {
   sx = (int) (x0 + ((i - y0) * d2));
   ex = (int) (x1 + ((i - y1) * d1));
	  
   if(sx < ex) qscan(sx,i,ex - sx);
   else        qscan(ex,i,sx - ex);
  }
 }

 private void qscan(int x, int y, int size)
 { 
  // clip y
  if(y > 0 && y < ysize && x < xsize)
  {
   // clip left hand side of scan
   if(x < 0)
   {
    size += x;
    x     = 0;
   }
   
   y = y * xsize + x;
   
   if(x + size < xsize) x = y + size;
   else                 x = y + size - (x + size - xsize);
   
   for(; y < x; y++)
    data[y] = qplot_color;
	  
  }
 }


 
So any tips, optimizations etc.... Please.... :-)

Share this post


Link to post
Share on other sites