Compare commits
3 Commits
c4d803389c
...
d2c76354e2
| Author | SHA1 | Date | |
|---|---|---|---|
| d2c76354e2 | |||
| 27a322d135 | |||
| 076e8d2e89 |
Binary file not shown.
@@ -40,10 +40,30 @@ namespace UwU {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
Coord2() {}
|
||||
uint32_t x;
|
||||
uint32_t y;
|
||||
};
|
||||
|
||||
|
||||
// trapezoid datatype (horizontally constrained)
|
||||
struct Trapezoid {
|
||||
Trapezoid(uint16_t y0, uint16_t y1, uint16_t xl0, uint16_t xl1, uint16_t xr0, uint16_t xr1) {
|
||||
this->y0 = y0;
|
||||
this->y1 = y1;
|
||||
this->xl0 = xl0;
|
||||
this->xl1 = xl1;
|
||||
this->xr0 = xr0;
|
||||
this->xr1 = xr1;
|
||||
}
|
||||
Trapezoid() {}
|
||||
uint16_t y0;
|
||||
uint16_t y1;
|
||||
uint16_t xl0;
|
||||
uint16_t xl1;
|
||||
uint16_t xr0;
|
||||
uint16_t xr1;
|
||||
};
|
||||
|
||||
class Draw {
|
||||
private:
|
||||
|
||||
@@ -75,6 +95,71 @@ namespace UwU {
|
||||
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;
|
||||
}
|
||||
|
||||
// plot lines if m <= 1
|
||||
void lineLow(int32_t x0, int32_t y0, int32_t x1, int32_t y1, Color color) {
|
||||
int32_t dx = x1 - x0;
|
||||
@@ -121,37 +206,19 @@ namespace UwU {
|
||||
return;
|
||||
}
|
||||
|
||||
// plot gradients' lerp t value if m <= 1
|
||||
void gradLow(int32_t x1, uint8_t* gradLut) {
|
||||
int32_t dx = x1;
|
||||
int32_t dy = 256;
|
||||
int32_t d = (2 * dy) - dx;
|
||||
int32_t y = 0;
|
||||
for (int32_t x = 0; x < x1; x++) {
|
||||
gradLut[x] = (uint8_t)y;
|
||||
if (d > 0) {
|
||||
y++;
|
||||
d += (2 * (dy -dx));
|
||||
} else {
|
||||
d += (2 * dy);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// plot gradients' lerp t value if m >1
|
||||
void gradHigh(int32_t x1, uint8_t* gradLut) {
|
||||
int32_t dx = x1;
|
||||
int32_t dy = 256;
|
||||
int32_t d = (2 * dx) - dy;
|
||||
int32_t x = 0;
|
||||
for (int32_t y = 0; y < 256; y++) {
|
||||
gradLut[x] = (uint8_t)y;
|
||||
if (d > 0) {
|
||||
x++;
|
||||
d += (2 * (dx -dy));
|
||||
} else {
|
||||
d += (2 * dx);
|
||||
// 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;
|
||||
@@ -211,34 +278,24 @@ namespace UwU {
|
||||
}
|
||||
|
||||
// setup gradient coefficient arrays
|
||||
int32_t w = x1 - x0;
|
||||
int32_t h = y1 - y0;
|
||||
uint8_t xGrad[w];
|
||||
uint8_t yGrad[h];
|
||||
int32_t w = x1 - x0 + 1;
|
||||
int32_t h = y1 - y0 + 1;
|
||||
int16_t xGrad[w];
|
||||
int16_t yGrad[h];
|
||||
|
||||
// determine m and calculate horizontal gradient coefficients
|
||||
if (w < 256) {
|
||||
this->gradHigh(w, xGrad);
|
||||
} else {
|
||||
this->gradLow(w, xGrad);
|
||||
}
|
||||
|
||||
// determine m and calculate vertical gradient coefficients
|
||||
if (h < 256) {
|
||||
this->gradHigh(h, yGrad);
|
||||
} else {
|
||||
this->gradLow(h, yGrad);
|
||||
}
|
||||
// calculate gradient coefficients
|
||||
this->bresenham(0, w - 1, 255, xGrad);
|
||||
this->bresenham(0, h - 1, 255, yGrad);
|
||||
|
||||
// for each point calculate, alpha blend and print color
|
||||
Color Lmix = Color();
|
||||
Color Rmix = Color();
|
||||
Color Xmix = Color();
|
||||
for (int32_t y = y0; y < y1; y++) {
|
||||
Lmix = this->lerpColor(colorUL, colorLL, yGrad[y - y0]);
|
||||
Rmix = this->lerpColor(colorUR, colorLR, yGrad[y - y0]);
|
||||
for (int32_t x = x0; x < x1; x++) {
|
||||
Xmix = this->lerpColor(Lmix, Rmix, xGrad[x - x0]);
|
||||
for (int32_t y = y0; y <= y1; y++) {
|
||||
Lmix = this->lerpColor(colorUL, colorLL, (uint8_t)yGrad[y - y0]);
|
||||
Rmix = this->lerpColor(colorUR, colorLR, (uint8_t)yGrad[y - y0]);
|
||||
for (int32_t x = x0; x <= x1; x++) {
|
||||
Xmix = this->lerpColor(Lmix, Rmix, (uint8_t)xGrad[x - x0]);
|
||||
putPixel(x, y, Xmix.r, Xmix.g, Xmix.b, Xmix.a);
|
||||
}
|
||||
}
|
||||
@@ -285,6 +342,39 @@ namespace UwU {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draws a horizontally bounded trapezoid
|
||||
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(saneCoords, color);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -306,7 +396,7 @@ 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(500, 400, 360, 360, 600, 401), green);
|
||||
|
||||
|
||||
draw.point(UwU::Coord2(387, 43), green);
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user