added gradient shaders to line

This commit is contained in:
2026-07-08 16:15:48 -07:00
parent 9096b01e19
commit 43c893984f
3 changed files with 77 additions and 17 deletions
+4 -1
View File
@@ -2,4 +2,7 @@
[x] refactor example code using composition [x] refactor example code using composition
[x] remove draw class entirely [x] remove draw class entirely
[x] create shader classes with polymorphism [x] create shader classes with polymorphism
[ ] optimize shader/primative performance (repeat myself!) [x] optimize shader/primative performance (repeat myself!)
[ ] finish implementing gradient shaders to primatives
[ ] implement buffer shader
[ ] implement more primatives
BIN
View File
Binary file not shown.
+73 -16
View File
@@ -11,7 +11,7 @@ namespace UwU {
class Line { class Line {
private: private:
// plot lines if m <= 1 // plot lines if m <= 1
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Color color) { void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Shader shader) {
int16_t dx = ix1 - ix0; int16_t dx = ix1 - ix0;
int16_t dy = iy1 - iy0; int16_t dy = iy1 - iy0;
int16_t yi = 1; int16_t yi = 1;
@@ -21,20 +21,48 @@ namespace UwU {
} }
int16_t d = (2 * dy) - dx; int16_t d = (2 * dy) - dx;
int16_t y = iy0; int16_t y = iy0;
for (int16_t x = ix0; x < ix1; x++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, color); if (shader.shaderType == COLOR) {
if (d > 0) { for (int16_t x = ix0; x < ix1; x++) {
y += yi; buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.c0);
d += (2 * (dy - dx)); if (d > 0) {
} else { y += yi;
d += (2 * dy); d += (2 * (dy - dx));
} else {
d += (2 * dy);
}
} }
return;
} else if (shader.shaderType == HGRADIENT) {
for (int16_t x = ix0; x < ix1; x++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x));
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
} else {
d += (2 * dy);
}
}
return;
} else if (shader.shaderType == VGRADIENT) {
for (int16_t x = ix0; x < ix1; x++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y));
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
} else {
d += (2 * dy);
}
}
return;
} }
return; return;
} }
// plot lines if m >1 // plot lines if m >1
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Color color) { void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Shader shader) {
int16_t dx = ix1 - ix0; int16_t dx = ix1 - ix0;
int16_t dy = iy1 - iy0; int16_t dy = iy1 - iy0;
int16_t xi = 1; int16_t xi = 1;
@@ -44,8 +72,10 @@ namespace UwU {
} }
int16_t d = (2 * dx) - dy; int16_t d = (2 * dx) - dy;
int16_t x = ix0; int16_t x = ix0;
for (int16_t y = iy0; y < iy1; y++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, color); if (shader.shaderType == COLOR) {
for (int16_t y = iy0; y < iy1; y++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.c0);
if (d > 0) { if (d > 0) {
x += xi; x += xi;
d += (2 * (dx - dy)); d += (2 * (dx - dy));
@@ -54,6 +84,33 @@ namespace UwU {
} }
} }
return; return;
} else if (shader.shaderType == HGRADIENT) {
for (int16_t y = iy0; y < iy1; y++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x));
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
}
}
return;
} else if (shader.shaderType == VGRADIENT) {
for (int16_t y = iy0; y < iy1; y++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y));
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
}
}
return;
}
return;
} }
public: public:
@@ -70,7 +127,7 @@ namespace UwU {
uint16_t y1; uint16_t y1;
// Uses Bresenham's line algorithm to draw line of any slope // Uses Bresenham's line algorithm to draw line of any slope
void draw (Buffer buffer, Color color) { void draw (Buffer buffer, Shader shader) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
int16_t ix0 = x0; int16_t ix0 = x0;
int16_t iy0 = y0; int16_t iy0 = y0;
@@ -78,15 +135,15 @@ namespace UwU {
int16_t iy1 = y1; int16_t iy1 = y1;
if (abs(iy1 - iy0) < abs(ix1 - ix0)) { if (abs(iy1 - iy0) < abs(ix1 - ix0)) {
if (ix0 > ix1) { if (ix0 > ix1) {
lineLow(ix1, iy1, ix0, iy0, buffer, color); lineLow(ix1, iy1, ix0, iy0, buffer, shader);
} else { } else {
lineLow(ix0, iy0, ix1, iy1, buffer, color); lineLow(ix0, iy0, ix1, iy1, buffer, shader);
} }
} else { } else {
if (iy0 > iy1) { if (iy0 > iy1) {
lineHigh(ix1, iy1, ix0, iy0, buffer, color); lineHigh(ix1, iy1, ix0, iy0, buffer, shader);
} else { } else {
lineHigh(ix0, iy0, ix1, iy1, buffer, color); lineHigh(ix0, iy0, ix1, iy1, buffer, shader);
} }
} }
return; return;