Files
UwUGL/primatives/triangle.cpp
T

116 lines
3.1 KiB
C++
Raw Normal View History

2026-07-05 20:55:17 -07:00
#pragma once
#include <stdint.h>
#include <algorithm>
2026-07-05 20:55:17 -07:00
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
2026-07-20 02:08:06 -07:00
#include "../shaders/shaders.h"
2026-07-24 18:31:49 -07:00
#include "../mappers/mappers.h"
2026-07-05 20:55:17 -07:00
namespace UwU {
class Triangle {
2026-07-06 17:33:52 -07:00
private:
2026-07-05 20:55:17 -07:00
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;
2026-07-06 17:33:52 -07:00
// Draw a triangle
2026-07-24 18:31:49 -07:00
template <class MapperT, class ShaderT>
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
uint16_t topX, topY, midX, midY, btmX, btmY;
// Find top, middle and bottom points of triangle
2026-07-06 17:33:52 -07:00
if (y0 <= y1 && y0 <= y2) {
topX = x0;
topY = y0;
2026-07-06 17:33:52 -07:00
if (y1 < y2) {
midX = x1;
midY = y1;
btmX = x2;
btmY = y2;
2026-07-06 17:33:52 -07:00
} else {
midX = x2;
midY = y2;
btmX = x1;
btmY = y1;
2026-07-06 17:33:52 -07:00
}
} else if (y1 <= y0 && y1 <= y2) {
topX = x1;
topY = y1;
2026-07-06 17:33:52 -07:00
if (y0 < y2) {
midX = x0;
midY = y0;
btmX = x2;
btmY = y2;
2026-07-06 17:33:52 -07:00
} else {
midX = x2;
midY = y2;
btmX = x0;
btmY = y0;
2026-07-06 17:33:52 -07:00
}
} else {
topX = x2;
topY = y2;
2026-07-06 17:33:52 -07:00
if (y0 < y1) {
midY = y0;
midX = x0;
btmX = x1;
btmY = y1;
2026-07-06 17:33:52 -07:00
} else {
midX = x1;
midY = y1;
btmX = x0;
btmY = y0;
2026-07-06 17:33:52 -07:00
}
}
2026-07-06 17:33:52 -07:00
uint16_t height = 1 + btmY - topY;
2026-07-06 17:33:52 -07:00
int16_t longBound[height];
int16_t splitBound[height];
// Calculate LUT for longest vertical line (from top to bottom)
Helpers::bresenham(0, topX, height - 1, btmX, longBound);
// Calculate LUT for short two lines
Helpers::bresenham(0, topX, midY - topY, midX, splitBound);
Helpers::bresenham(midY - topY, midX, height - 1, btmX, splitBound);
2026-07-24 18:31:49 -07:00
Coord2 uv;
2026-07-20 02:08:06 -07:00
// Left-handed triangle case (midpoint is left of triangle)
if ((int16_t)midX < longBound[midY - topY]) {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
2026-07-24 18:31:49 -07:00
uv = mapper.map(x, i + (uint16_t)topY);
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(uv.x, uv.y));
}
}
2026-07-20 02:08:06 -07:00
// Right-handed triangle case (midpoint is right of triangle)
} else {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
2026-07-24 18:31:49 -07:00
uv = mapper.map(x, i + (uint16_t)topY);
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(uv.x, uv.y));
2026-07-08 17:13:57 -07:00
}
}
2026-07-06 17:33:52 -07:00
}
return;
}
2026-07-05 20:55:17 -07:00
};
}