Skip to main content
gamedev.net
🔒 Locked

[java] Triangle filler source....

Started by Chaoslab 0 replies 1.1k views
Original Post
Chaoslab
Chaoslab
I have noticed that some of people have been asking in here and in the graphics forum about traingle rasterizers so heres mine! The clipping is pretty lazy though! (if the scan line is in the display) so any suggestions there are welcome. I also want to get round to adding texturing to it so any help would be appreciate there as well. But it does work nicely for flat shaded triangles. And any suggestions on optimization, please....

(what ever the ruddy code tags are!?)



 // NOTE: that the pixel buffer is the variable data[] and xsize & ysize are the screen sizes

 /* fill a triangle into the pixel buffer
  *
  * @param xi1 - x postion of point 1 of traingle
  * @param yi1 - y postion of point 1 of traingle
  * @param xi2 - x postion of point 2 of traingle
  * @param yi2 - y postion of point 2 of traingle
  * @param xi3 - x postion of point 3 of traingle
  * @param yi3 - y postion of point 3 of traingle
  * @param color - color of triangle
  *
  */
 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);
  }
  
 }





 /** do quick scan
  * 
  *  @param x    x
  *  @param y    y
  *  @param size size of scan
  * 
  */
 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;
	  
  }
 }




 
ujhkfkfk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.