Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
raster.cpp (4534B) · raw
1 #include "render/raster.h" 2 3 #include <algorithm> 4 #include <cstdlib> 5 #include <utility> 6 7 namespace kumo { 8 9 namespace { 10 11 /// @brief The edge function of the edge a -> b, evaluated at (px, py). 12 /// 13 /// Positive on one side of the edge, negative on the other, zero on it. For a 14 /// triangle a, b, c the three values at a point inside add up to the 15 /// triangle's doubled area. 64 bits, because the products of two coordinate 16 /// differences do not fit in 32. 17 int64_t edge_function(Point a, Point b, int64_t px, int64_t py) { 18 return static_cast<int64_t>(b.x - a.x) * (py - a.y) - 19 static_cast<int64_t>(b.y - a.y) * (px - a.x); 20 } 21 22 /// @brief Tells whether the edge a -> b is a top or a left edge. 23 /// 24 /// The triangle is oriented so that its inside is where edge functions are 25 /// positive; with y pointing down, that inside is on the right of an edge that 26 /// goes up (a left edge) and below an edge that goes right (a top edge). 27 bool is_top_left(Point a, Point b) { 28 const int32_t dx = b.x - a.x; 29 const int32_t dy = b.y - a.y; 30 return dy < 0 || (dy == 0 && dx > 0); 31 } 32 33 } // namespace 34 35 void clear(FrameBuffer& framebuffer, Color color) { 36 std::fill(framebuffer.pixels.begin(), framebuffer.pixels.end(), color); 37 } 38 39 void fill_rect(FrameBuffer& framebuffer, int x, int y, int w, int h, 40 Color color) { 41 const int x0 = std::max(x, 0); 42 const int y0 = std::max(y, 0); 43 const int x1 = std::min(x + w, framebuffer.width); 44 const int y1 = std::min(y + h, framebuffer.height); 45 for (int py = y0; py < y1; ++py) { 46 for (int px = x0; px < x1; ++px) framebuffer.at(px, py) = color; 47 } 48 } 49 50 void draw_line(FrameBuffer& framebuffer, int x0, int y0, int x1, int y1, 51 Color color) { 52 // Bresenham's algorithm in the form that works in every direction: `error` 53 // tracks how far the pixel is from the ideal line, and each step moves in x, 54 // in y, or in both. 55 const int dx = std::abs(x1 - x0); 56 const int dy = -std::abs(y1 - y0); 57 const int step_x = x0 < x1 ? 1 : -1; 58 const int step_y = y0 < y1 ? 1 : -1; 59 int error = dx + dy; 60 for (;;) { 61 if (framebuffer.contains(x0, y0)) framebuffer.at(x0, y0) = color; 62 if (x0 == x1 && y0 == y1) break; 63 const int doubled = error * 2; 64 if (doubled >= dy) { 65 error += dy; 66 x0 += step_x; 67 } 68 if (doubled <= dx) { 69 error += dx; 70 y0 += step_y; 71 } 72 } 73 } 74 75 void fill_triangle(FrameBuffer& framebuffer, Point a, Point b, Point c, 76 Color color) { 77 // Make the winding the same for every input, so that "inside" always means 78 // "all three edge functions are positive". A zero area is a line or a point. 79 const int64_t area = edge_function(a, b, c.x, c.y); 80 if (area == 0) return; 81 if (area < 0) std::swap(b, c); 82 83 // Pixels that can possibly be inside: the bounding box, clipped to the 84 // buffer. Shifting right rounds toward minus infinity, which is what the 85 // lower bound needs; the upper bound is rounded up by hand. 86 const int min_x = std::max(std::min({a.x, b.x, c.x}) >> kSubpixelBits, 0); 87 const int min_y = std::max(std::min({a.y, b.y, c.y}) >> kSubpixelBits, 0); 88 const int max_x = 89 std::min((std::max({a.x, b.x, c.x}) + kSubpixelOne - 1) >> kSubpixelBits, 90 framebuffer.width - 1); 91 const int max_y = 92 std::min((std::max({a.y, b.y, c.y}) + kSubpixelOne - 1) >> kSubpixelBits, 93 framebuffer.height - 1); 94 95 // A sample exactly on an edge counts as inside only for top and left edges. 96 // Subtracting 1 from the edge function of the other edges turns "on the 97 // edge" (0) into "outside" (-1) and leaves every other value on its side. 98 const int64_t bias_bc = is_top_left(b, c) ? 0 : -1; // edge opposite a 99 const int64_t bias_ca = is_top_left(c, a) ? 0 : -1; // edge opposite b 100 const int64_t bias_ab = is_top_left(a, b) ? 0 : -1; // edge opposite c 101 102 for (int py = min_y; py <= max_y; ++py) { 103 for (int px = min_x; px <= max_x; ++px) { 104 // The pixel's center, in Point units. 105 const int64_t sx = 106 static_cast<int64_t>(px) * kSubpixelOne + kSubpixelOne / 2; 107 const int64_t sy = 108 static_cast<int64_t>(py) * kSubpixelOne + kSubpixelOne / 2; 109 const int64_t w0 = edge_function(b, c, sx, sy) + bias_bc; 110 const int64_t w1 = edge_function(c, a, sx, sy) + bias_ca; 111 const int64_t w2 = edge_function(a, b, sx, sy) + bias_ab; 112 // The sign bit of the OR is set if any of the three is negative. 113 if ((w0 | w1 | w2) >= 0) framebuffer.at(px, py) = color; 114 } 115 } 116 } 117 118 } // namespace kumo
