Files
UwUGL/primatives/triangle.cpp
T

182 lines
6.0 KiB
C++

#pragma once
#include <stdint.h>
#include <algorithm>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shader.cpp"
namespace UwU {
class Triangle {
private:
public:
Triangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
this->x0 = x0;
this->y0 = y0;
this->x1 = x1;
this->y1 = y1;
this->x2 = x2;
this->y2 = y2;
}
Triangle() {}
uint16_t x0;
uint16_t y0;
uint16_t x1;
uint16_t y1;
uint16_t x2;
uint16_t y2;
// Draw a triangle
void draw (Buffer buffer, Shader shader) {
uint16_t topX, topY, midX, midY, btmX, btmY;
// Find top, middle and bottom points of triangle
if (y0 <= y1 && y0 <= y2) {
topX = x0;
topY = y0;
if (y1 < y2) {
midX = x1;
midY = y1;
btmX = x2;
btmY = y2;
} else {
midX = x2;
midY = y2;
btmX = x1;
btmY = y1;
}
} else if (y1 <= y0 && y1 <= y2) {
topX = x1;
topY = y1;
if (y0 < y2) {
midX = x0;
midY = y0;
btmX = x2;
btmY = y2;
} else {
midX = x2;
midY = y2;
btmX = x0;
btmY = y0;
}
} else {
topX = x2;
topY = y2;
if (y0 < y1) {
midY = y0;
midX = x0;
btmX = x1;
btmY = y1;
} else {
midX = x1;
midY = y1;
btmX = x0;
btmY = y0;
}
}
uint16_t height = 1 + btmY - topY;
int16_t longBound[height];
int16_t splitBound[height];
// Calculate LUT for longest vertical line (from top to bottom)
Helpers::bresenham(0, topX, height - 1, btmX, longBound);
// Calculate LUT for short two lines
Helpers::bresenham(0, topX, midY - topY, midX, splitBound);
Helpers::bresenham(midY - topY, midX, height - 1, btmX, splitBound);
if (shader.shaderType == COLOR) {
// Left-handed triangle case (midpoint is left of triangle)
if ((int16_t)midX < longBound[midY - topY]) {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, shader.c0);
}
}
// Right-handed triangle case (midpoint is right of triangle)
} else {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, shader.c0);
}
}
}
return;
} else if (shader.shaderType == HGRADIENT) {
// find left and right bounds
uint16_t leftmost = std::min(x0, std::min(x1, x2));
uint16_t rightmost = std::max(x0, std::max(x1, x2));
// precompute gradient LUT
Color cols[rightmost - leftmost];
for (uint16_t x = leftmost; x < rightmost; x++) {
cols[x - leftmost] = ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x);
}
// Left-handed triangle case (midpoint is left of triangle)
if ((int16_t)midX < longBound[midY - topY]) {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, cols[x - leftmost]);
}
}
// Right-handed triangle case (midpoint is right of triangle)
} else {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, cols[x - leftmost]);
}
}
}
return;
} else if (shader.shaderType == VGRADIENT) {
Color c;
// Left-handed triangle case (midpoint is left of triangle)
if ((int16_t)midX < longBound[midY - topY]) {
for (uint16_t i = 0; i < height; i++) {
c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, i + topY);
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, c);
}
}
// Right-handed triangle case (midpoint is right of triangle)
} else {
for (uint16_t i = 0; i < height; i++) {
c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, i + topY);
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, c);
}
}
}
return;
} else if (shader.shaderType == BUFFER) {
// Left-handed triangle case (midpoint is left of triangle)
if ((int16_t)midX < longBound[midY - topY]) {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, i + topY));
}
}
// Right-handed triangle case (midpoint is right of triangle)
} else {
for (uint16_t i = 0; i < height; i++) {
for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) {
buffer.drawPixel(x, i + (uint16_t)topY, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, i + topY));
}
}
}
return;
}
return;
}
};
}