added gradient shader logic to trapezoids

This commit is contained in:
2026-07-08 14:34:55 -07:00
parent b7032c3b8e
commit f61e3a8237
8 changed files with 70 additions and 54 deletions
+2 -2
View File
@@ -22,7 +22,7 @@ namespace UwU {
int16_t d = (2 * dy) - dx;
int16_t y = iy0;
for (int16_t x = ix0; x < ix1; x++) {
buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), color);
buffer.drawPixel((uint16_t)x, (uint16_t)y, color);
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
@@ -45,7 +45,7 @@ namespace UwU {
int16_t d = (2 * dx) - dy;
int16_t x = ix0;
for (int16_t y = iy0; y < iy1; y++) {
buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), color);
buffer.drawPixel((uint16_t)x, (uint16_t)y, color);
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
+1 -1
View File
@@ -21,7 +21,7 @@ namespace UwU {
// Draw a rectangle
void draw (Buffer buffer, Color color) {
if (x > buffer.width || y > buffer.height) {return;};
buffer.drawPixel(Coord2(x, y), color);
buffer.drawPixel(x, y, color);
return;
}
};
+6 -3
View File
@@ -29,20 +29,23 @@ namespace UwU {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
for (uint16_t y = y0; y < y1; y++) {
for (uint16_t x = x0; x < x1; x++) {
buffer.drawPixel(Coord2(x, y), shader.c0);
buffer.drawPixel(x, y, shader.c0);
}
}
return;
} else if (shader.shaderType == HGRADIENT) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
// Precompute gradient LUT
Color cols[x1 - x0];
for (uint16_t x = x0; x < x1; x++) {
cols[x - x0] = ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x);
}
for (uint16_t y = y0; y < y1; y++) {
for (uint16_t x = x0; x < x1; x++) {
buffer.drawPixel(Coord2(x, y), cols[x - x0]);
buffer.drawPixel(x, y, cols[x - x0]);
}
}
return;
@@ -52,7 +55,7 @@ namespace UwU {
for (uint16_t y = y0; y < y1; y++) {
Color c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y);
for (uint16_t x = x0; x < x1; x++) {
buffer.drawPixel(Coord2(x, y), c);
buffer.drawPixel(x, y, c);
}
}
return;
+46 -16
View File
@@ -5,6 +5,7 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shader.cpp"
namespace UwU {
// trapezoid (horizontally constrained)
@@ -28,9 +29,7 @@ namespace UwU {
// Draw a horizontally bounded trapezoid
// y1 >= y0
// todo move to private and add public checks
void draw (Buffer buffer, Color color) {
// printf("(%u, %u, %u, %u, %u, %u)", coords.y0, coords.y1, coords.xl0, coords.xl1, coords.xr0, coords.xr1);
void draw (Buffer buffer, Shader shader) {
uint16_t height = 1 + y1 - y0;
int16_t LeftBound[height];
int16_t RightBound[height];
@@ -38,20 +37,51 @@ namespace UwU {
Helpers::bresenham(0, xl0, y1 - y0, xl1, LeftBound);
Helpers::bresenham(0, xr0, y1 - y0, xr1, RightBound);
// printf("leftbound\n");
for (uint16_t i = 0; i < height; i++) {
// printf("(%u, %u)\n", i, LeftBound[i]);
}
// printf("rightbound\n");
for (uint16_t i = 0; i < height; i++) {
// printf("(%u, %u)\n", i, RightBound[i]);
}
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
// printf("(%u, %u)", x, i + coords.y0);
buffer.drawPixel(Coord2(x, i + y0), color);
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);
}
}
return;
} else if (shader.shaderType == HGRADIENT) {
// find left-right boundaries
uint16_t leftmost, rightmost;
if (xl0 < xl1) {
leftmost = xl0;
} else {
leftmost = xl1;
}
if (xr0 > xr1) {
rightmost = xr0;
} else {
rightmost = 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;
}
return;
}
+2 -2
View File
@@ -101,7 +101,7 @@ namespace UwU {
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(Coord2(x, i + (uint16_t)top.y), color);
buffer.drawPixel(x, i + (uint16_t)top.y, color);
}
}
} else {
@@ -115,7 +115,7 @@ namespace UwU {
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(Coord2(x, i + (uint16_t)top.y), color);
buffer.drawPixel(x, i + (uint16_t)top.y, color);
}
}
}