got triangles kinda working... will probably refactor
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "datatypes.cpp"
|
||||
#include "helpers.cpp"
|
||||
// #include "triangle.cpp"
|
||||
|
||||
// this should be handled elsewhere, eventually, maybe, probably
|
||||
static void on_key(void *user, uint32_t keycode, uint32_t state) {
|
||||
@@ -25,83 +26,20 @@ namespace UwU {
|
||||
class Draw {
|
||||
private:
|
||||
|
||||
uint8_t *pixels;
|
||||
|
||||
void putPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0xff) {
|
||||
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
|
||||
uint8_t b0 = this->pixels[i + 0];
|
||||
uint8_t g0 = this->pixels[i + 1];
|
||||
uint8_t r0 = this->pixels[i + 2];
|
||||
size_t i = (size_t)y * waymini_get_stride(wm) + (size_t)x * 4;
|
||||
uint8_t b0 = pixels[i + 0];
|
||||
uint8_t g0 = pixels[i + 1];
|
||||
uint8_t r0 = pixels[i + 2];
|
||||
b = Helpers::lerp8(b0, b, a);
|
||||
g = Helpers::lerp8(g0, g, a);
|
||||
r = Helpers::lerp8(r0, r, a);
|
||||
this->pixels[i + 0] = b; // B
|
||||
this->pixels[i + 1] = g; // G
|
||||
this->pixels[i + 2] = r; // R
|
||||
this->pixels[i + 3] = 0xff; // A
|
||||
}
|
||||
|
||||
void bresenhamLow(int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
|
||||
int16_t dx = int16_t(x1);
|
||||
int16_t dy = y1 - y0;
|
||||
int16_t yi = 1;
|
||||
if (dy < 0) {
|
||||
yi = -1;
|
||||
dy = -dy;
|
||||
}
|
||||
int16_t d = (2 * dy) - dx;
|
||||
int16_t y = y0;
|
||||
for (uint16_t x = 0; x <= x1; x++) {
|
||||
LUT[x] = y;
|
||||
if (d > 0) {
|
||||
y += yi;
|
||||
d += (2 * (dy - dx));
|
||||
} else {
|
||||
d += (2 * dy);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void bresenhamHigh(uint16_t x0, int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
|
||||
int16_t dx = (int16_t)x1 - (int16_t)x0;
|
||||
int16_t dy = y1 - y0;
|
||||
int16_t xi = 1;
|
||||
if (dx < 0) {
|
||||
xi = -1;
|
||||
dx = -dx;
|
||||
}
|
||||
int16_t d = (2 * dx) - dy;
|
||||
int16_t x = (int16_t)x0;
|
||||
for (int16_t y = y0; y <= y1; y++) {
|
||||
LUT[x] = y;
|
||||
if (d > 0) {
|
||||
x += xi;
|
||||
d += (2 * (dx - dy));
|
||||
} else {
|
||||
d += (2 * dx);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void bresenham(int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
|
||||
int16_t dy = y1 - y0;
|
||||
if (dy == 0) {
|
||||
// catch hline condition
|
||||
for (uint16_t i = 0; i <= x1; i++) {
|
||||
LUT[i] = y0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (abs(y1 - y0) <= x1) {
|
||||
bresenhamLow(y0, x1, y1, LUT);
|
||||
} else {
|
||||
if (y0 > y1) {
|
||||
bresenhamHigh(x1, y1, 0, y0, LUT);
|
||||
} else {
|
||||
bresenhamHigh(0, y0, x1, y1, LUT);
|
||||
}
|
||||
}
|
||||
return;
|
||||
pixels[i + 0] = b; // B
|
||||
pixels[i + 1] = g; // G
|
||||
pixels[i + 2] = r; // R
|
||||
pixels[i + 3] = 0xff; // A
|
||||
}
|
||||
|
||||
// plot lines if m <= 1
|
||||
@@ -149,40 +87,18 @@ namespace UwU {
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Draw a horizontally bounded trapezoid
|
||||
// coords.y1 >= coords.y0
|
||||
void pTrapezoid (Trapezoid coords, Color color) {
|
||||
uint16_t height = 1 + coords.y1 - coords.y0;
|
||||
int16_t LeftBound[height];
|
||||
int16_t RightBound[height];
|
||||
|
||||
this->bresenham(coords.xl0, coords.y1 - coords.y0, coords.xl1, LeftBound);
|
||||
this->bresenham(coords.xr0, coords.y1 - coords.y0, coords.xr1, RightBound);
|
||||
|
||||
for (uint16_t i = 0; i <= height; i++) {
|
||||
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
|
||||
this->putPixel((uint32_t)x, (uint32_t)i + coords.y0, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
struct waymini* wm;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t stride;
|
||||
uint8_t *pixels;
|
||||
|
||||
|
||||
Draw(uint32_t width, uint32_t height, waymini_key_cb on_key) {
|
||||
this->wm = waymini_create(width, height, on_key, NULL);
|
||||
if (!this->wm) exit(1);
|
||||
|
||||
this->width = waymini_get_width(this->wm);
|
||||
this->height = waymini_get_height(this->wm);
|
||||
this->stride = waymini_get_stride(this->wm);
|
||||
printf("surface: %ux%u stride=%u\n", this->width, this->height, this->stride);
|
||||
// this->width = waymini_get_width(this->wm);
|
||||
// this->height = waymini_get_height(this->wm);
|
||||
// this->stride = waymini_get_stride(this->wm);
|
||||
printf("surface: %ux%u stride=%u\n", waymini_get_width(this->wm), waymini_get_height(this->wm), waymini_get_stride(this->wm));
|
||||
|
||||
this->pixels = (uint8_t *)waymini_get_pixels(this->wm);
|
||||
if (!this->pixels) {
|
||||
@@ -190,10 +106,34 @@ namespace UwU {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
struct waymini* wm;
|
||||
|
||||
uint32_t getWidth() {
|
||||
return waymini_get_width(wm);
|
||||
}
|
||||
|
||||
uint32_t getHeight() {
|
||||
return waymini_get_height(wm);
|
||||
}
|
||||
|
||||
uint32_t getStride() {
|
||||
return waymini_get_stride(wm);
|
||||
}
|
||||
|
||||
void present() {
|
||||
waymini_present(wm);
|
||||
return;
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
waymini_destroy(wm);
|
||||
return;
|
||||
}
|
||||
|
||||
// Draw a rectangle (duh)
|
||||
void rectangle(Coord2 point0, Coord2 point1, Color color) {
|
||||
if (point0.x > this->width || point0.y > this->height || point1.x > this->width || point1.y > this->height) {return;};
|
||||
if (point0.x > getWidth() || point0.y > getHeight() || point1.x > getWidth() || point1.y > getHeight()) {return;};
|
||||
for (uint32_t y = point0.y; y < point1.y; y++) {
|
||||
for (uint32_t x = point0.x; x < point1.x; x++) {
|
||||
putPixel(x, y, color.r, color.g, color.b, color.a);
|
||||
@@ -228,8 +168,8 @@ namespace UwU {
|
||||
int16_t yGrad[h];
|
||||
|
||||
// calculate gradient coefficients
|
||||
this->bresenham(0, w - 1, 255, xGrad);
|
||||
this->bresenham(0, h - 1, 255, yGrad);
|
||||
Helpers::bresenham(0, w - 1, 255, xGrad);
|
||||
Helpers::bresenham(0, h - 1, 255, yGrad);
|
||||
|
||||
// for each point calculate, alpha blend and print color
|
||||
Color Lmix = Color();
|
||||
@@ -248,77 +188,159 @@ namespace UwU {
|
||||
|
||||
// Fill a simple gradient. Each row is stride bytes.
|
||||
void rainbow() {
|
||||
for (uint32_t y = 0; y < this->height; y++) {
|
||||
for (uint32_t x = 0; x < this->width; x++) {
|
||||
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
|
||||
this->pixels[i + 0] = (uint8_t)(y * 255 / this->height); // B
|
||||
this->pixels[i + 1] = 0x00; // G
|
||||
this->pixels[i + 2] = (uint8_t)(x * 255 / this->width); // R
|
||||
this->pixels[i + 3] = 0xFF; // A
|
||||
for (uint32_t y = 0; y < getHeight(); y++) {
|
||||
for (uint32_t x = 0; x < getWidth(); x++) {
|
||||
size_t i = (size_t)y * getStride() + (size_t)x * 4;
|
||||
pixels[i + 0] = (uint8_t)(y * 255 / getHeight()); // B
|
||||
pixels[i + 1] = 0x00; // G
|
||||
pixels[i + 2] = (uint8_t)(x * 255 / getWidth()); // R
|
||||
pixels[i + 3] = 0xFF; // A
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw a single point
|
||||
void point (Coord2 point, Color color) {
|
||||
if (point.x > this->width || point.y > this->height) {return;};
|
||||
if (point.x > getWidth() || point.y > getHeight()) {return;};
|
||||
putPixel(point.x, point.y, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
// Uses Bresenham's line algorithm to draw line of any slope
|
||||
void line (Coord2 point0, Coord2 point1, Color color) {
|
||||
if (point0.x > this->width || point0.y > this->height || point1.x > this->width || point1.y > this->height) {return;};
|
||||
if (point0.x > getWidth() || point0.y > getHeight() || point1.x > getWidth() || point1.y > getHeight()) {return;};
|
||||
int32_t x0 = (int32_t)point0.x;
|
||||
int32_t y0 = (int32_t)point0.y;
|
||||
int32_t x1 = (int32_t)point1.x;
|
||||
int32_t y1 = (int32_t)point1.y;
|
||||
if (abs(y1 - y0) < abs(x1 - x0)) {
|
||||
if (x0 > x1) {
|
||||
this->lineLow(x1, y1, x0, y0, color);
|
||||
lineLow(x1, y1, x0, y0, color);
|
||||
} else {
|
||||
this->lineLow(x0, y0, x1, y1, color);
|
||||
lineLow(x0, y0, x1, y1, color);
|
||||
}
|
||||
} else {
|
||||
if (y0 > y1) {
|
||||
this->lineHigh(x1, y1, x0, y0, color);
|
||||
lineHigh(x1, y1, x0, y0, color);
|
||||
} else {
|
||||
this->lineHigh(x0, y0, x1, y1, color);
|
||||
lineHigh(x0, y0, x1, y1, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draws a horizontally bounded trapezoid
|
||||
// TODO: FIX
|
||||
// Draw a horizontally bounded trapezoid
|
||||
// coords.y1 >= coords.y0
|
||||
// todo move to private and add public checks
|
||||
void trapezoid (Trapezoid coords, Color color) {
|
||||
// UwU::Trapezoid saneCoords = coords;
|
||||
// if (coords.y0 > coords.y1) {
|
||||
// saneCoords.y0 = coords.y1;
|
||||
// saneCoords.y1 = coords.y0;
|
||||
// saneCoords.xl0 = coords.xl1;
|
||||
// saneCoords.xl1 = coords.xl0;
|
||||
// saneCoords.xr0 = coords.xr1;
|
||||
// saneCoords.xr1 = coords.xr0;
|
||||
// }
|
||||
// uint16_t tempVal;
|
||||
// bool hflip0 = false;
|
||||
// if (saneCoords.xl0 > saneCoords.xr0) {
|
||||
// tempVal = saneCoords.xl0;
|
||||
// saneCoords.xl0 = saneCoords.xr0;
|
||||
// saneCoords.xr0 = tempVal;
|
||||
// hflip0 = true;
|
||||
// }
|
||||
// if (saneCoords.xl1 > saneCoords.xr1) {
|
||||
// if (!hflip0) {
|
||||
// // invalid geometry; emit console warning and don't attempt to draw
|
||||
// // possible options are: don't draw, lazy draw (incorrect geometry) -
|
||||
// // or split into two (way more work)
|
||||
// return;
|
||||
// }
|
||||
// tempVal = saneCoords.xl1;
|
||||
// saneCoords.xl1 = saneCoords.xr1;
|
||||
// saneCoords.xr1 = tempVal;
|
||||
// }
|
||||
this->pTrapezoid(coords, color);
|
||||
printf("(%u, %u, %u, %u, %u, %u)", coords.y0, coords.y1, coords.xl0, coords.xl1, coords.xr0, coords.xr1);
|
||||
uint16_t height = 1 + coords.y1 - coords.y0;
|
||||
int16_t LeftBound[height];
|
||||
int16_t RightBound[height];
|
||||
|
||||
Helpers::bresenham(coords.xl0, coords.y1 - coords.y0, coords.xl1, LeftBound);
|
||||
Helpers::bresenham(coords.xr0, coords.y1 - coords.y0, coords.xr1, RightBound);
|
||||
|
||||
for (uint16_t i = 0; i <= height; i++) {
|
||||
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
|
||||
// printf("(%u, %u)", x, i + coords.y0);
|
||||
putPixel((uint32_t)x, (uint32_t)i + coords.y0, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Draw a triangle
|
||||
void triangle (Triangle coords, Color color) {
|
||||
Coord2 top = Coord2();
|
||||
Coord2 mid = Coord2();
|
||||
Coord2 btm = Coord2();
|
||||
// cases: flat top, flat bottom, left point, right point
|
||||
if (coords.y0 < coords.y1 && coords.y0 < coords.y2) {
|
||||
top.x = coords.x0;
|
||||
top.y = coords.y0;
|
||||
if (coords.y1 < coords.y2) {
|
||||
mid.x = coords.x1;
|
||||
mid.y = coords.y1;
|
||||
btm.x = coords.x2;
|
||||
btm.y = coords.y2;
|
||||
} else {
|
||||
mid.x = coords.x2;
|
||||
mid.y = coords.y2;
|
||||
btm.x = coords.x1;
|
||||
btm.y = coords.y1;
|
||||
}
|
||||
} else if (coords.y1 < coords.y0 && coords.y1 < coords.y2) {
|
||||
top.x = coords.x1;
|
||||
top.y = coords.y1;
|
||||
if (coords.y0 < coords.y2) {
|
||||
mid.x = coords.x0;
|
||||
mid.y = coords.y0;
|
||||
btm.x = coords.x2;
|
||||
btm.y = coords.y2;
|
||||
} else {
|
||||
mid.x = coords.x2;
|
||||
mid.y = coords.y2;
|
||||
btm.x = coords.x0;
|
||||
btm.y = coords.y0;
|
||||
}
|
||||
} else {
|
||||
top.x = coords.x2;
|
||||
top.y = coords.y2;
|
||||
if (coords.y0 < coords.y1) {
|
||||
mid.x = coords.x0;
|
||||
mid.y = coords.y0;
|
||||
btm.x = coords.x1;
|
||||
btm.y = coords.y1;
|
||||
} else {
|
||||
mid.x = coords.x1;
|
||||
mid.y = coords.y1;
|
||||
btm.x = coords.x0;
|
||||
btm.y = coords.y0;
|
||||
}
|
||||
}
|
||||
|
||||
// handle case where top or bottom is flat
|
||||
if (mid.y == top.y) {
|
||||
// flat top
|
||||
if (mid.x < top.x) {
|
||||
trapezoid(Trapezoid(top.y, btm.y, mid.x, btm.x, top.x, btm.x), color);
|
||||
return;
|
||||
} else {
|
||||
trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, mid.x, btm.x), color);
|
||||
return;
|
||||
}
|
||||
} else if (mid.y == btm.y) {
|
||||
// flat bottom
|
||||
if (mid.x < btm.x) {
|
||||
trapezoid(Trapezoid(top.y, btm.y, top.x, mid.x, top.x, btm.x), color);
|
||||
return;
|
||||
} else {
|
||||
trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, top.x, mid.x), color);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// calculate midpoint of straight line
|
||||
int32_t dy = (int32_t)btm.y - (int32_t)top.y;
|
||||
if (dy == 0) {
|
||||
return; // shouldn't be possible, should be detected earlier
|
||||
}
|
||||
int32_t dx = (int32_t)btm.x - (int32_t)top.x;
|
||||
dx *= (mid.y - top.y);
|
||||
dx /= dy;
|
||||
dx += top.x;
|
||||
uint16_t oppx = (uint16_t)dx;
|
||||
|
||||
// split triangle into two trapezoids
|
||||
if (mid.x < oppx) {
|
||||
trapezoid(Trapezoid(top.y, mid.y, top.x, mid.x, top.x, oppx), color);
|
||||
trapezoid(Trapezoid(mid.y, btm.y, mid.x, btm.x, oppx, btm.x), color);
|
||||
return;
|
||||
} else {
|
||||
trapezoid(Trapezoid(top.y, mid.y, top.x, oppx, top.x, mid.x), color);
|
||||
trapezoid(Trapezoid(mid.y, btm.y, oppx, btm.x, mid.x, btm.x), color);
|
||||
return;
|
||||
}
|
||||
return; // nothing should ever reach this
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -341,7 +363,8 @@ int main(void) {
|
||||
draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia);
|
||||
draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
|
||||
|
||||
// draw.trapezoid(UwU::Trapezoid(400, 500, 360, 360, 400, 601), green);
|
||||
draw.trapezoid(UwU::Trapezoid(300, 400, 360, 380, 600, 400), green);
|
||||
draw.triangle(UwU::Triangle(200, 100, 240, 200, 320, 300), green);
|
||||
|
||||
|
||||
draw.point(UwU::Coord2(387, 43), green);
|
||||
@@ -353,12 +376,12 @@ int main(void) {
|
||||
draw.line(UwU::Coord2(250, 300), UwU::Coord2(100, 469), green);
|
||||
draw.line(UwU::Coord2(100, 300), UwU::Coord2(469, 250), green);
|
||||
draw.line(UwU::Coord2(469, 300), UwU::Coord2(100, 250), green);
|
||||
waymini_present(draw.wm);
|
||||
draw.present();
|
||||
|
||||
while (!waymini_should_close(draw.wm)) {
|
||||
if (waymini_dispatch(draw.wm) < 0) break;
|
||||
}
|
||||
|
||||
waymini_destroy(draw.wm);
|
||||
draw.destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user