https://haqr.eu/tinyrenderer/rasterization/

Start point

020.png

Scanline - Old-school way

  1. Sort the vertices of the triangle by their y-coordinates.
  2. Rasterize both the left and right edges of the triangle simultaneously.
  3. Draw horizontal line segments between the left and right boundary points.

看他的代码是没有什么价值的,这里已经给了一个 Idea, 我们就是从图片底部一行行往上刷。把三角形边框和内部涂色。 June 23, 2026 6:11 AM (GMT+8) (计时开始,看一下一个小时能不能自己实现出来) (June 23, 2026 6:41 AM (GMT+8) OK,只用了 30 分钟)

021_bottom_half.png

021_full.png

image.png

简而言之,旧式的扫描线方法就是一个三角形,从下往上一行行从左向右刷色。

AABB - Modern way

前面的过时的扫描线方法虽然实现起来并不复杂,代码行数不是很多。但是扫描线的实现细节还是繁琐的,先对 y 排序,然后又分上下部分,找左右端点之类。

更现代的做法是先确定 aabb (轴对齐的包围盒),然后判断包围盒内的点是否在三角形内部。这两点都是非常容易做到的。

  1. 确定包围盒,直接求 x y 坐标的 min, max
  2. 判断点是否在三角形内部,梦回 GAMES 101
triangle(vec2 points[3]) {
    vec2 bbox[2] = find_bounding_box(points);
    for (each pixel in the bounding box) {
        if (inside(points, pixel)) {
            put_pixel(pixel);
        }
    }
}

这里就很能感受到和我们上面的扫描线做法的区分,一个很 “CPU”, 一个很 “GPU”。

022.png

023.png