changed coördinates to vectors, added rasterization in triangle function
This commit is contained in:
85
src/main.cpp
85
src/main.cpp
@@ -16,24 +16,24 @@ const TGAColor purple = TGAColor(150, 47, 254, 255);
|
||||
const int windowHeight = 800;
|
||||
const int windowWidth = 800;
|
||||
|
||||
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
|
||||
void line(Vec2i t0, Vec2i t1, TGAImage &image, TGAColor color) {
|
||||
bool steep = false;
|
||||
|
||||
// if the line is steep, we transpose the image.
|
||||
if (abs(x0 - x1) < abs(y0 - y1)) {
|
||||
swap(x0, y0);
|
||||
swap(x1, y1);
|
||||
if (abs(t0.x - t1.x) < abs(t0.y - t1.y)) {
|
||||
swap(t0.x, t0.y);
|
||||
swap(t1.x, t1.y);
|
||||
steep = true;
|
||||
}
|
||||
// Make it left to right.
|
||||
if (x0 > x1) {
|
||||
swap(x0, x1);
|
||||
swap(y0, y1);
|
||||
if (t0.x > t1.x) {
|
||||
swap(t0.x, t1.x);
|
||||
swap(t0.y, t1.y);
|
||||
}
|
||||
|
||||
for (int x = x0; x <= x1; x++) {
|
||||
float t = (x - x0) / (float)(x1 - x0);
|
||||
int y = y0 * (1.0 - t) + y1 * t;
|
||||
for (int x = t0.x; x <= t1.x; x++) {
|
||||
float t = (x - t0.x) / (float)(t1.x - t0.x);
|
||||
int y = t0.y * (1.0 - t) + t1.y * t;
|
||||
if (steep) {
|
||||
image.set(y, x, color); // if transposed, de-transpose.
|
||||
} else {
|
||||
@@ -42,30 +42,59 @@ void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
|
||||
}
|
||||
}
|
||||
|
||||
void triangle(int x1, int y1, int x2, int y2, int x3, int y3, TGAImage &image,
|
||||
TGAColor colour) {
|
||||
line(x1, y1, x2, y2, image, colour);
|
||||
line(x2, y2, x3, y3, image, colour);
|
||||
line(x3, y3, x1, y1, image, colour);
|
||||
void triangle(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage &image, TGAColor colour) {
|
||||
// Sorting the y-coördinates
|
||||
if (t0.y > t1.y)
|
||||
swap(t0, t1);
|
||||
if (t0.y > t2.y)
|
||||
swap(t0, t2);
|
||||
if (t1.y > t2.y)
|
||||
swap(t1, t2);
|
||||
|
||||
// Cut the triangle horizontally to render the bottom half.
|
||||
int totalHeight = t2.y - t0.y;
|
||||
for (int y = t0.y; y <= t1.y; y++) {
|
||||
int segmentHeight = t1.y - t0.y + 1;
|
||||
float alpha = (float)(y - t0.y) / totalHeight;
|
||||
float beta = (float)(y - t0.y) / segmentHeight;
|
||||
|
||||
Vec2i A = t0 + (t2 - t0) * alpha;
|
||||
Vec2i B = t0 + (t1 - t0) * beta;
|
||||
|
||||
if (A.x > B.x)
|
||||
swap(A, B);
|
||||
for (int j = A.x; j <= B.x; j++) {
|
||||
image.set(j, y, colour);
|
||||
}
|
||||
}
|
||||
|
||||
// Render the top half.
|
||||
for (int y = t1.y; y < t2.y; y++) {
|
||||
int segmentHeight = t2.y - t1.y + 1;
|
||||
float alpha = (float)(y - t0.y) / totalHeight;
|
||||
float beta = (float)(y - t1.y) / segmentHeight;
|
||||
|
||||
Vec2i A = t0 + (t2 - t0) * alpha;
|
||||
Vec2i B = t1 + (t2 - t1) * beta;
|
||||
|
||||
if (A.x > B.x)
|
||||
swap(A, B);
|
||||
for (int j = A.x; j <= B.x; j++) {
|
||||
image.set(j, y, colour);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
TGAImage image(windowWidth, windowHeight, TGAImage::RGB);
|
||||
|
||||
// Initialise random.
|
||||
srand(time(NULL));
|
||||
int x1 = rand() % (windowWidth + 1);
|
||||
int x2 = rand() % (windowWidth + 1);
|
||||
int x3 = rand() % (windowWidth + 1);
|
||||
Vec2i t0[3] = {Vec2i(20, 140), Vec2i(100, 320), Vec2i(140, 160)};
|
||||
Vec2i t1[3] = {Vec2i(360, 100), Vec2i(300, 2), Vec2i(140, 360)};
|
||||
Vec2i t2[3] = {Vec2i(360, 300), Vec2i(240, 320), Vec2i(260, 360)};
|
||||
|
||||
int y1 = rand() % (windowHeight + 1);
|
||||
int y2 = rand() % (windowHeight + 1);
|
||||
int y3 = rand() % (windowHeight + 1);
|
||||
|
||||
// Random colours.
|
||||
TGAColor colours[] = {white, purple, red};
|
||||
|
||||
triangle(x1, y1, x2, y2, x3, y3, image, colours[(rand() % 3) - 1]);
|
||||
triangle(t0[0], t0[1], t0[2], image, red);
|
||||
triangle(t1[0], t1[1], t1[2], image, white);
|
||||
triangle(t2[0], t2[1], t2[2], image, purple);
|
||||
|
||||
image.write_tga_file("output.tga");
|
||||
|
||||
|
Reference in New Issue
Block a user