#include "render/raster.h" #include #include #include namespace kumo { namespace { /// @brief The edge function of the edge a -> b, evaluated at (px, py). /// /// Positive on one side of the edge, negative on the other, zero on it. For a /// triangle a, b, c the three values at a point inside add up to the /// triangle's doubled area. 64 bits, because the products of two coordinate /// differences do not fit in 32. int64_t edge_function(Point a, Point b, int64_t px, int64_t py) { return static_cast(b.x - a.x) * (py - a.y) - static_cast(b.y - a.y) * (px - a.x); } /// @brief Tells whether the edge a -> b is a top or a left edge. /// /// The triangle is oriented so that its inside is where edge functions are /// positive; with y pointing down, that inside is on the right of an edge that /// goes up (a left edge) and below an edge that goes right (a top edge). bool is_top_left(Point a, Point b) { const int32_t dx = b.x - a.x; const int32_t dy = b.y - a.y; return dy < 0 || (dy == 0 && dx > 0); } } // namespace void clear(FrameBuffer& framebuffer, Color color) { std::fill(framebuffer.pixels.begin(), framebuffer.pixels.end(), color); } void fill_rect(FrameBuffer& framebuffer, int x, int y, int w, int h, Color color) { const int x0 = std::max(x, 0); const int y0 = std::max(y, 0); const int x1 = std::min(x + w, framebuffer.width); const int y1 = std::min(y + h, framebuffer.height); for (int py = y0; py < y1; ++py) { for (int px = x0; px < x1; ++px) framebuffer.at(px, py) = color; } } void draw_line(FrameBuffer& framebuffer, int x0, int y0, int x1, int y1, Color color) { // Bresenham's algorithm in the form that works in every direction: `error` // tracks how far the pixel is from the ideal line, and each step moves in x, // in y, or in both. const int dx = std::abs(x1 - x0); const int dy = -std::abs(y1 - y0); const int step_x = x0 < x1 ? 1 : -1; const int step_y = y0 < y1 ? 1 : -1; int error = dx + dy; for (;;) { if (framebuffer.contains(x0, y0)) framebuffer.at(x0, y0) = color; if (x0 == x1 && y0 == y1) break; const int doubled = error * 2; if (doubled >= dy) { error += dy; x0 += step_x; } if (doubled <= dx) { error += dx; y0 += step_y; } } } void fill_triangle(FrameBuffer& framebuffer, Point a, Point b, Point c, Color color) { // Make the winding the same for every input, so that "inside" always means // "all three edge functions are positive". A zero area is a line or a point. const int64_t area = edge_function(a, b, c.x, c.y); if (area == 0) return; if (area < 0) std::swap(b, c); // Pixels that can possibly be inside: the bounding box, clipped to the // buffer. Shifting right rounds toward minus infinity, which is what the // lower bound needs; the upper bound is rounded up by hand. const int min_x = std::max(std::min({a.x, b.x, c.x}) >> kSubpixelBits, 0); const int min_y = std::max(std::min({a.y, b.y, c.y}) >> kSubpixelBits, 0); const int max_x = std::min((std::max({a.x, b.x, c.x}) + kSubpixelOne - 1) >> kSubpixelBits, framebuffer.width - 1); const int max_y = std::min((std::max({a.y, b.y, c.y}) + kSubpixelOne - 1) >> kSubpixelBits, framebuffer.height - 1); // A sample exactly on an edge counts as inside only for top and left edges. // Subtracting 1 from the edge function of the other edges turns "on the // edge" (0) into "outside" (-1) and leaves every other value on its side. const int64_t bias_bc = is_top_left(b, c) ? 0 : -1; // edge opposite a const int64_t bias_ca = is_top_left(c, a) ? 0 : -1; // edge opposite b const int64_t bias_ab = is_top_left(a, b) ? 0 : -1; // edge opposite c for (int py = min_y; py <= max_y; ++py) { for (int px = min_x; px <= max_x; ++px) { // The pixel's center, in Point units. const int64_t sx = static_cast(px) * kSubpixelOne + kSubpixelOne / 2; const int64_t sy = static_cast(py) * kSubpixelOne + kSubpixelOne / 2; const int64_t w0 = edge_function(b, c, sx, sy) + bias_bc; const int64_t w1 = edge_function(c, a, sx, sy) + bias_ca; const int64_t w2 = edge_function(a, b, sx, sy) + bias_ab; // The sign bit of the OR is set if any of the three is negative. if ((w0 | w1 | w2) >= 0) framebuffer.at(px, py) = color; } } } } // namespace kumo