fast software triangle drawing

Started by
1 comment, last by Royale00 22 years, 2 months ago
Hi I have written a pretty simple software based 3d engine for the Game Boy Advanced. My problem is that my code is based on floating point numbers and the GBA only supports floating point numbers through software emulation which means its really slow. I wanterd to know if someone code provide code that draws triangles and is software based or modify the code I will provide below and make it use fixed point math or however. Thanks in advanced here it is: void Draw_Triangle(int x1, int y1, int x2, int y2, int x3, int y3,int color) { // this function draws a filled triangle // by dividing it in half and stepping through // each line ///// int x, y, //looping variables height; // the heights of the triangles float dx_right, // the dx/dy ratio of the right edge of the line dx_left, // the dx/dy ratio of the left edge of the line xs, xe; // the starting and ending points of the edges int right_x, left_x; // make sure points are in order int temp_x, temp_y; if (y2 < y1) { temp_y = y1; temp_x = x1; y1 = y2; x1 = x2; y2 = temp_y; x2 = temp_x; } if (y3 < y1) { temp_y = y1; temp_x = x1; y1 = y3; x1 = x3; y3 = temp_y; x3 = temp_x; } if (y3 < y2) { temp_y = y3; temp_x = x3; y3 = y2; x3 = x2; y2 = temp_y; x2 = temp_x; } left_x = x2; right_x = x1 + (int)((float)(y2-y1)*(float)(x3-x1)/(float)(y3-y1)); if (right_x < left_x) // messes up if right is on left { temp_x = right_x; right_x = left_x; left_x = temp_x; } /////!!DRAW TOP!!/////// //draw the triangle top if (y1 != y2) { // compute height of subtriangle height = y2 - y1; // set starting points xs = (float)x1; xe = (float)x1; // compute edge ratios dx_left = (float)(left_x - x1) / (float)height; dx_right = (float)(right_x - x1) / (float)height; ///DRAW IT ALREADY!!!! for (y = y1; y <= y2; y++) { for (x = (int)xs; x <= xe; x++) { DrawPixel5(x,y,color); } //adjust starting and ending point xs += dx_left; xe += dx_right; } } if (y2 != y3) { //now recompute slope of shorter edge to finish triangle bottom // recompute slopes height = y3 - y2; dx_right = (float)(x3 - right_x) / (float)(height); dx_left = (float)(x3 - left_x) / (float)(height); xs = (float)left_x; xe = (float)right_x; // draw the rest of the triangle for (y = y2; y <= y3; y++) { for (x = (int)xs; x <= xe; x++) { DrawPixel5(x,y,color); } //adjust starting and ending point xs += dx_left; xe += dx_right; } //end for } }
Advertisement
http://www.flipcode.com/tutorials/tut_toofast02.shtml
I would suggest replacing the
  for (x = (int)xs; x <= xe; x++) {   DrawPixel5(x,y,color);}   


with soem sort of HorLine method. I''m not sure how GBA has its video memory organized, in mode 13h on PC a fill (I think its memset in c) command could be used to fill a horizontal line with a single color. If there is an existing box/bar command that draws a filled box on the screen you could use that: bar((int)xs,y,(int)xe,y,color)

This topic is closed to new replies.

Advertisement