created buffer shader and added shader logic to rectangle
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
+34
-33
@@ -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 {
|
||||
|
||||
+6
-4
@@ -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);
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "../datatypes.cpp"
|
||||
#include "../helpers.cpp"
|
||||
#include "../buffer.cpp"
|
||||
#include "../shader.cpp"
|
||||
|
||||
namespace UwU {
|
||||
// line
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "../datatypes.cpp"
|
||||
#include "../helpers.cpp"
|
||||
#include "../buffer.cpp"
|
||||
#include "../shader.cpp"
|
||||
|
||||
namespace UwU {
|
||||
// point
|
||||
|
||||
@@ -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,7 +35,6 @@ 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];
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "../datatypes.cpp"
|
||||
#include "../helpers.cpp"
|
||||
#include "../buffer.cpp"
|
||||
#include "../shader.cpp"
|
||||
|
||||
namespace UwU {
|
||||
class Triangle {
|
||||
|
||||
+18
-7
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user