-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
128 lines (101 loc) · 3.02 KB
/
model.cpp
File metadata and controls
128 lines (101 loc) · 3.02 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
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
126
127
#include <fstream>
#include <sstream>
#include <string>
#include "model.h"
Model::Model(const char* fileName){
std::ifstream f(fileName);
if (f.fail())
return;
std::string s;
while(!f.eof()){
std::getline(f, s);
std::stringstream ss(s);
std::string prefix;
ss>>prefix;
if(prefix == "v"){
vec3f v;
ss>>v.x>>v.y>>v.z;
verts.push_back(v);
}
if(prefix == "vt"){
vec3f vtx(0, 0, 0);
ss>>vtx.x>>vtx.y;
text_verts.push_back(vtx);
}
if (prefix == "f") {
int ver, tex, normal;
std::vector<int> face_data;
char slash;
// This loop handles the "f v/t/n v/t/n v/t/n" format
while(ss>>ver>>slash>>tex>>slash>>normal){ //follow the obj file format
face_data.push_back(ver-1);
face_data.push_back(tex-1);
face_data.push_back(normal-1);
}
faces.push_back(face_data);
}
if (prefix == "vn"){
vec3f vn;
ss>>vn.x>>vn.y>>vn.z;
norms.push_back(vn);
}
}
auto load_texture = [&fileName](const std::string suffix, TGAImage& img ){
std::string name(fileName);
size_t dot = name.find_last_of(".");
if (dot == std::string::npos)
return;
std::string textfile = name.substr(0, dot) + suffix;
std::cerr << "texture file " << textfile << " loading " << (img.read_tga_file(textfile.c_str())? "ok":"failed")<<std::endl;
};
//load diffuse texture
load_texture("_diffuse.tga", diffusemap);
diffusemap.flip_vertically();
//load normal mapping texture
load_texture("_nm_tangent.tga", normalmap);
normalmap.flip_vertically();
//load specular texture
load_texture("_spec.tga", specularmap);
specularmap.flip_vertically();
}
vec2f Model::uv(int iface, int nvert){
int index = faces[iface][nvert * 3 + 1];
vec3f v = text_verts[index];
return vec2f(v.x, v.y);
}
vec3f Model::norm(int iface, int nvert){
int idx = faces[iface][nvert * 3 + 2];
vec3f v = norms[idx];
return vec3f(v.x, v.y, v.z).normalized();
}
TGAColor Model::diffuse (vec2f uv){
double u = uv.x * diffusemap.get_width();
double v = uv.y * diffusemap.get_height();
return diffusemap.get(u, v);
}
TGAColor Model::normal_Map(vec2f uv){
double u = uv.x * normalmap.get_width();
double v = uv.y * normalmap.get_height();
return normalmap.get(u, v);
}
TGAColor Model::specular(vec2f uv){
double u = uv.x * specularmap.get_width();
double v = uv.y * specularmap.get_height();
return specularmap.get(u, v);
}
Model::~Model(){}
int Model::num_verts(){
return verts.size();
}
int Model::num_faces(){
return faces.size();
}
vec3f Model::vert(int i){
return verts[i];
}
vec3f Model::text_vert(int i){
return text_verts[i];
}
std::vector<int> Model::face(int i){
return faces[i];
}