#pragma once #include #include "../datatypes.cpp" #include "../helpers.cpp" #include "../buffer.cpp" namespace UwU { class Triangle { private: public: Triangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { this->x0 = x0; this->y0 = y0; this->x1 = x1; this->y1 = y1; this->x2 = x2; this->y2 = y2; } Triangle() {} uint16_t x0; uint16_t y0; uint16_t x1; uint16_t y1; uint16_t x2; uint16_t y2; // Draw a triangle void draw (Buffer buffer, Color color) { Coord2 top = Coord2(); Coord2 mid = Coord2(); Coord2 btm = Coord2(); // cases: left-handed, right-handed if (y0 <= y1 && y0 <= y2) { top.x = x0; top.y = y0; if (y1 < y2) { mid.x = x1; mid.y = y1; btm.x = x2; btm.y = y2; } else { mid.x = x2; mid.y = y2; btm.x = x1; btm.y = y1; } } else if (y1 <= y0 && y1 <= y2) { top.x = x1; top.y = y1; if (y0 < y2) { mid.x = x0; mid.y = y0; btm.x = x2; btm.y = y2; } else { mid.x = x2; mid.y = y2; btm.x = x0; btm.y = y0; } } else { top.x = x2; top.y = y2; if (y0 < y1) { mid.y = y0; mid.x = x0; btm.x = x1; btm.y = y1; } else { mid.x = x1; mid.y = y1; btm.x = x0; btm.y = y0; } } // printf("top: (%u, %u)\n", top.x, top.y); // printf("mid: (%u, %u)\n", mid.x, mid.y); // printf("btm: (%u, %u)\n", btm.x, btm.y); uint16_t height = 1 + btm.y - top.y; int16_t longBound[height]; int16_t splitBound[height]; // int16_t rightBound[height]; Helpers::bresenham(0, top.x, height - 1, btm.x, longBound); // printf("longBound\n"); if ((int16_t)mid.x < longBound[mid.y - top.y]) { // left-handed triangle // printf("left-handed\n"); // rightBound = longBound; Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); // printf("splitTop\n"); Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); // printf("splitBtm\n"); // for (uint16_t i = 0; i < height; i++) { // printf("(%u, %u)\n", i, splitBound[i]); // } for (uint16_t i = 0; i < height; i++) { for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) { // Helpers::putPixel(buffer, x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a); buffer.drawPixel(x, i + (uint16_t)top.y, color); } } } else { // right-handed triangle // printf("right-handed\n"); // leftBound = longBound; Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); // printf("splitTop\n"); Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); // printf("splitBtm\n"); for (uint16_t i = 0; i < height; i++) { for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) { // Helpers::putPixel(buffer, x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a); buffer.drawPixel(x, i + (uint16_t)top.y, color); } } } return; } }; }