diff --git a/buffer.cpp b/buffer.cpp index f3fbac4..ddbe894 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -4,7 +4,7 @@ #include "datatypes.cpp" #include "helpers.cpp" -#include "shader.cpp" +// #include "shader.cpp" namespace UwU { // Buffer object - can be a wayland surface or a virtual buffer diff --git a/datatypes.cpp b/datatypes.cpp index 47659d8..a27de46 100644 --- a/datatypes.cpp +++ b/datatypes.cpp @@ -26,41 +26,41 @@ namespace UwU { }; // Coord2 datatype for xy coordinates, from top-left of surface - struct Coord2 { - Coord2(uint16_t x, uint16_t y) { - this->x = x; - this->y = y; - } - Coord2() {} - uint16_t x; - uint16_t y; - }; + // struct Coord2 { + // Coord2(uint16_t x, uint16_t y) { + // this->x = x; + // this->y = y; + // } + // Coord2() {} + // uint16_t x; + // uint16_t y; + // }; // Vec2 datatype for signed x, y coordinates - struct Vec2 { - Vec2(int16_t x, int16_t y) { - this->x = x; - this->y = y; - } - Vec2() {} - int16_t x; - int16_t y; - }; + // struct Vec2 { + // Vec2(int16_t x, int16_t y) { + // this->x = x; + // this->y = y; + // } + // Vec2() {} + // int16_t x; + // int16_t y; + // }; - // 2x2 matrix, Q15.16 signed fixed point - struct Matrix4 { - Matrix4(int32_t xx, int32_t xy, int32_t yx, int32_t yy) { - this->xx = xx; - this->xy = xy; - this->yx = yx; - this->yy = yy; - } - Matrix4() {} - int32_t xx; // x' = x*xx + y*xy - int32_t xy; - int32_t yx; // y' = x*yx + y*yy - int32_t yy; - }; + // // 2x2 matrix, Q15.16 signed fixed point + // struct Matrix4 { + // Matrix4(int32_t xx, int32_t xy, int32_t yx, int32_t yy) { + // this->xx = xx; + // this->xy = xy; + // this->yx = yx; + // this->yy = yy; + // } + // Matrix4() {} + // int32_t xx; // x' = x*xx + y*xy + // int32_t xy; + // int32_t yx; // y' = x*yx + y*yy + // int32_t yy; + // }; // class Mapper { // public: @@ -74,7 +74,8 @@ namespace UwU { enum ShaderType { COLOR, HGRADIENT, - VGRADIENT + VGRADIENT, + BUFFER }; enum Direction { diff --git a/example b/example index bd0b5c4..9f33114 100755 Binary files a/example and b/example differ diff --git a/example.cpp b/example.cpp index 3366573..d1edb9a 100644 --- a/example.cpp +++ b/example.cpp @@ -32,7 +32,7 @@ static void onKey(void *user, uint32_t keycode, uint32_t state) { int main(void) { uint16_t w = 800, h = 480; - UwU::Shader fuchsia = UwU::Shader(UwU::Direction::H, 360, 600, UwU::Color(0xb00b69ff), UwU::Color(0x00000000)); + UwU::Shader fuchsia = UwU::Shader(UwU::Direction::V, 100, 300, UwU::Color(0xb00b69ff), UwU::Color(0x00000000)); UwU::Shader red = UwU::Shader(UwU::Direction::V, 69, 420, UwU::Color(0xff0000ff), UwU::Color(0x00000000)); UwU::Shader warmup = UwU::Shader(UwU::Color(0xb00b69ff)); UwU::Color green = UwU::Color(0x00ff00ff); @@ -40,14 +40,17 @@ int main(void) { // UwU::Color alpha = UwU::Color(0x00000000); UwU::Surface surface = UwU::Surface(w, h, onKey); - // UwU::Buffer buffer = UwU::Buffer(surface.getWidth(), surface.getHeight()); + UwU::Buffer buffer = UwU::Buffer(800, 480); + UwU::Shader triangleBuffer = UwU::Shader(69, 69, buffer); UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420); UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480); UwU::Triangle triangle = UwU::Triangle(280, 100, 240, 290, 320, 300); UwU::Point point = UwU::Point(387, 43); - rectangle.draw(surface.buffer, red); + + triangle.draw(buffer, fuchsia); + rectangle.draw(surface.buffer, triangleBuffer); // Benchmarking struct timespec start, end; @@ -75,7 +78,6 @@ int main(void) { timeAvg /= BATCH; printf("Elapsed: %ld ns\n", timeAvg); - triangle.draw(surface.buffer, fuchsia); point.draw(surface.buffer, green); diff --git a/primatives/line.cpp b/primatives/line.cpp index ae0d8a6..210acf7 100644 --- a/primatives/line.cpp +++ b/primatives/line.cpp @@ -5,6 +5,7 @@ #include "../datatypes.cpp" #include "../helpers.cpp" #include "../buffer.cpp" +#include "../shader.cpp" namespace UwU { // line diff --git a/primatives/point.cpp b/primatives/point.cpp index bda72e6..861f5a0 100644 --- a/primatives/point.cpp +++ b/primatives/point.cpp @@ -5,6 +5,7 @@ #include "../datatypes.cpp" #include "../helpers.cpp" #include "../buffer.cpp" +#include "../shader.cpp" namespace UwU { // point diff --git a/primatives/rectangle.cpp b/primatives/rectangle.cpp index b072550..1f26ac3 100644 --- a/primatives/rectangle.cpp +++ b/primatives/rectangle.cpp @@ -25,8 +25,8 @@ namespace UwU { // Draw a rectangle void draw (Buffer buffer, Shader shader) { + if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; if (shader.shaderType == COLOR) { - if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; for (uint16_t y = y0; y < y1; y++) { for (uint16_t x = x0; x < x1; x++) { buffer.drawPixel(x, y, shader.c0); @@ -35,14 +35,13 @@ namespace UwU { return; } else if (shader.shaderType == HGRADIENT) { - if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; // Precompute gradient LUT Color cols[x1 - x0]; for (uint16_t x = x0; x < x1; x++) { cols[x - x0] = ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x); } - + for (uint16_t y = y0; y < y1; y++) { for (uint16_t x = x0; x < x1; x++) { buffer.drawPixel(x, y, cols[x - x0]); @@ -51,7 +50,6 @@ namespace UwU { return; } else if (shader.shaderType == VGRADIENT) { - if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; for (uint16_t y = y0; y < y1; y++) { Color c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y); for (uint16_t x = x0; x < x1; x++) { @@ -59,8 +57,16 @@ namespace UwU { } } return; + + } else if (shader.shaderType == BUFFER) { + for (uint16_t y = y0; y < y1; y++) { + for (uint16_t x = x0; x < x1; x++) { + buffer.drawPixel(x, y, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, y)); + } + } + return; } - + return; } }; } \ No newline at end of file diff --git a/primatives/triangle.cpp b/primatives/triangle.cpp index 7456d0d..acb30e6 100644 --- a/primatives/triangle.cpp +++ b/primatives/triangle.cpp @@ -6,6 +6,7 @@ #include "../datatypes.cpp" #include "../helpers.cpp" #include "../buffer.cpp" +#include "../shader.cpp" namespace UwU { class Triangle { diff --git a/shader.cpp b/shader.cpp index adb2bf1..c99036b 100644 --- a/shader.cpp +++ b/shader.cpp @@ -35,11 +35,12 @@ namespace UwU { } // BUFFER shader - // Shader(uint16_t u, uint16_t v, Buffer buffer) { - // this->shaderType = BUFFER; - // this->u0 = u; - // this->v0 = v; - // } + Shader(uint16_t u, uint16_t v, Buffer buffer) { + this->shaderType = BUFFER; + this->u0 = u; + this->v0 = v; + this->buffer = buffer; + } ShaderType shaderType; uint16_t u0; @@ -47,13 +48,23 @@ namespace UwU { Color c0; Color c1; uint32_t scale; + Buffer buffer; }; class ShaderMethods { public: - static Color gradient(uint16_t uv0, uint32_t scale, Color c0, Color c1, uint16_t uv) { - uint32_t t = (((uint32_t)uv - (uint32_t)uv0) * scale) / 0x01000000; + static Color gradient(uint16_t uv0, uint32_t scale, Color c0, Color c1, uint16_t xy) { + uint32_t t = (((uint32_t)xy - (uint32_t)uv0) * scale) / 0x01000000; return Helpers::lerpColor(c0, c1, (uint8_t)t); } + + static Color buffer(uint16_t u0, uint16_t v0, Buffer buffer, uint16_t x, uint16_t y) { + size_t i = ((size_t)y - (size_t)v0) * buffer.stride + ((size_t)x - (size_t)u0) * 4; + uint8_t b = buffer.pixels[i + 0]; + uint8_t g = buffer.pixels[i + 1]; + uint8_t r = buffer.pixels[i + 2]; + uint8_t a = buffer.pixels[i + 3]; + return Color(r, g, b, a); + } }; } \ No newline at end of file