CRTP shaders on Lines

This commit is contained in:
2026-07-20 02:04:17 -07:00
parent 0f0e352fca
commit 3588a41657
4 changed files with 46 additions and 155 deletions
+6 -44
View File
@@ -6,7 +6,7 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shader.cpp"
#include "../shaders/shaders.h"
namespace UwU {
// trapezoid (horizontally constrained)
@@ -30,7 +30,8 @@ namespace UwU {
// Draw a horizontally bounded trapezoid
// y1 >= y0
void draw (Buffer buffer, Shader shader) {
template <class ShaderT>
void draw (Buffer buffer, ShaderBase<ShaderT>& shader) {
uint16_t height = 1 + y1 - y0;
int16_t LeftBound[height];
int16_t RightBound[height];
@@ -38,49 +39,10 @@ namespace UwU {
Helpers::bresenham(0, xl0, y1 - y0, xl1, LeftBound);
Helpers::bresenham(0, xr0, y1 - y0, xr1, RightBound);
if (shader.shaderType == COLOR) {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
buffer.drawPixel(x, i + y0, shader.c0);
}
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
buffer.drawPixel(x, i + y0, shader.shade(x, i + y0));
}
return;
} else if (shader.shaderType == HGRADIENT) {
// find left-right boundaries
uint16_t leftmost = std::min(xl0, xl1);
uint16_t rightmost = std::max(xr0, xr1);
// precompute gradient LUT
Color cols[rightmost - leftmost];
for (uint16_t x = leftmost; x < rightmost; x++) {
cols[x - leftmost] = ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x);
}
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
buffer.drawPixel(x, i + y0, cols[x - leftmost]);
}
}
return;
} else if (shader.shaderType == VGRADIENT) {
Color c;
for (uint16_t i = 0; i < height; i++) {
c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, i + y0);
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
buffer.drawPixel(x, i + y0, c);
}
}
return;
} else if (shader.shaderType == BUFFER) {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
buffer.drawPixel(x, i + y0, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, i + y0));
}
}
return;
}
return;
}