Skip to main content
GameDev.net gamedev.net
🔒 Locked

Fill Triangle routine

Started by Austrian Coder Jul 13, 2004 at 5:50 PM 6 replies 16.6k views
Original Post
Austrian Coder
Austrian Coder
Hi community. I have only the function DrawPixle(x,y,color) and now need a nice algorithm to draw a filled tringle. The function must look like this: void FillTriangle(x1,y1,x2,y2,x3,y3,color) I hope you can help me. Christian
Charles B
Charles B
Google "software renderer code sample"
I am sure you'll be able to select the best copy/paste for your needs.

Pretty trivial algo anyway ;)
"Coding math tricks in asm is more fun than Java"
rviney
rviney
There is code for sub-pixel accurate scanline conversion in Chris Hecker's Perspective Texture Mapping articles. You should be able to find it easily on Google.

- Richard
Chaoslab
Chaoslab
Here's my triangle solution

 // screen pixels and size vars int data[]; int xsize; int ysize; /** fill 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 dx[] = {0,0,0};  double dy[] = {0,0,0};  double d[]  = {0,0,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 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
valg3
valg3
Try this, its a small site but helpful for triangle fillers:
http://www-users.mat.uni.torun.pl/~wrona/3d_tutor/tri_fillers.html
v71
v71
i have an optimized software rasterizer i use for my occlusion culling engine, unfortunatley i can't give or explain because i am unhelpfull, uneducated AND unfriendly.
Rate me

Topic Locked

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

Sign in to reply to this topic.