generalized bresenham LUT generation method
This commit is contained in:
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,66 @@ 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(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 d = (2 * dx) - dy;
|
||||
int16_t x = 0;
|
||||
for (int16_t y = y0; y <= y1; y++) {
|
||||
LUT[x] = y;
|
||||
if (d > 0) {
|
||||
x++;
|
||||
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(y1, x1, y0, LUT);
|
||||
} else {
|
||||
bresenhamHigh(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,42 +201,6 @@ 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);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
struct waymini* wm;
|
||||
uint32_t width;
|
||||
@@ -211,34 +255,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 +319,12 @@ namespace UwU {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw a horizontally bounded trapezoid
|
||||
// coords.y1 >= coords.y0
|
||||
void trapezoid (Trapezoid coords, Color color) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user