CRTP shaders on Lines
This commit is contained in:
@@ -50,6 +50,10 @@ int main(void) {
|
|||||||
UwU::Trapezoid trapezoid = UwU::Trapezoid(100, 300, 690, 320, 720, 780);
|
UwU::Trapezoid trapezoid = UwU::Trapezoid(100, 300, 690, 320, 720, 780);
|
||||||
trapezoid.draw(surface.buffer, blue);
|
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
|
// // Benchmarking
|
||||||
// struct timespec start, end;
|
// struct timespec start, end;
|
||||||
// const int WARMUP = 100;
|
// const int WARMUP = 100;
|
||||||
|
|||||||
+5
-75
@@ -6,7 +6,7 @@
|
|||||||
#include "../datatypes.cpp"
|
#include "../datatypes.cpp"
|
||||||
#include "../helpers.cpp"
|
#include "../helpers.cpp"
|
||||||
#include "../buffer.cpp"
|
#include "../buffer.cpp"
|
||||||
#include "../shader.cpp"
|
#include "../shaders/shaders.h"
|
||||||
|
|
||||||
namespace UwU {
|
namespace UwU {
|
||||||
class Triangle {
|
class Triangle {
|
||||||
@@ -30,7 +30,8 @@ namespace UwU {
|
|||||||
uint16_t y2;
|
uint16_t y2;
|
||||||
|
|
||||||
// Draw a triangle
|
// 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;
|
uint16_t topX, topY, midX, midY, btmX, btmY;
|
||||||
// Find top, middle and bottom points of triangle
|
// Find top, middle and bottom points of triangle
|
||||||
if (y0 <= y1 && y0 <= y2) {
|
if (y0 <= y1 && y0 <= y2) {
|
||||||
@@ -90,93 +91,22 @@ namespace UwU {
|
|||||||
Helpers::bresenham(0, topX, midY - topY, midX, splitBound);
|
Helpers::bresenham(0, topX, midY - topY, midX, splitBound);
|
||||||
Helpers::bresenham(midY - topY, midX, height - 1, btmX, splitBound);
|
Helpers::bresenham(midY - topY, midX, height - 1, btmX, splitBound);
|
||||||
|
|
||||||
if (shader.shaderType == COLOR) {
|
|
||||||
// Left-handed triangle case (midpoint is left of triangle)
|
// Left-handed triangle case (midpoint is left of triangle)
|
||||||
if ((int16_t)midX < longBound[midY - topY]) {
|
if ((int16_t)midX < longBound[midY - topY]) {
|
||||||
for (uint16_t i = 0; i < height; i++) {
|
for (uint16_t i = 0; i < height; i++) {
|
||||||
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
|
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
|
||||||
buffer.drawPixel(x, i + (uint16_t)topY, shader.c0);
|
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(x, i + (uint16_t)topY));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Right-handed triangle case (midpoint is right of triangle)
|
// Right-handed triangle case (midpoint is right of triangle)
|
||||||
} else {
|
} else {
|
||||||
for (uint16_t i = 0; i < height; i++) {
|
for (uint16_t i = 0; i < height; i++) {
|
||||||
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
|
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
|
||||||
buffer.drawPixel(x, i + (uint16_t)topY, shader.c0);
|
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(x, i + (uint16_t)topY));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user