added gradient shaders to line
This commit is contained in:
+73
-16
@@ -11,7 +11,7 @@ namespace UwU {
|
||||
class Line {
|
||||
private:
|
||||
// plot lines if m <= 1
|
||||
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Color color) {
|
||||
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Shader shader) {
|
||||
int16_t dx = ix1 - ix0;
|
||||
int16_t dy = iy1 - iy0;
|
||||
int16_t yi = 1;
|
||||
@@ -21,20 +21,48 @@ namespace UwU {
|
||||
}
|
||||
int16_t d = (2 * dy) - dx;
|
||||
int16_t y = iy0;
|
||||
for (int16_t x = ix0; x < ix1; x++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, color);
|
||||
if (d > 0) {
|
||||
y += yi;
|
||||
d += (2 * (dy - dx));
|
||||
} else {
|
||||
d += (2 * dy);
|
||||
|
||||
if (shader.shaderType == COLOR) {
|
||||
for (int16_t x = ix0; x < ix1; x++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.c0);
|
||||
if (d > 0) {
|
||||
y += yi;
|
||||
d += (2 * (dy - dx));
|
||||
} else {
|
||||
d += (2 * dy);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
} else if (shader.shaderType == HGRADIENT) {
|
||||
for (int16_t x = ix0; x < ix1; x++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x));
|
||||
if (d > 0) {
|
||||
y += yi;
|
||||
d += (2 * (dy - dx));
|
||||
} else {
|
||||
d += (2 * dy);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
} else if (shader.shaderType == VGRADIENT) {
|
||||
for (int16_t x = ix0; x < ix1; x++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y));
|
||||
if (d > 0) {
|
||||
y += yi;
|
||||
d += (2 * (dy - dx));
|
||||
} else {
|
||||
d += (2 * dy);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// plot lines if m >1
|
||||
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Color color) {
|
||||
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Shader shader) {
|
||||
int16_t dx = ix1 - ix0;
|
||||
int16_t dy = iy1 - iy0;
|
||||
int16_t xi = 1;
|
||||
@@ -44,8 +72,10 @@ namespace UwU {
|
||||
}
|
||||
int16_t d = (2 * dx) - dy;
|
||||
int16_t x = ix0;
|
||||
for (int16_t y = iy0; y < iy1; y++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, color);
|
||||
|
||||
if (shader.shaderType == COLOR) {
|
||||
for (int16_t y = iy0; y < iy1; y++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.c0);
|
||||
if (d > 0) {
|
||||
x += xi;
|
||||
d += (2 * (dx - dy));
|
||||
@@ -54,6 +84,33 @@ namespace UwU {
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
} else if (shader.shaderType == HGRADIENT) {
|
||||
for (int16_t y = iy0; y < iy1; y++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x));
|
||||
if (d > 0) {
|
||||
x += xi;
|
||||
d += (2 * (dx - dy));
|
||||
} else {
|
||||
d += (2 * dx);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
} else if (shader.shaderType == VGRADIENT) {
|
||||
for (int16_t y = iy0; y < iy1; y++) {
|
||||
buffer.drawPixel((uint16_t)x, (uint16_t)y, ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y));
|
||||
if (d > 0) {
|
||||
x += xi;
|
||||
d += (2 * (dx - dy));
|
||||
} else {
|
||||
d += (2 * dx);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -70,7 +127,7 @@ namespace UwU {
|
||||
uint16_t y1;
|
||||
|
||||
// Uses Bresenham's line algorithm to draw line of any slope
|
||||
void draw (Buffer buffer, Color color) {
|
||||
void draw (Buffer buffer, Shader shader) {
|
||||
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
|
||||
int16_t ix0 = x0;
|
||||
int16_t iy0 = y0;
|
||||
@@ -78,15 +135,15 @@ namespace UwU {
|
||||
int16_t iy1 = y1;
|
||||
if (abs(iy1 - iy0) < abs(ix1 - ix0)) {
|
||||
if (ix0 > ix1) {
|
||||
lineLow(ix1, iy1, ix0, iy0, buffer, color);
|
||||
lineLow(ix1, iy1, ix0, iy0, buffer, shader);
|
||||
} else {
|
||||
lineLow(ix0, iy0, ix1, iy1, buffer, color);
|
||||
lineLow(ix0, iy0, ix1, iy1, buffer, shader);
|
||||
}
|
||||
} else {
|
||||
if (iy0 > iy1) {
|
||||
lineHigh(ix1, iy1, ix0, iy0, buffer, color);
|
||||
lineHigh(ix1, iy1, ix0, iy0, buffer, shader);
|
||||
} else {
|
||||
lineHigh(ix0, iy0, ix1, iy1, buffer, color);
|
||||
lineHigh(ix0, iy0, ix1, iy1, buffer, shader);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user