-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
125 lines (117 loc) · 4.22 KB
/
main.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "tgaimage.h"
#include <iostream>
#include <regex>
#include <fstream>
#include <vector>
#include <math.h>
#include "vec3.h"
#include "display.h"
// f -> triangles, premier sommet c'est 1 pas 0, il faut décrementer.
const int depth = 255;
//for the output.
int width = 800 ;
int height = 800 ;
// to be displayed
vec3 eye = vec3({1,1,3});
vec3 center = vec3({0,0,0});
vec3 up = {0,1,0};
std::string path_struct = "diablo3_pose/diablo3_pose.obj";
std::string path_texture = "diablo3_pose/diablo3_pose_diffuse.tga";
std::string path_shadder = "diablo3_pose/diablo3_pose_nm.tga";
//int argc, char** argv
int main(int argc, char** argv) {
/// arg1 : path_struct, arg2 : path_texture, arg3 : eye, arg4 : center -> canvas dimension ?
// dimensions
constexpr int t_width = 1023;
constexpr int t_height = 1023;
// arg input.
if (argc == 5){
path_struct = argv[1];
path_texture = argv[2];
/* eye = argv[3];
center = argv[4];*/
}else {
std::cout << " le nombre d'arguments rentrée est insufisant : " << argc<<"\n Arguments par défaut pris. \n";
}
//FILE PROSSESSING
std::ifstream file(path_struct);
std::string myText;
std::vector<vec3 > vertices;
std::vector<vec3 > vertices_texture;
std::vector<vec3 > vertice_normals;
std::vector<vec3> faces;
std::vector<vec3> t_faces;
std::string type;
while ( file >> type ){
if( type == "v"){
double x, y, z;
file >> x >> y >> z ;
vec3 v = {x,y,z};
vertices.push_back(v);
}
if( type == "vt"){
double x, y, z;
file >> x >> y >> z ;
vec3 v = {std::round(x*t_width),std::round(y*t_height),std::round(z*t_height)};
vertices_texture.push_back(v);
}
if( type == "vn"){
double x, y, z;
file >> x >> y >> z ;
vec3 v = {x,y,z};
vertice_normals.push_back(v);
}
if( type == "f"){
double x, y, z, tx ,ty, tz;
std::string coord;
file >> coord;
x = std::stoi(coord.substr(0,coord.find("/")));
coord = coord.substr(coord.find("/")+1,coord.length());
tx = std::stoi(coord.substr(0,coord.find("/")));
file >> coord;
y = std::stoi(coord.substr(0,coord.find("/")));
coord = coord.substr(coord.find("/")+1,coord.length());
ty = std::stoi(coord.substr(0,coord.find("/")));
file >> coord;
z = std::stoi(coord.substr(0,coord.find("/")));
coord = coord.substr(coord.find("/")+1,coord.length());
tz = std::stoi(coord.substr(0,coord.find("/")));
vec3 v = {x-1,y-1,z-1};
faces.push_back(v);
vec3 tv = {tx-1,ty-1,tz-1};
t_faces.push_back(tv);
}
}
file.close();
//END FILE PROSSESSING
TGAImage framebuffer(width, height, TGAImage::RGB);
//load background.
TGAImage bg(1,1,TGAImage::RGB);
bg.read_tga_file("envmap.tga");
bg.flip_vertically();
//DRAW BACKGROUND
/*for (int i =0; i < width; i++){
for(int j=0; j < height; j++){
int phi = ((std::atan2(j,i) / M_PI) + 1) / 2 * bg.width();
int theta = (std::acos(depth) / M_PI) * bg.height();
framebuffer.set(i,j,bg.get(phi,theta));
}
}*/
// Compute perspective
mat<4,4> mat;
std::vector<vec3 > new_vertices = compute_perspective(vertices,eye,center,up,width,height, &mat);
vec3 light_vector = {1,0,1}; light_vector = light_vector.normalized();
std::vector<vec3 > shadow_vertices = compute_perspective(vertices,light_vector,center,up,width,height, &mat);
// load shadder
TGAImage shadder(1,1,TGAImage::RGB);
shadder.read_tga_file(path_shadder);
shadder.flip_vertically();
//load texture.
TGAImage texture(1,1,TGAImage::RGB);
texture.read_tga_file(path_texture);
texture.flip_vertically();
// creating the image
draw_triangles(faces, new_vertices, shadder, t_faces, vertices_texture, framebuffer, texture, width, height, light_vector, center-eye, shadow_vertices);
framebuffer.write_tga_file("output.tga");
return 0;
}