CRTP shaders on Lines
This commit is contained in:
+17
-13
@@ -45,6 +45,10 @@ 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);
|
||||
|
||||
// // Benchmarking
|
||||
// struct timespec start, end;
|
||||
@@ -73,20 +77,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
@@ -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,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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user