-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (59 loc) · 2.2 KB
/
main.cpp
File metadata and controls
71 lines (59 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "tgaimage.h"
#include "pen.h"
#include "model.h"
#include "geometry.h"
const TGAColor white = TGAColor(255, 255, 255, 255);
Model *model = NULL;
const int width = 200;
const int height = 200;
int main(int argc, char** argv) {
TGAImage image(width, height, TGAImage::RGB);
// Default rotation angles and zoom
float rot_x = 0, rot_y = 0, rot_z = 0;
float zoom = 1.0f;
const char* model_path = "obj/african_head.obj";
// Parse command line arguments
// Usage: ./main [model.obj] [x_deg] [y_deg] [z_deg] [zoom]
// Or: ./main [x_deg] [y_deg] [z_deg] [zoom] (uses default model)
if (argc >= 2) {
// Check if first arg is a number (rotation) or a filename
char* endptr;
float test = strtof(argv[1], &endptr);
if (*endptr == '\0') {
// First arg is a number, so it's rotation_x
rot_x = test;
if (argc >= 3) rot_y = strtof(argv[2], NULL);
if (argc >= 4) rot_z = strtof(argv[3], NULL);
if (argc >= 5) zoom = strtof(argv[4], NULL);
} else {
// First arg is a filename
model_path = argv[1];
if (argc >= 3) rot_x = strtof(argv[2], NULL);
if (argc >= 4) rot_y = strtof(argv[3], NULL);
if (argc >= 5) rot_z = strtof(argv[4], NULL);
if (argc >= 6) zoom = strtof(argv[5], NULL);
}
}
model = new Model(model_path);
for (int i=0; i<model->nfaces(); i++) {
std::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]);
// Apply rotations
v0 = rotateVec(v0, rot_x, rot_y, rot_z);
v1 = rotateVec(v1, rot_x, rot_y, rot_z);
// Apply zoom (scale)
v0 = v0 * zoom;
v1 = v1 * zoom;
int x0 = (v0.x+1.)*width/2.;
int y0 = (v0.y+1.)*height/2.;
int x1 = (v1.x+1.)*width/2.;
int y1 = (v1.y+1.)*height/2.;
drawLine(x0, y0, x1, y1, image, white);
}
}
delete model;
image.write_tga_file("output.tga");
return 0;
}