This application claims the benefit of Chinese Patent Application No. 200810146771.6, filed on Aug. 29, 2008 in the Chinese Intellectual Property Office, the disclosure of which is incorporated herein by reference.
The invention relates generally to a method and apparatus for displaying an image. More specifically, the invention relates to a method and apparatus for triangle traversal process in graphic rasterization.
The graphic rasterization is the procedure to convert the geometry primitives (line, triangle, polygon . . . ) into pixels for transmission to raster graphics displays. Triangle is the most important primitive in graphic rasterization for its useful properties: always convex, always reside in a simple plane and easy to create complex two-dimension primitives. The “triangle traversal” is the procedure to find all the pixels that are inside the triangle.
Edge functions are the fundamental to rasterizing triangle in hardware. The “edge functions” are three line functions of the three edges of a triangle, which are formed by the three vertices of triangle. The pixel inside the triangle has the property that three edge function values at this pixel are all positive or all negative. So edge functions are useful tool to decide whether a pixel is inside a triangle.
Many triangle traversal methods have been proposed based on edge functions. For example: Bounding box traversal, Backtrack traversal, and Zigzag traversal.
Above traversal algorithms are all scan-line based traversal methods. The basic idea of these is to create a bounding box of triangle and scan the bounding box following some orders.
In one aspect of the invention, a method for traversing a triangle in graphic rasterization is provided. The method comprises steps: creating a bounding box which is the smallest rectangle consisting of pixels that covers the triangle; choosing a traversal start pixel for traversing the triangle based on the bounding box; choosing a traversal direction based on the position relation between the traversal start pixel and the triangle; and traversing the triangle from the traversal start pixel in the traversal direction. The step for traversing the triangle comprises a step for calculating a next scan start pixel based on slope of a edge function of the triangle with respect to the traversal start pixel and the traversal direction.
In another aspect of the invention, an apparatus for traversing a triangle in graphic rasterization is provided. The apparatus comprises: a setup block 104 that receives vertex coordinates of the triangle, calculates coefficients of edge functions of the triangle by using the vertex coordinates, creates a bounding box of the triangle, chooses a traversal start pixel and a traversal direction; and a traverse block 105 that traverses the triangle based on the coefficients of edge functions, the traversal start pixel and the traversal direction. The traverse block 104 also calculates a next scan start pixel based on slope of a edge function of the triangle with respect to the traversal start pixel and the traversal direction.
The method and apparatus of the invention implement very fast and effective triangle traversal by not accessing pixels outside the triangle as much as possible in the traversal.
a) and (b) show the traversal directions each chosen in one of two different cases;
In the following description of the preferred embodiments, for the purpose of explanation, numerous specific details are set forth in order to provide a thorough understanding of the present invention. It will be apparent, however, to one skilled in the art that the present invention may be practiced without these specific details.
The efficiency of a triangle traversal method mainly depends on the redundant cost in visiting pixels that outside the triangle. Therefore, the objective of the presented invention is not to visit the pixels outside the triangle as much as possible.
Firstly, the vertex information (object coordinate) is read from vertex memory 101 into the transform block 102. The transform block 102 then transforms the vertex coordinates and the project block 103 projects the transformed vertex coordinates into device coordinates. The setup block 104 and traverse block 105 use the device coordinates to calculate which pixels are inside the triangle and calculate the parameters (for example, colour/texture) of these pixels. Then, the parameters are written into the frame buffer 106. The display 107 displays based on the contents written into the frame buffer 106.
Below, the method and apparatus for triangle traversal process will be described in detail.
The edge function calculate block 301 in the setup block 104 receives from the project block 103 the coordinates of three vertexes of a triangle, V0 (x0, y0), V1 (x1, y1) and V2 (x2, y2). As for i=0, 1, 2, the (xi, yi) is a float value. In addition, the three vertexes V0, V1 and V2 are clockwise on the triangle. Based on the coordinates of the three vertexes, the edge function calculate block 301 calculates the edge functions of three edges of the triangle:
e0(x,y)=a0*x+b0*y+c0;
e1(x,y)=a1*x+b1*y+c1;
e2(x,y)=a2*x+b2*y+c2.
The bounding box create block 302 in the setup block 104 receives from the project block 103 the coordinates of three vertexes of the triangle, and creates the bounding box of the triangle by using the vertex coordinates. A bounding box of a triangle is the smallest rectangle consisting of pixels that covers the triangle, which is presented by coordinates of the pixels at the four corners of the rectangle (xmin, ymin), (xmin, ymax), (xmax, ymin) and (xmax, ymax).
wherein the functions min( ) and max( ) are used to calculate the minimum and maximum values of three variables therein, and all of fxmin, fxmax, fymin and fymax are float values.
Since the pixels 203 are located in integer coordinates, the above intermediate values will be converted into integer values by the following operations:
where the ceil (float a) function returns the minimum integer that is not less than a. For example, ceil (1.5)=2. The floor (float a) function returns the maximum integer that is not larger than a. For example, floor (1.5)=1.
Since one or more vertexes of a triangle may exist outside the display plane (which is not shown in
The clip operation gives the coordinates of pixels at the four corners of the bounding box of the triangle, that is, (xmin, ymin), (xmin, ymax), (xmax, ymin), (xmax, ymax), which define the bounding box.
Based on the edge functions of the triangle calculated by the edge function calculate block 301 and the bounding box of the triangle created by the bounding box create block 302, the traversal start pixel and traversal direction choose block 303 in the setup block 104 decides the traversal start pixel and the traversal direction.
In particular, according to the process shown in
Although
As shown, there is at least one vertex of the triangle falling within a pixel at one corner of the bounding box before clip operation. If the bounding box is not subjected to a clip operation, the traversal start pixel is any one, within which a vertex of the triangle falls, among the pixels at the four corners of the bounding box. If the bounding box is subjected to a clip operation, the traversal start pixel is the one pixel at the corner corresponding to the corner where the traversal start pixel should be before the clip operation.
The traversal start pixel and traversal direction choose block 303 also chooses a traversal direction based on the coefficients of the edge functions received from the edge function calculate block 301 and the pixels (xmin, ymin), (xmin, ymax), (xmax, ymin) and (xmax, ymax) at the four corners of the bounding box. There are two possible traversal directions, that is, horizontal (line) and vertical (column) directions. If there is any pixel inside the triangle exists from the traversal start pixel in the horizontal line, the traversal direction should be horizontal; else the traversal direction should be vertical.
Based on the position of the traversal start pixel with respect to the triangle, it can be decided whether there is a pixel inside the triangle exists from the traversal start pixel in the line. The position of the traversal start pixel with respect to the triangle can be obtained from the values of the three edge functions of the triangle at the traversal start pixel. If a pixel is outside of a triangle, the values of the three edge functions at this pixel can be used to decide whether this pixel is in left/right/top/bottom of the triangle.
The following four equations are used to decide the position relationship of a pixel p(x,y) and a triangle:
The (a) and (b) of
The orders shown in
At this time, the setup block 104 has calculated the edge functions, the traversal start pixel and the traversal direction. The traverse block 105 will traverse the triangle based on the information prepared by setup block 104.
The start pixel register block 304 in the traverse block 105 firstly receives the traversal start pixel from the traversal start pixel and traversal direction choose block 303 in the setup block 104, and registers it as current scan start pixel. The scan block 305 receives the current scan start pixel from the start pixel register block 304 and the traversal direction from the traversal start pixel and traversal direction choose block 303 in the setup block 104, and then scans the current line/column of the triangle from the current scan start pixel in the traversal direction. In the traversal, the scan start pixel in each line/column is adaptively adjusted so as not to visit pixels outside the triangle as much as possible, improving speed of the traversal.
In scan, the scan block 305 sends the scanned pixel to drawing judge block 307. The drawing judge block 307 decides whether the pixel is inside the triangle. If all values of the three edge functions of the triangle at the coordinate of the pixel are positive, it can be decided that the pixel is inside the triangle. The drawing judge block 307 sends the first pixel that is inside the triangle in the current line/column received from the scan block 305 to the next start pixel calculate block 306. If the current line/column there is no pixel that is inside the triangle, the traversal is ended and an instruction indicating end of the traversal is sent to the scan block 305.
The drawing judge block 307 also decides whether to end the scan of the current line/column with respect to each pixel p(x,y) received from the scan block 305. If the drawing judge block 307 decides not to end the scan of the current line/column, it sends the pixel p(x,y) to the parameter calculate block 308. If it decides to end the scan of the current line/column, it sends an instruction indicating end of the scan of the current line/column to the scan block 305.
If the pixel p(x,y) meets with one of the following conditions, the drawing judge block 307 decide to end the scan of the current line/column:
Every time receiving a pixel from the drawing judge block 307, the next start pixel calculate block 306 calculates the scan start pixel of the next line/column. The scan start pixel of the next line/column is determined adaptively based on the coefficients of edge functions. That is, the scan start pixel of the next line/column is obtained by calculating the distance between the first pixel that is inside the triangle in the current line/column and the one in the next line/column.
Likewise, as for a combination of a scan start pixel in different position with respect to a triangle and different traversal direction, the scan start pixel in the next line/column can be adaptively chosen by using the coefficients of edge functions.
As shown, if the traversal direction is horizontal, the delta_x is the distance between xc and xp. If xc=xp, the delta_x=0. Otherwise, the delta_x=|xc−xp|−1. The value of delta_y depends on whether the traversal start pixel is in the top line or the bottom line of the bounding box. If the traversal start pixel is in the top line, the delta_y=1. Otherwise, the delta_y=−1.
If the traversal direction is vertical, the delta_y is the distance between yc and yp. If yc=yp, the delta_y=0. Otherwise, the delta_y=|yc−yp|−1. The value of delta_x depends on whether the traversal start pixel is in the left column or the right column of the bounding box. If the traversal start pixel is in the left column, the delta_x=1. Otherwise, the delta_x=−1.
The order of the four branches shown in
In accordance with the above method, the next start pixel calculate block 306 calculates the scan start pixel in the next line/column, and sends it to the start pixel register block 304. The start pixel register block 304 registers the scan start pixel received from the next start pixel calculate block 306 as the current scan start pixel.
Upon receiving an instruction indicating end of the current line/column from the drawing judge block 307, the scan block 305 retrieves the current scan start pixel that was registered previously from the start pixel register block 304 and begins to scan the next line/column.
The scan process is repeated until the end of the traversal process.
For each pixel p(x,y) received from the drawing judge block 307, the parameter calculate block 308 calculates its parameters such as colour, texture, and sends the calculated parameters and the pixel to the frame buffer 106.
The prefer embodiments have been described above. Although the invention is described in specific embodiments, the invention can be implemented in hardware, software, firmware or a combination thereof, and applied to a system, subsystem and parts or subparts thereof. When implemented in software, the blocks of the invention are substantively code segments for accomplishing necessary works.
Number | Date | Country | Kind |
---|---|---|---|
200810146771.6 | Aug 2008 | CN | national |