Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 1e8549429bbe973428096003a08b801d626494b3
parent 5b462a93c023f62839c63f617b4185892d379fc7
Author: sentomk <sentomk@proton.me>
Date:   Thu,  8 Oct 2026 21:39:20 +0800

feat: add integer rasterizer for rects, lines and triangles

Everything is integer arithmetic, so the same call gives the same pixels
on every compiler and CPU. Triangle vertices are fixed point with four
fractional bits, the edge functions use 64 bits, and the top-left rule
decides samples that fall exactly on an edge, so triangles sharing an edge
neither overlap nor leave gaps.

Diffstat:
Asrc/render/raster.cpp | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/render/raster.h | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 171 insertions(+), 0 deletions(-)

diff --git a/src/render/raster.cpp b/src/render/raster.cpp @@ -0,0 +1,116 @@ +#include "render/raster.h" + +#include <algorithm> + +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<int64_t>(b.x - a.x) * (py - a.y) - + static_cast<int64_t>(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<int64_t>(px) * kSubpixelOne + kSubpixelOne / 2; + const int64_t sy = + static_cast<int64_t>(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 diff --git a/src/render/raster.h b/src/render/raster.h @@ -0,0 +1,55 @@ +/// @file render/raster.h +/// @brief Drawing primitives of the software renderer. +/// +/// Everything here uses integer arithmetic only, so the same call produces the +/// same pixels on every compiler and CPU. +#pragma once + +#include <cstdint> + +#include "render/frame_buffer.h" + +namespace kumo { + +/// @brief Number of fractional bits in a Point coordinate. +constexpr int kSubpixelBits = 4; + +/// @brief One pixel, in the units of a Point coordinate. +constexpr int32_t kSubpixelOne = 1 << kSubpixelBits; + +/// @brief A position in fixed point: 4 fractional bits, so 1/16 pixel steps. +/// +/// Pixel (px, py) occupies the square from (px, py) to (px + 1, py + 1) and is +/// sampled at its center. Keep coordinates within about +-32000 pixels. +struct Point { + int32_t x; + int32_t y; +}; + +/// @brief The Point at the top left corner of pixel (x, y). +constexpr Point pixel_point(int x, int y) { + return {x * kSubpixelOne, y * kSubpixelOne}; +} + +/// @brief Sets every pixel to @p color. +void clear(FrameBuffer& framebuffer, Color color); + +/// @brief Fills the rectangle at (x, y) of size w by h, clipped to the buffer. +void fill_rect(FrameBuffer& framebuffer, int x, int y, int w, int h, + Color color); + +/// @brief Draws a one pixel wide line from (x0, y0) to (x1, y1), both ends +/// included, clipped to the buffer. +void draw_line(FrameBuffer& framebuffer, int x0, int y0, int x1, int y1, + Color color); + +/// @brief Fills the triangle @p a, @p b, @p c, whichever way it winds. +/// +/// A pixel belongs to the triangle if its center is inside. A center exactly +/// on an edge belongs to it only if that is a top or a left edge (the +/// "top-left rule"), so two triangles that share an edge never draw a pixel +/// twice and never leave a gap. +void fill_triangle(FrameBuffer& framebuffer, Point a, Point b, Point c, + Color color); + +} // namespace kumo