Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a00e42f75 | |||
| 90df991e7e | |||
| 3588a41657 |
+21
-13
@@ -45,6 +45,14 @@ int main(void) {
|
|||||||
UwU::ColorShader green = UwU::ColorShader(UwU::Color(0x00ff00ff));
|
UwU::ColorShader green = UwU::ColorShader(UwU::Color(0x00ff00ff));
|
||||||
UwU::Point point = UwU::Point(187, 37);
|
UwU::Point point = UwU::Point(187, 37);
|
||||||
point.draw(surface.buffer, green);
|
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
|
// // Benchmarking
|
||||||
// struct timespec start, end;
|
// struct timespec start, end;
|
||||||
@@ -73,20 +81,20 @@ int main(void) {
|
|||||||
// printf("Elapsed: %ld ns\n", timeAvg);
|
// printf("Elapsed: %ld ns\n", timeAvg);
|
||||||
|
|
||||||
|
|
||||||
// UwU::Line lines[8] {
|
UwU::Line lines[8] {
|
||||||
// UwU::Line(300, 100, 250, 469),
|
UwU::Line(300, 100, 250, 469),
|
||||||
// UwU::Line(250, 100, 300, 469),
|
UwU::Line(250, 100, 300, 469),
|
||||||
// UwU::Line(300, 100, 469, 250),
|
UwU::Line(300, 100, 469, 250),
|
||||||
// UwU::Line(469, 100, 300, 250),
|
UwU::Line(469, 100, 300, 250),
|
||||||
// UwU::Line(100, 300, 250, 469),
|
UwU::Line(100, 300, 250, 469),
|
||||||
// UwU::Line(250, 300, 100, 469),
|
UwU::Line(250, 300, 100, 469),
|
||||||
// UwU::Line(100, 300, 469, 250),
|
UwU::Line(100, 300, 469, 250),
|
||||||
// UwU::Line(469, 300, 100, 250)
|
UwU::Line(469, 300, 100, 250)
|
||||||
// };
|
};
|
||||||
|
|
||||||
// for (uint16_t i = 0; i < 8; i++) {
|
for (uint16_t i = 0; i < 8; i++) {
|
||||||
// lines[i].draw(surface.buffer, bisexual);
|
lines[i].draw(surface.buffer, blue);
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
|
||||||
surface.present();
|
surface.present();
|
||||||
|
|||||||
+23
-98
@@ -5,14 +5,15 @@
|
|||||||
#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 {
|
||||||
// line
|
// line
|
||||||
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, 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 dx = ix1 - ix0;
|
||||||
int16_t dy = iy1 - iy0;
|
int16_t dy = iy1 - iy0;
|
||||||
int16_t yi = 1;
|
int16_t yi = 1;
|
||||||
@@ -23,59 +24,21 @@ namespace UwU {
|
|||||||
int16_t d = (2 * dy) - dx;
|
int16_t d = (2 * dy) - dx;
|
||||||
int16_t y = iy0;
|
int16_t y = iy0;
|
||||||
|
|
||||||
if (shader.shaderType == COLOR) {
|
for (int16_t x = ix0; x < ix1; x++) {
|
||||||
for (int16_t x = ix0; x < ix1; x++) {
|
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(x, y));
|
||||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.c0);
|
if (d > 0) {
|
||||||
if (d > 0) {
|
y += yi;
|
||||||
y += yi;
|
d += (2 * (dy - dx));
|
||||||
d += (2 * (dy - dx));
|
} else {
|
||||||
} else {
|
d += (2 * dy);
|
||||||
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;
|
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, 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 dx = ix1 - ix0;
|
||||||
int16_t dy = iy1 - iy0;
|
int16_t dy = iy1 - iy0;
|
||||||
int16_t xi = 1;
|
int16_t xi = 1;
|
||||||
@@ -86,55 +49,16 @@ namespace UwU {
|
|||||||
int16_t d = (2 * dx) - dy;
|
int16_t d = (2 * dx) - dy;
|
||||||
int16_t x = ix0;
|
int16_t x = ix0;
|
||||||
|
|
||||||
if (shader.shaderType == COLOR) {
|
for (int16_t y = iy0; y < iy1; y++) {
|
||||||
for (int16_t y = iy0; y < iy1; y++) {
|
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(x, 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));
|
} else {
|
||||||
} else {
|
d += (2 * dx);
|
||||||
d += (2 * dx);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
|
|
||||||
} 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -151,7 +75,8 @@ 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, 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;};
|
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;
|
||||||
|
|||||||
@@ -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 {
|
||||||
// trapezoid (horizontally constrained)
|
// trapezoid (horizontally constrained)
|
||||||
@@ -30,7 +30,8 @@ namespace UwU {
|
|||||||
|
|
||||||
// Draw a horizontally bounded trapezoid
|
// Draw a horizontally bounded trapezoid
|
||||||
// y1 >= y0
|
// y1 >= y0
|
||||||
void draw (Buffer buffer, Shader shader) {
|
template <class ShaderT>
|
||||||
|
void draw (Buffer buffer, ShaderBase<ShaderT>& shader) {
|
||||||
uint16_t height = 1 + y1 - y0;
|
uint16_t height = 1 + y1 - y0;
|
||||||
int16_t LeftBound[height];
|
int16_t LeftBound[height];
|
||||||
int16_t RightBound[height];
|
int16_t RightBound[height];
|
||||||
@@ -38,49 +39,10 @@ namespace UwU {
|
|||||||
Helpers::bresenham(0, xl0, y1 - y0, xl1, LeftBound);
|
Helpers::bresenham(0, xl0, y1 - y0, xl1, LeftBound);
|
||||||
Helpers::bresenham(0, xr0, y1 - y0, xr1, RightBound);
|
Helpers::bresenham(0, xr0, y1 - y0, xr1, RightBound);
|
||||||
|
|
||||||
if (shader.shaderType == COLOR) {
|
for (uint16_t i = 0; i < height; i++) {
|
||||||
for (uint16_t i = 0; i < height; i++) {
|
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
|
||||||
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
|
buffer.drawPixel(x, i + y0, shader.shade(x, i + y0));
|
||||||
buffer.drawPixel(x, i + y0, shader.c0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-83
@@ -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,91 +91,20 @@ 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.shade(x, i + (uint16_t)topY));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
// Right-handed triangle case (midpoint is right of triangle)
|
||||||
|
} else {
|
||||||
} else if (shader.shaderType == HGRADIENT) {
|
for (uint16_t i = 0; i < height; i++) {
|
||||||
// find left and right bounds
|
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
|
||||||
uint16_t leftmost = std::min(x0, std::min(x1, x2));
|
buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(x, i + (uint16_t)topY));
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user