Compare commits

..

3 Commits

Author SHA1 Message Date
Annika 8a00e42f75 completed CRTP shader migration 2026-07-20 02:08:40 -07:00
Annika 90df991e7e CRTP shaders on Lines 2026-07-20 02:08:06 -07:00
Annika 3588a41657 CRTP shaders on Lines 2026-07-20 02:04:17 -07:00
5 changed files with 63 additions and 238 deletions
BIN
View File
Binary file not shown.
+21 -13
View File
@@ -45,6 +45,14 @@ int main(void) {
UwU::ColorShader green = UwU::ColorShader(UwU::Color(0x00ff00ff));
UwU::Point point = UwU::Point(187, 37);
point.draw(surface.buffer, green);
UwU::ColorShader blue = UwU::ColorShader(UwU::Color(0x0000ffff));
UwU::Trapezoid trapezoid = UwU::Trapezoid(100, 300, 690, 320, 720, 780);
trapezoid.draw(surface.buffer, blue);
UwU::ColorShader red = UwU::ColorShader(UwU::Color(0xff0000ff));
UwU::Triangle triangle = UwU::Triangle(140, 140, 60, 400, 240, 240);
triangle.draw(surface.buffer, red);
// // Benchmarking
// struct timespec start, end;
@@ -73,20 +81,20 @@ int main(void) {
// printf("Elapsed: %ld ns\n", timeAvg);
// UwU::Line lines[8] {
// UwU::Line(300, 100, 250, 469),
// UwU::Line(250, 100, 300, 469),
// UwU::Line(300, 100, 469, 250),
// UwU::Line(469, 100, 300, 250),
// UwU::Line(100, 300, 250, 469),
// UwU::Line(250, 300, 100, 469),
// UwU::Line(100, 300, 469, 250),
// UwU::Line(469, 300, 100, 250)
// };
UwU::Line lines[8] {
UwU::Line(300, 100, 250, 469),
UwU::Line(250, 100, 300, 469),
UwU::Line(300, 100, 469, 250),
UwU::Line(469, 100, 300, 250),
UwU::Line(100, 300, 250, 469),
UwU::Line(250, 300, 100, 469),
UwU::Line(100, 300, 469, 250),
UwU::Line(469, 300, 100, 250)
};
// for (uint16_t i = 0; i < 8; i++) {
// lines[i].draw(surface.buffer, bisexual);
// }
for (uint16_t i = 0; i < 8; i++) {
lines[i].draw(surface.buffer, blue);
}
surface.present();
+23 -98
View File
@@ -5,14 +5,15 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shader.cpp"
#include "../shaders/shaders.h"
namespace UwU {
// line
class Line {
private:
// plot lines if m <= 1
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Shader shader) {
template <class ShaderT>
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, ShaderBase<ShaderT>& shader) {
int16_t dx = ix1 - ix0;
int16_t dy = iy1 - iy0;
int16_t yi = 1;
@@ -23,59 +24,21 @@ namespace UwU {
int16_t d = (2 * dy) - dx;
int16_t y = iy0;
if (shader.shaderType == COLOR) {
for (int16_t x = ix0; x < ix1; x++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.c0);
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
} else {
d += (2 * dy);
}
for (int16_t x = ix0; x < ix1; x++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(x, y));
if (d > 0) {
y += yi;
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;
} else if (shader.shaderType == BUFFER) {
for (int16_t x = ix0; x < ix1; x++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, y));
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
} else {
d += (2 * dy);
}
}
return;
}
return;
}
// plot lines if m >1
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Shader shader) {
template <class ShaderT>
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, ShaderBase<ShaderT>& shader) {
int16_t dx = ix1 - ix0;
int16_t dy = iy1 - iy0;
int16_t xi = 1;
@@ -86,55 +49,16 @@ namespace UwU {
int16_t d = (2 * dx) - dy;
int16_t x = ix0;
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) {
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
}
for (int16_t y = iy0; y < iy1; y++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(x, y));
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
}
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;
} else if (shader.shaderType == BUFFER) {
for (int16_t y = iy0; y < iy1; y++) {
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, y));
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
}
}
return;
}
return;
}
return;
}
public:
@@ -151,7 +75,8 @@ namespace UwU {
uint16_t y1;
// Uses Bresenham's line algorithm to draw line of any slope
void draw (Buffer buffer, Shader shader) {
template <class ShaderT>
void draw (Buffer buffer, ShaderBase<ShaderT>& shader) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
int16_t ix0 = x0;
int16_t iy0 = y0;
+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;
}
+13 -83
View File
@@ -6,7 +6,7 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shader.cpp"
#include "../shaders/shaders.h"
namespace UwU {
class Triangle {
@@ -30,7 +30,8 @@ namespace UwU {
uint16_t y2;
// Draw a triangle
void draw (Buffer buffer, Shader shader) {
template <class ShaderT>
void draw (Buffer buffer, ShaderBase<ShaderT>& shader) {
uint16_t topX, topY, midX, midY, btmX, btmY;
// Find top, middle and bottom points of triangle
if (y0 <= y1 && y0 <= y2) {
@@ -90,91 +91,20 @@ namespace UwU {
Helpers::bresenham(0, topX, midY - topY, midX, splitBound);
Helpers::bresenham(midY - topY, midX, height - 1, btmX, splitBound);
if (shader.shaderType == COLOR) {
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, shader.c0);
}
}
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, shader.c0);
}
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(x, i + (uint16_t)topY));
}
}
return;
} else if (shader.shaderType == HGRADIENT) {
// find left and right bounds
uint16_t leftmost = std::min(x0, std::min(x1, x2));
uint16_t rightmost = std::max(x0, std::max(x1, x2));
// 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);
}
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, cols[x - leftmost]);
}
}
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, cols[x - leftmost]);
}
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(x, i + (uint16_t)topY));
}
}
return;
} else if (shader.shaderType == VGRADIENT) {
Color c;
// Left-handed triangle case (midpoint is left of triangle)
if ((int16_t)midX < longBound[midY - topY]) {
for (uint16_t i = 0; i < height; i++) {
c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, i + topY);
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, c);
}
}
// Right-handed triangle case (midpoint is right of triangle)
} else {
for (uint16_t i = 0; i < height; i++) {
c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, i + topY);
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, c);
}
}
}
return;
} else if (shader.shaderType == BUFFER) {
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, i + topY));
}
}
// 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++) {
buffer.drawPixel(x, i + (uint16_t)topY, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, i + topY));
}
}
}
return;
}
return;
}