diff --git a/example.cpp b/example.cpp index 43855c4..6c6297d 100644 --- a/example.cpp +++ b/example.cpp @@ -50,16 +50,17 @@ int main(void) { rectangle.draw(surface.buffer, rectMapper, bisexual); UwU::ColorShader greenShader = UwU::ColorShader(green); + UwU::NullMapper nullMapper = UwU::NullMapper(); UwU::Point point = UwU::Point(187, 37); - point.draw(surface.buffer, greenShader); + point.draw(surface.buffer, nullMapper, greenShader); UwU::ColorShader blueShader = UwU::ColorShader(blue); UwU::Trapezoid trapezoid = UwU::Trapezoid(100, 300, 690, 320, 720, 780); - trapezoid.draw(surface.buffer, blueShader); + trapezoid.draw(surface.buffer, nullMapper, blueShader); UwU::ColorShader redShader = UwU::ColorShader(red); UwU::Triangle triangle = UwU::Triangle(140, 140, 60, 400, 240, 240); - triangle.draw(surface.buffer, redShader); + triangle.draw(surface.buffer, nullMapper, redShader); // // Benchmarking // struct timespec start, end; @@ -100,7 +101,7 @@ int main(void) { }; for (uint16_t i = 0; i < 8; i++) { - lines[i].draw(surface.buffer, blueShader); + lines[i].draw(surface.buffer, nullMapper, blueShader); } diff --git a/primatives/line.cpp b/primatives/line.cpp index b9a7bc9..d6ddfec 100644 --- a/primatives/line.cpp +++ b/primatives/line.cpp @@ -6,14 +6,15 @@ #include "../helpers.cpp" #include "../buffer.cpp" #include "../shaders/shaders.h" +#include "../mappers/mappers.h" namespace UwU { // line class Line { private: // plot lines if m <= 1 - template - void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, ShaderBase& shader) { + template + void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase& mapper, ShaderBase& shader) { int16_t dx = ix1 - ix0; int16_t dy = iy1 - iy0; int16_t yi = 1; @@ -24,8 +25,11 @@ namespace UwU { int16_t d = (2 * dy) - dx; int16_t y = iy0; + Coord2 uv; + for (int16_t x = ix0; x < ix1; x++) { - buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(x, y)); + uv = mapper.map(x, y); + buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(uv.x, uv.y)); if (d > 0) { y += yi; d += (2 * (dy - dx)); @@ -37,8 +41,8 @@ namespace UwU { } // plot lines if m >1 - template - void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, ShaderBase& shader) { + template + void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, MapperBase& mapper, ShaderBase& shader) { int16_t dx = ix1 - ix0; int16_t dy = iy1 - iy0; int16_t xi = 1; @@ -49,14 +53,17 @@ namespace UwU { int16_t d = (2 * dx) - dy; int16_t x = ix0; + Coord2 uv; + 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); - } + uv = mapper.map(x, y); + buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(uv.x, uv.y)); + if (d > 0) { + x += xi; + d += (2 * (dx - dy)); + } else { + d += (2 * dx); + } } return; } @@ -75,8 +82,8 @@ namespace UwU { uint16_t y1; // Uses Bresenham's line algorithm to draw line of any slope - template - void draw (Buffer buffer, ShaderBase& shader) { + template + void draw (Buffer buffer, MapperBase& mapper, ShaderBase& shader) { if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; int16_t ix0 = x0; int16_t iy0 = y0; @@ -84,15 +91,15 @@ namespace UwU { int16_t iy1 = y1; if (abs(iy1 - iy0) < abs(ix1 - ix0)) { if (ix0 > ix1) { - lineLow(ix1, iy1, ix0, iy0, buffer, shader); + lineLow(ix1, iy1, ix0, iy0, buffer, mapper, shader); } else { - lineLow(ix0, iy0, ix1, iy1, buffer, shader); + lineLow(ix0, iy0, ix1, iy1, buffer, mapper, shader); } } else { if (iy0 > iy1) { - lineHigh(ix1, iy1, ix0, iy0, buffer, shader); + lineHigh(ix1, iy1, ix0, iy0, buffer, mapper, shader); } else { - lineHigh(ix0, iy0, ix1, iy1, buffer, shader); + lineHigh(ix0, iy0, ix1, iy1, buffer, mapper, shader); } } return; diff --git a/primatives/point.cpp b/primatives/point.cpp index bb81ae3..0fc84fe 100644 --- a/primatives/point.cpp +++ b/primatives/point.cpp @@ -6,6 +6,7 @@ #include "../helpers.cpp" #include "../buffer.cpp" #include "../shaders/shaders.h" +#include "../mappers/mappers.h" namespace UwU { // point @@ -20,10 +21,11 @@ namespace UwU { uint16_t y; // Draw a point - template - void draw (Buffer buffer, ShaderBase& shader) { + template + void draw (Buffer buffer, MapperBase& mapper, ShaderBase& shader) { if (x > buffer.width || y > buffer.height) {return;}; - buffer.drawPixel(x, y, shader.shade(x, y)); + Coord2 uv = mapper.map(x, y); + buffer.drawPixel(x, y, shader.shade(uv.x, uv.y)); return; } }; diff --git a/primatives/rectangle.cpp b/primatives/rectangle.cpp index 77e5ee3..677a6ed 100644 --- a/primatives/rectangle.cpp +++ b/primatives/rectangle.cpp @@ -25,7 +25,7 @@ namespace UwU { uint16_t y1; // Draw a rectangle - template + template void draw (Buffer buffer, MapperBase& mapper, ShaderBase& shader) { if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; for (uint16_t y = y0; y <= y1; y++) { diff --git a/primatives/trapezoid.cpp b/primatives/trapezoid.cpp index c5b6408..256c2db 100644 --- a/primatives/trapezoid.cpp +++ b/primatives/trapezoid.cpp @@ -7,6 +7,7 @@ #include "../helpers.cpp" #include "../buffer.cpp" #include "../shaders/shaders.h" +#include "../mappers/mappers.h" namespace UwU { // trapezoid (horizontally constrained) @@ -30,8 +31,8 @@ namespace UwU { // Draw a horizontally bounded trapezoid // y1 >= y0 - template - void draw (Buffer buffer, ShaderBase& shader) { + template + void draw (Buffer buffer, MapperBase& mapper, ShaderBase& shader) { uint16_t height = 1 + y1 - y0; int16_t LeftBound[height]; int16_t RightBound[height]; @@ -41,7 +42,8 @@ namespace UwU { 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)); + Coord2 uv = mapper.map(x, i + y0); + buffer.drawPixel(x, i + y0, shader.shade(uv.x, uv.y)); } } return; diff --git a/primatives/triangle.cpp b/primatives/triangle.cpp index 6c22b71..6d98652 100644 --- a/primatives/triangle.cpp +++ b/primatives/triangle.cpp @@ -7,6 +7,7 @@ #include "../helpers.cpp" #include "../buffer.cpp" #include "../shaders/shaders.h" +#include "../mappers/mappers.h" namespace UwU { class Triangle { @@ -30,8 +31,8 @@ namespace UwU { uint16_t y2; // Draw a triangle - template - void draw (Buffer buffer, ShaderBase& shader) { + template + void draw (Buffer buffer, MapperBase& mapper, ShaderBase& shader) { uint16_t topX, topY, midX, midY, btmX, btmY; // Find top, middle and bottom points of triangle if (y0 <= y1 && y0 <= y2) { @@ -90,19 +91,22 @@ namespace UwU { // Calculate LUT for short two lines Helpers::bresenham(0, topX, midY - topY, midX, splitBound); Helpers::bresenham(midY - topY, midX, height - 1, btmX, splitBound); + Coord2 uv; // 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)); + uv = mapper.map(x, i + (uint16_t)topY); + buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(uv.x, uv.y)); } } // 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)); + uv = mapper.map(x, i + (uint16_t)topY); + buffer.drawPixel(x, i + (uint16_t)topY, shader.shade(uv.x, uv.y)); } } }