add triangle

This commit is contained in:
CJSatnarine
2025-04-03 21:38:26 -04:00
parent aa64290ce1
commit 527d3f68b8

View File

@@ -2,6 +2,9 @@
#include "model.h"
#include "tgaimage.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <vector>
using namespace std;
@@ -13,8 +16,6 @@ const TGAColor purple = TGAColor(150, 47, 254, 255);
const int windowHeight = 800;
const int windowWidth = 800;
Model *model = NULL;
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
bool steep = false;
@@ -41,28 +42,32 @@ 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);
}
int main(int argc, char **argv) {
model = new Model("../obj/head.obj");
TGAImage image(windowWidth, windowHeight, TGAImage::RGB);
// Iterate for the number of faces.
for (int i = 0; i < model->nfaces(); i++) {
// Set each face to be part of the vector.
vector<int> face = model->face(i);
for (int j = 0; j < 3; j++) {
Vec3f v0 = model->vert(face[j]);
Vec3f v1 = model->vert(face[(j + 1) % 3]);
int x0 = (v0.x + 1.0) * windowWidth / 2.0;
int y0 = (v0.y + 1.0) * windowHeight / 2.0;
int x1 = (v1.x + 1.0) * windowWidth / 2.0;
int y1 = (v1.y + 1.0) * windowHeight / 2.0;
line(x0, y0, x1, y1, image, purple);
}
}
// Initialise random.
srand(time(NULL));
int x1 = rand() % (windowWidth + 1);
int x2 = rand() % (windowWidth + 1);
int x3 = rand() % (windowWidth + 1);
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]);
image.flip_vertically(); // Origin at bottom left of image.
image.write_tga_file("output.tga");
delete model;
return 0;
}