2026-07-07 06:47:24 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2026-07-08 15:08:48 -07:00
|
|
|
#include <algorithm>
|
2026-07-07 06:47:24 -07:00
|
|
|
|
2026-07-07 08:09:54 -07:00
|
|
|
#include "../datatypes.cpp"
|
|
|
|
|
#include "../helpers.cpp"
|
|
|
|
|
#include "../buffer.cpp"
|
2026-07-20 02:04:17 -07:00
|
|
|
#include "../shaders/shaders.h"
|
2026-07-24 18:31:49 -07:00
|
|
|
#include "../mappers/mappers.h"
|
2026-07-07 06:47:24 -07:00
|
|
|
|
|
|
|
|
namespace UwU {
|
|
|
|
|
// trapezoid (horizontally constrained)
|
|
|
|
|
class Trapezoid {
|
|
|
|
|
public:
|
|
|
|
|
Trapezoid(uint16_t y0, uint16_t y1, uint16_t xl0, uint16_t xl1, uint16_t xr0, uint16_t xr1) {
|
|
|
|
|
this->y0 = y0;
|
|
|
|
|
this->y1 = y1;
|
|
|
|
|
this->xl0 = xl0;
|
|
|
|
|
this->xl1 = xl1;
|
|
|
|
|
this->xr0 = xr0;
|
|
|
|
|
this->xr1 = xr1;
|
|
|
|
|
}
|
|
|
|
|
Trapezoid() {}
|
|
|
|
|
uint16_t y0;
|
|
|
|
|
uint16_t y1;
|
|
|
|
|
uint16_t xl0;
|
|
|
|
|
uint16_t xl1;
|
|
|
|
|
uint16_t xr0;
|
|
|
|
|
uint16_t xr1;
|
|
|
|
|
|
|
|
|
|
// Draw a horizontally bounded trapezoid
|
|
|
|
|
// y1 >= y0
|
2026-07-24 18:31:49 -07:00
|
|
|
template <class MapperT, class ShaderT>
|
|
|
|
|
void draw (Buffer buffer, MapperBase<MapperT>& mapper, ShaderBase<ShaderT>& shader) {
|
2026-07-07 06:47:24 -07:00
|
|
|
uint16_t height = 1 + y1 - y0;
|
|
|
|
|
int16_t LeftBound[height];
|
|
|
|
|
int16_t RightBound[height];
|
|
|
|
|
|
|
|
|
|
Helpers::bresenham(0, xl0, y1 - y0, xl1, LeftBound);
|
|
|
|
|
Helpers::bresenham(0, xr0, y1 - y0, xr1, RightBound);
|
|
|
|
|
|
2026-07-20 02:04:17 -07:00
|
|
|
for (uint16_t i = 0; i < height; i++) {
|
|
|
|
|
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
|
2026-07-24 18:31:49 -07:00
|
|
|
Coord2 uv = mapper.map(x, i + y0);
|
|
|
|
|
buffer.drawPixel(x, i + y0, shader.shade(uv.x, uv.y));
|
2026-07-07 06:47:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|