Triangle Fast Scan-Conversion Algorithm Report Jian Cui Jim X. Chen Xiaorong Zhou Computer Science Department George Mason University Fairfax, Virginia, USA Computer Science Department George Mason University Fairfax, Virginia, USA Computer Science Department George Mason University Fairfax, Virginia, USA [email protected] [email protected] [email protected] ABSTRACT To date, very little has been done to analyze the very importance of the distribution of the shape and size of triangles in the real world. We statistically analyzed various collected graphics application files and got the conclusion that most of the triangles scan-converted onto the screen are rather smaller ones. We thus come up with a new small triangle scan-conversion algorithm which fully takes this feature into consideration. This report gives detailed description of a new small triangle scan-conversion algorithm. It gives various experiments performed based on the new algorithm and the anlysis of the performance of the algorithm. 1 2 3 Keywords Small triangle, scan-conversion, algorithm. 1. Figure 1: Edge walking algorithm. INTRODUCTION Many methods have been proposed to speed up the process of scan-converting triangles onto the raster plane. However, most of them neglect the fact that triangles are not randomly distributed across different sizes and shapes. We collected various graphics applications and analyzed over 1.4 million triangles drawn by these applications. The conclusion of the analysis testifies that most triangles are small ones. More specifically, almost 95% of triangles can be held in a 40 × 31 rectangle area. Figure 1 shows an example of the edge walking algorithm. In the edge walking algorithm, it first sorts the vertices in both x and y values. Then it determines if the middle vertex (or breakpoint) lies on the left or right side of the triangle. If the triangle has an edge parallel to the scanline direction then there is no breakpoint. It then determines the left and right edge for scanline (or called spans). The algorithm will walk down the left and right edges filling the pixels in-between until either a breakpoint or the bottom vertex is reached. We thus come up with the new algorithm to speed up the process of drawing small triangles and compare the performance of our algorithm with the existing triangle scanconverting algorithm. The edge walking algorithm generally is very fast. But it also has its own disadvantages. First, it is loaded with special cases, such as left and right breakpoints, or even no breakpoints. Second, because of the various cases, it is difficult to get it right when implementing it. Third, it requires computing fractional offsets when interpolating parameters across the triangle. 2. PREVIOUS ALGORITHMS There exists various algorithms to scan-convert triangles. Edge-walking and edge-equation are the two most popular algorithms. 2.1 Edge Walking Algorithm 2.2 Edge Equation Algorithm As shown in figure 2, the edge equation algorithm first computes the edge equations from the three vertices. Then it orients the edge equations so that their positive-half spaces are in the triangle’s interior. From the edge equations, it will get a bounding box. The algorithm then scan through pixel (x, y) in the bounding box and evaluate the edge equations. When all three equations with the same (x, y) are positive, it will draw the pixel. 3. FAST SMALL TRIANGLE SCAN-CONVERSION ALGORITHM Triangle 1 1 A0x+B0y+c 1 2 A2x+B2y+c + + 2 3 1 4 5 6 7 8 9 10 11 I Triangle 3 2 _ 3 4 5 II _+ III Triangle 2 A1x+B1y+c 3 Figure 2: Edge equation algorithm. Figure 3: Rectangle (w=11, h=5) and the triangles inside it. 1 3.1 Basic Idea The basic idea of our algorithm fully incorporates our statistics analysis result. We use an w × h rectangle area as a boundary. All the triangles which can be held inside the boundary are regarded as small triangle and will be scanconverted with our new algorithm. The ones beyond this boundary will be using the traditional scan-conversion algorithm. In our algorithm, we generate a pixel matrix array. Each matrix of the array which is in the size of (w × h) is used to save the pixel image of a small triangle. In a predefined order, the array contains all the possible triangles that can be held inside this rectangle area. According to the same order, we generate an index method for the matrix array. When a program calls to draw a small triangle which is within the rectangle area onto the screen, we can use the index method to locate the position of the triangle in the matrix array and scan-convert the pixel matrix(pixel image of the triangle) directly into the frame buffer. 3.2 The Pixel Matrix Array In our algorithm, first we need to generate a pixel matrix array which contains all possible small triangles that fall into a specific w × h rectangle area. When generating this array, we need to make sure: • The pixel matrix array does cover all the possible small triangles within the given area. • The pixel matrices stored in the array should follow certain order so that we can generate efficient index on the array. 3.2.1 All Possible Triangles Inside a Rectangle Area 3 ................. _ 2 represent position fixed vertex. represent moving vertex. 1, 2, 3 represent the three vertices of a triangle. Figure 4: Situation 1. And for each of those rectangles, the algorithm will calculate the number of triangles that fit in these rectangles exactly. We use 1, 2, 3 to represent the three vertices of a triangle. When we consider a triangle that can be held exactly by a w × h rectangle area, there are five possible situations we need to consider. In situation 1(as shown in Figure 4), we have vertex 1 and vertex 2 fixed at the upper-left and bottom-right corner respectively. Vertex 3 can locate at any position other than these two places. And we make the assumption that vertex 3 will move from left to right, top to bottom. In situation 2(as shown in Figure 5), vertex 1 still locates at the upper-left corner. But now vertex 2 and vertex 3 moves along the bottom and right edges of the rectangle respectively, except the pixel marked with a ×. Situation 3 actually contains two possibilities(as shown in 1 A w × h rectangle area can hold all triangles with their widths and heights equal to or less than w and h respectively. Figure 3 shows a 11 × 5 rectangle area and several triangles it can hold. 3 2 From the figure, we notice for each triangle falls inside the 11×5 rectangle area, there exists a rectangle that can hold it exactly. For example, triangle 1, 2 and 3 in Figure 3 can be held exactly by rectangle I, II and III. So when we calculate the pixel matrix array for a specific w × h rectangle area, the algorithm will first pick out all the rectangles with their width and height less than or equal to w and h respectively. × represent position fixed vertex. represent moving vertex. × represent a position that cannot be occupied by any vertex. 1, 2, 3 represent the three vertices of a triangle. Figure 5: Situation 2. × 1 × 1 ........... 3 2 3 2 represent position fixed vertex. represent position fixed vertex. represent moving vertex. × represent a position that cannot be occupied by any vertex. 1, 2, 3 represent the three vertices of a triangle. Situation 3(a) × 1 × 3 × 2 represent position fixed vertex. represent moving vertex. × represent a position that cannot be occupied by any vertex. 1, 2, 3 represent the three vertices of a triangle. Situation 3(b) Figure 6: Situation 3. × Figure 8: Situation 5. With these 5 situations, we’ve covered all the possible triangles within a certain rectangle area. 3.2.2 Order of The Pixel Matrix in The Array When generating a pixel matrix array for a rectangle area with certain width and height, not only do we need to cover all the possiblities of the triangles inside the rectangle area, we also need to generate these matrix in a certain order, so we can later make a simple and clear index of all these matrices. Suppose we want to generate a pixel matrix array for a rectangle area with width w and height h. 1 2 × represent moving vertex. 1, 2, 3 represent the three vertices of a triangle. 3 represent position fixed vertex. represent moving vertex. × represent a position that cannot be occupied by any vertex. 1, 2, 3 represent the three vertices of a triangle. Figure 7: Situation 4. Figure 6). In both possiblities, vertex 1 will move along the upper edge in the rectangle area, except the upper-leftmost and upper-rightmost pixels. In situation (a), vertex 3 is fixed at the bottom-rightmost corner of the rectangle and vertex 2 will roam along the left edge of the rectangle except the upper-leftmost pixel. In situation (b), vertex 2 will be stationary at the bottom-leftmost corner and vertex 3 will travel along the right edge of the rectangle area, except for the two end pixels of the edge. In situation 4(as shown in Figure 7), vertex 1 is fixed at the upper-rightmost corner of the rectangle. Vertex 2 moves along the left edge except for the two end pixels. Vertex 3 moves along the bottom edge except for the bottom-leftmost pixel. In situation 5(as shown in Figure 8), both vertex 1 and vertex 2 are stationary at the upper-rightmost and bottomleftmost positions respectively. Vertex 3 can locate at any place except the first row and the pixel that has already been occupied by vertex 2. Figure 9 shows the pseudo code of the algorithm to generate the pixel matrix array. For each rectangle area with certain width and height, it generates the pixel matrices of all the possible triangles in five situations accordingly. 4. IMPLEMENTATION AND PERFORMANCE ALYSIS OF THE ALGORITHM 4.1 Implementation of The Algorithm The implementation of the algorithm is rather straight forward. Based on the nature of the algorithm, we first generate the pixel matrix array of a given rectangle with the width w and height h, using the idea shown in Figure 9. We also generate an index matrix I which has the exact same size as the rectangle area. Each value I[i][j] in the index matrix will represent how many triangles have been stored before this i × j rectangle. When we execute the algorithm, we first read in the pixel matrix array and the index matrix. We then use the same data files as we used to do the statistics analysis as the input file to test the algorithm. Each time when we read in three vertices values of a triangle, we use the index file to locate the triangle in the matrix array. We then directly scan-convert the pixel matrix into the frame buffer. 4.2 Test data set We performed several experiments to compare the performance between our algorithm and the traditional edge equation algorithm. For each experiment, we use several different input test data sets. Table 1: Our algorithm VS Edge equation algorithm. Algorithm Used Data Set I Data Set II Our algorithm 7280 4605 Edge equation algo. 7720 2357 for (i=1; i<=h; i++) for (j=1; j<=w; j++) { // for each pair of (i,j) value, generate all the // possible situation of triangles from situation // 1 to situation 5 accordingly. // situation 1 for (k=1; k<=h; k++) for (m=1; m<=w; m++) if (!(k==1&&l==1 || k==w&&l==h)) // draw a matrix with vertices 1, 2 and 3 // equal to (1,1), (w,h) and (k,m) // respectively. // situation 2 for (k=1; k<h; k++) for (m=1; m<w; m++) // draw a matrix with vertices 1, 2 and 3 // equal to (1,1), (m,h) and (w,k) // respectively. // situation 3 for (k=2; k<w; k++) { // situation 3(a) for (m=2; m<=h; m++) // draw a matrix with vertices 1, 2 and 3 // equal to (k,1), (1,m) and (w,h) // respectively. // situation 3(b) for (m=1; m<h; m++) // draw a matrix with vertices 1, 2 and 3 // equal to (k,1), (1,h) and (w,m) // respectively. } // situation 4 for (k=2; k<h; k++) for (m=2; m<=w; m++) // draw a matrix with vertices 1, 2 and 3 // equal to (w,1), (1,k) and (m,h) // respectively. // situation 5 for (k=2; k<=h; k++) for (m=1; m<=w; m++) if (!(m==1&&k==h) // draw a matrix with vertices 1, 2 and 3 // equal to (w,1), (1,h) and (m,k) // respectively. } Figure 9: Pseudo code for generating the pixel matrix array. The first test data set is merely the small triangles that are automatically generated using the psudo code shown in Figure 9 within the specific rectangle area. For a 11×5 rectangle area, there will be 3225 triangles. Along with the pixel matrix array of the triangles, we also generate the coordinates values of the three vertices of the triangles. Because this data set is rather small compared to the other two data set, we repeat it 1000 times and generate a larger data set. The second test data set contains all the small triangles of all the graphics applications we’ve collected over the internet. From our statistics result, we found out that a 11×5 rectangle area can hold 67.37% triangles of all 1,414,858 triangles. This data set will contain 953226 triangles. All the experiments are done on a Pentium III 1GHz PC with 384M memory. The results are shown in Table 1. 4.3 Out algorithm VS Edge Equation Algorithm We compared our algorithm with the edge equation algorithm. We run each test data set 20 times and use the average as the result. The result is shown in Table 1. All results are counted in millisecond (ms). 5. CONCLUSIONS It is rather disappointing to see that our algorithm can’t beat OpenGL, or even the edge equation algorithm. We analyze our code and find out two possible reasons reasulting in this rather disappointing performance. • Our indexing algorithm is too complicated. As you may see, each triangle has three vertices, and each vertex can locate at either the hightest, lowest, or in the middle left or middle right. Not to mention two vertices can locate at the same horizontal line or ever all the three vertices will form a horizontal line. With these many situations to consider, and with our certain ways of generating the matrix array, it makes the indexing algorithm really complicate. • The way we implemented the algorithm may cause the performance of the algorithm to suffer. We store the pixel matrix of each triangle as a small bitmap. After we retrieve the bitmap of a small triangle, we use OpenGL’s glBitmap function to directly send the bitmap of the small triangle to the framebuffer. As we were doing our experiments, we noticed that several articles on the internet had pointed out that glBitmap is a rather slow function and its performance relies on hardware, especially the video card, very much. In the near future, we will try to solve the above two problems. We will first try to come up with a new pixel ma- trix array generating algorithm and new indexing algorithm. The pixel matrix array generating algorithm we use now is complete. It has been minimized so it doesn’t have any redundancy or duplications in the pixel matrix array. But this also makes it difficult to understand and makes our indexing algorithm become complicate. We want to think other ways to allow some redundancy or duplication, but will get much better performance. Then we want to try other ways of implementing the algorithm. For example, we noticed that OpenGL’s texture mapping functions are much faster than its bitmap functions. We will try those options and choose the one that suits our algorithm best.
© Copyright 2026 Paperzz