Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
raster_test.cpp (10660B) · raw
1 #include "render/raster.h" 2 3 #include <algorithm> 4 #include <array> 5 6 #include "check.h" 7 #include "core/types.h" 8 #include "render/frame_buffer.h" 9 10 using kumo::Color; 11 using kumo::FrameBuffer; 12 using kumo::Point; 13 14 namespace { 15 16 constexpr Color kOn = 0xFFFFFFFF; 17 18 // The number of pixels that are not zero. 19 int count_set(const FrameBuffer& frame) { 20 return static_cast<int>(std::count_if(frame.pixels.begin(), 21 frame.pixels.end(), 22 [](Color c) { return c != 0; })); 23 } 24 25 // A small deterministic random number generator (an LCG). The standard 26 // distributions are not used, because their results differ between standard 27 // libraries and these tests must behave the same everywhere. 28 struct Random { 29 uint64_t state; 30 uint32_t next() { 31 state = state * 6364136223846793005ull + 1442695040888963407ull; 32 return static_cast<uint32_t>(state >> 33); 33 } 34 // A value in [low, high]. 35 int between(int low, int high) { 36 return low + 37 static_cast<int>(next() % static_cast<uint32_t>(high - low + 1)); 38 } 39 }; 40 41 // Splits the rectangle with corners (x0, y0) and (x1, y1) into two triangles, 42 // the second one given in the opposite winding, and checks that every pixel of 43 // the rectangle is drawn exactly once and nothing else is drawn. The diagonal 44 // goes through pixel centers whenever the rectangle is square, which is the 45 // case the top-left rule exists for. The buffer is not square, so that a mix-up 46 // of width and height shows. 47 void check_split_rect(int x0, int y0, int x1, int y1) { 48 FrameBuffer first(64, 48); 49 FrameBuffer second(64, 48); 50 const Point tl = kumo::pixel_point(x0, y0); 51 const Point tr = kumo::pixel_point(x1, y0); 52 const Point br = kumo::pixel_point(x1, y1); 53 const Point bl = kumo::pixel_point(x0, y1); 54 kumo::fill_triangle(first, tl, tr, br, kOn); 55 kumo::fill_triangle(second, tl, bl, br, kOn); 56 57 int wrong = 0; 58 for (int y = 0; y < 48; ++y) { 59 for (int x = 0; x < 64; ++x) { 60 const int drawn = (first.at(x, y) != 0) + (second.at(x, y) != 0); 61 const bool inside = x >= x0 && x < x1 && y >= y0 && y < y1; 62 if (drawn != (inside ? 1 : 0)) ++wrong; 63 } 64 } 65 CHECK_EQ(wrong, 0); 66 } 67 68 // Draws a line into an 8 by 5 buffer and compares it with a picture: '#' for a 69 // pixel that must be set and '.' for one that must not. 70 void expect_line(int x0, int y0, int x1, int y1, 71 const std::array<const char*, 5>& picture) { 72 FrameBuffer frame(8, 5); 73 kumo::draw_line(frame, x0, y0, x1, y1, kOn); 74 int wrong = 0; 75 for (int y = 0; y < 5; ++y) { 76 for (int x = 0; x < 8; ++x) { 77 if ((frame.at(x, y) != 0) != (picture[y][x] == '#')) ++wrong; 78 } 79 } 80 CHECK_EQ(wrong, 0); 81 } 82 83 // The sign of a value: -1, 0 or 1. 84 int sign(double value) { return (value > 0) - (value < 0); } 85 86 // The edge function again, written independently and in floating point. All 87 // the numbers involved are multiples of 1/512, so the arithmetic is exact. 88 double cross(double ax, double ay, double bx, double by, double px, double py) { 89 return (bx - ax) * (py - ay) - (by - ay) * (px - ax); 90 } 91 92 } // namespace 93 94 TEST(make_color_packs_alpha_red_green_blue) { 95 CHECK_EQ(kumo::make_color(1, 2, 3, 4), 0x04010203u); 96 CHECK_EQ(kumo::make_color(255, 0, 0), 0xFFFF0000u); 97 } 98 99 TEST(clear_sets_every_pixel) { 100 FrameBuffer frame(7, 5); 101 kumo::clear(frame, 0x12345678); 102 CHECK_EQ(std::count(frame.pixels.begin(), frame.pixels.end(), 0x12345678u), 103 static_cast<std::ptrdiff_t>(35)); 104 } 105 106 TEST(fill_rect_covers_exactly_the_rectangle) { 107 FrameBuffer frame(20, 10); 108 kumo::fill_rect(frame, 3, 2, 5, 4, kOn); 109 CHECK_EQ(count_set(frame), 20); 110 CHECK(frame.at(3, 2) != 0); 111 CHECK(frame.at(7, 5) != 0); 112 CHECK(frame.at(8, 5) == 0); 113 CHECK(frame.at(7, 6) == 0); 114 CHECK(frame.at(2, 2) == 0); 115 } 116 117 TEST(fill_rect_is_clipped_to_the_buffer) { 118 FrameBuffer frame(20, 10); 119 kumo::fill_rect(frame, -10, -10, 25, 25, kOn); 120 CHECK_EQ(count_set(frame), 15 * 10); // x in [0, 15), y in [0, 10) 121 kumo::fill_rect(frame, 100, 100, 5, 5, 0); // far outside: nothing happens 122 CHECK_EQ(count_set(frame), 15 * 10); 123 } 124 125 TEST(draw_line_includes_both_end_points) { 126 FrameBuffer frame(16, 16); 127 kumo::draw_line(frame, 2, 3, 2, 3, kOn); // a single point 128 CHECK_EQ(count_set(frame), 1); 129 CHECK(frame.at(2, 3) != 0); 130 131 FrameBuffer horizontal(16, 16); 132 kumo::draw_line(horizontal, 1, 1, 8, 1, kOn); 133 CHECK_EQ(count_set(horizontal), 8); 134 135 FrameBuffer vertical(16, 16); 136 kumo::draw_line(vertical, 5, 9, 5, 2, kOn); // upwards 137 CHECK_EQ(count_set(vertical), 8); 138 } 139 140 TEST(draw_line_diagonal_hits_every_corner_pixel) { 141 FrameBuffer frame(16, 16); 142 kumo::draw_line(frame, 0, 0, 9, 9, kOn); 143 CHECK_EQ(count_set(frame), 10); 144 for (int i = 0; i <= 9; ++i) CHECK(frame.at(i, i) != 0); 145 } 146 147 TEST(draw_line_draws_one_pixel_per_step_of_the_longer_side) { 148 FrameBuffer steep(16, 16); 149 kumo::draw_line(steep, 0, 0, 3, 9, kOn); 150 CHECK_EQ(count_set(steep), 10); 151 CHECK(steep.at(0, 0) != 0); 152 CHECK(steep.at(3, 9) != 0); 153 154 FrameBuffer shallow(16, 16); 155 kumo::draw_line(shallow, 15, 2, 0, 5, kOn); // right to left 156 CHECK_EQ(count_set(shallow), 16); 157 CHECK(shallow.at(15, 2) != 0); 158 CHECK(shallow.at(0, 5) != 0); 159 } 160 161 // When two pixels are equally close to the ideal line, which one a step picks 162 // is arbitrary, but it must be the same on every platform, so it is pinned 163 // here. Note that the same line drawn from the other end can differ. 164 TEST(draw_line_picks_the_same_pixel_when_two_are_equally_good) { 165 expect_line(0, 0, 7, 3, 166 {"##......", "..##....", "....##..", "......##", "........"}); 167 expect_line(0, 0, 4, 2, 168 {"#.......", ".##.....", "...##...", "........", "........"}); 169 expect_line(4, 2, 0, 0, 170 {"##......", "..##....", "....#...", "........", "........"}); 171 expect_line(0, 0, 2, 4, 172 {"#.......", ".#......", ".#......", "..#.....", "..#....."}); 173 } 174 175 TEST(draw_line_is_clipped_to_the_buffer) { 176 FrameBuffer frame(64, 48); 177 kumo::draw_line(frame, -500, -500, 500, 500, kOn); 178 CHECK_EQ(count_set(frame), 48); // the diagonal, up to the shorter side 179 for (int i = 0; i < 48; ++i) CHECK(frame.at(i, i) != 0); 180 } 181 182 TEST(two_triangles_sharing_an_edge_draw_each_pixel_once) { 183 check_split_rect(8, 8, 48, 40); 184 check_split_rect(5, 11, 42, 34); // not square: the diagonal misses centers 185 check_split_rect(0, 0, 48, 48); // square, touches the buffer's top left 186 check_split_rect(0, 0, 64, 48); // the whole buffer 187 check_split_rect(10, 10, 11, 11); // a single pixel 188 } 189 190 TEST(triangle_does_not_depend_on_the_order_of_its_corners) { 191 const std::array<Point, 3> corners = {Point{52, 40}, Point{812, 113}, 192 Point{760, 895}}; // 1/16 pixel units 193 FrameBuffer expected(64, 64); 194 kumo::fill_triangle(expected, corners[0], corners[1], corners[2], kOn); 195 CHECK(count_set(expected) > 100); 196 197 std::array<int, 3> order = {0, 1, 2}; 198 int permutations = 0; 199 do { 200 FrameBuffer frame(64, 64); 201 kumo::fill_triangle(frame, corners[order[0]], corners[order[1]], 202 corners[order[2]], kOn); 203 CHECK(frame.pixels == expected.pixels); 204 ++permutations; 205 } while (std::next_permutation(order.begin(), order.end())); 206 CHECK_EQ(permutations, 6); 207 } 208 209 TEST(degenerate_triangles_draw_nothing) { 210 FrameBuffer frame(32, 32); 211 kumo::fill_triangle(frame, kumo::pixel_point(2, 2), kumo::pixel_point(10, 10), 212 kumo::pixel_point(20, 20), 213 kOn); // three points on a line 214 kumo::fill_triangle(frame, kumo::pixel_point(5, 5), kumo::pixel_point(5, 5), 215 kumo::pixel_point(9, 12), kOn); // two equal corners 216 CHECK_EQ(count_set(frame), 0); 217 } 218 219 TEST(triangle_is_clipped_to_the_buffer) { 220 FrameBuffer outside(100, 100); 221 kumo::fill_triangle(outside, kumo::pixel_point(200, 200), 222 kumo::pixel_point(300, 200), kumo::pixel_point(250, 300), 223 kOn); 224 CHECK_EQ(count_set(outside), 0); 225 226 // A triangle whose hypotenuse is x + y = 10 (in pixels). The pixels inside 227 // the buffer are those with x + y + 1 < 10; the ones with x + y + 1 == 10 228 // are on the hypotenuse, which is neither a top nor a left edge. 229 FrameBuffer corner(100, 100); 230 kumo::fill_triangle(corner, kumo::pixel_point(-50, -50), 231 kumo::pixel_point(60, -50), kumo::pixel_point(-50, 60), 232 kOn); 233 CHECK_EQ(count_set(corner), 45); 234 235 FrameBuffer huge(100, 100); 236 kumo::fill_triangle(huge, kumo::pixel_point(-30000, -30000), 237 kumo::pixel_point(30000, -30000), 238 kumo::pixel_point(0, 30000), kOn); 239 CHECK_EQ(count_set(huge), 100 * 100); 240 } 241 242 // Compares fill_triangle with a floating point version of the same question 243 // on random triangles with corners in sixteenths of a pixel: is the center of 244 // the pixel inside? Centers that lie exactly on an edge are skipped, because 245 // the answer there is a matter of the top-left rule, which the tests above 246 // cover. Everywhere else the two must agree. 247 TEST(triangle_agrees_with_an_independent_floating_point_version) { 248 Random random = {12345}; 249 int compared = 0; 250 int disagreements = 0; 251 for (int round = 0; round < 300; ++round) { 252 Point p[3]; 253 for (Point& corner : p) { 254 corner.x = random.between(-20 * 16, 84 * 16); 255 corner.y = random.between(-20 * 16, 68 * 16); 256 } 257 FrameBuffer frame(64, 48); 258 kumo::fill_triangle(frame, p[0], p[1], p[2], kOn); 259 260 double x[3], y[3]; 261 for (int i = 0; i < 3; ++i) { 262 x[i] = p[i].x / 16.0; 263 y[i] = p[i].y / 16.0; 264 } 265 if (cross(x[0], y[0], x[1], y[1], x[2], y[2]) == 0) continue; // degenerate 266 267 for (int py = 0; py < 48; ++py) { 268 for (int px = 0; px < 64; ++px) { 269 const double cx = px + 0.5; 270 const double cy = py + 0.5; 271 const int s0 = sign(cross(x[0], y[0], x[1], y[1], cx, cy)); 272 const int s1 = sign(cross(x[1], y[1], x[2], y[2], cx, cy)); 273 const int s2 = sign(cross(x[2], y[2], x[0], y[0], cx, cy)); 274 const bool has_positive = s0 > 0 || s1 > 0 || s2 > 0; 275 const bool has_negative = s0 < 0 || s1 < 0 || s2 < 0; 276 bool inside; 277 if (has_positive && has_negative) { 278 inside = false; // clearly outside 279 } else if (s0 == 0 || s1 == 0 || s2 == 0) { 280 continue; // on an edge: decided by the top-left rule 281 } else { 282 inside = true; // all three on the same side 283 } 284 ++compared; 285 if ((frame.at(px, py) != 0) != inside) ++disagreements; 286 } 287 } 288 } 289 CHECK(compared > 100000); 290 CHECK_EQ(disagreements, 0); 291 }
