add triangle
This commit is contained in:
45
src/main.cpp
45
src/main.cpp
@@ -2,6 +2,9 @@
|
|||||||
#include "model.h"
|
#include "model.h"
|
||||||
#include "tgaimage.h"
|
#include "tgaimage.h"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <time.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -13,8 +16,6 @@ const TGAColor purple = TGAColor(150, 47, 254, 255);
|
|||||||
const int windowHeight = 800;
|
const int windowHeight = 800;
|
||||||
const int windowWidth = 800;
|
const int windowWidth = 800;
|
||||||
|
|
||||||
Model *model = NULL;
|
|
||||||
|
|
||||||
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
|
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
|
||||||
bool steep = false;
|
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) {
|
int main(int argc, char **argv) {
|
||||||
model = new Model("../obj/head.obj");
|
|
||||||
|
|
||||||
TGAImage image(windowWidth, windowHeight, TGAImage::RGB);
|
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++) {
|
// Initialise random.
|
||||||
Vec3f v0 = model->vert(face[j]);
|
srand(time(NULL));
|
||||||
Vec3f v1 = model->vert(face[(j + 1) % 3]);
|
int x1 = rand() % (windowWidth + 1);
|
||||||
int x0 = (v0.x + 1.0) * windowWidth / 2.0;
|
int x2 = rand() % (windowWidth + 1);
|
||||||
int y0 = (v0.y + 1.0) * windowHeight / 2.0;
|
int x3 = rand() % (windowWidth + 1);
|
||||||
int x1 = (v1.x + 1.0) * windowWidth / 2.0;
|
|
||||||
int y1 = (v1.y + 1.0) * windowHeight / 2.0;
|
int y1 = rand() % (windowHeight + 1);
|
||||||
line(x0, y0, x1, y1, image, purple);
|
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");
|
image.write_tga_file("output.tga");
|
||||||
delete model;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user