【GAMES101】05 Rasterization(Triangles)

Rasterization process: The process of converting a sequence of transformed triangles into pixels.

Triangles are used a lot in graphics.

  • The most basic polygon (least number of sides).
  • Any polygon can be broken down into triangles.
  • Properties: The interior of a triangle must be planar. The interior and exterior of the triangle are very clearly defined.
  • After the three vertices are defined, interpolation within the triangle is possible.

The most important step in rasterization: judging the positional relationship between a triangle and a pixel (center point)

sampling:

  • A process of discretizing a function.
  • Sampling can be one-dimensional, two-dimensional, or three-dimensional. Rasterization is two-dimensional sampling.

Rasterization method:

Given a triangle, determine whether the pixel center is within the triangle.

 The process of converting triangles into pixels is actually to judge the relationship between pixels and triangles. More precisely, whether the center point of the pixel is inside the triangle. And whether a point is in the triangle can be judged by the cross product of vectors.

for(int x = 0; x < xMax; ++x)
    for(int y = 0; y < yMax; ++y)
         image[x][y] = inside(tri, x + 0.5, y + 0.5);

Through the above function, we know whether the coordinate point (x, y) is in the triangle. This solution process, we generally call it sampling (sampling of the screen from the center of the pixel). Therefore, the popular understanding of sampling is the process of giving a linear function, inputting some values, and finally obtaining the output value.

From the for loop above, we know that basically every pixel on the screen is traversed. A simple optimization can be done here. First judge whether it is within the rectangle formed by the triangle (take the minimum and maximum values ​​of the abscissa and ordinate of the three vertices of the triangle). We can only judge the pixels within the rectangle.

The blue area represents the bounding box of the triangle. Only the pixels in this area can do the above for loop; that is, it is not necessary to walk every pixel from (0-width/height) once).

 After the rasterization process, the following results can be obtained. However, the above result is obviously far worse than the original triangle, and the hypotenuse is irregular (this is what we call sawtooth ).

Guess you like

Origin blog.csdn.net/qq_37308779/article/details/130477901