/// @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 "core/types.h" #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