forked from ssloy/tinyrenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.cpp
101 lines (89 loc) · 3.26 KB
/
model.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
#include <iostream>
#include <fstream>
#include <sstream>
#include "model.h"
Model::Model(const std::string filename) : verts_(), uv_(), norms_(), facet_vrt_(), facet_tex_(), facet_nrm_(), diffusemap_(), normalmap_(), specularmap_() {
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail()) return;
std::string line;
while (!in.eof()) {
std::getline(in, line);
std::istringstream iss(line.c_str());
char trash;
if (!line.compare(0, 2, "v ")) {
iss >> trash;
vec3 v;
for (int i=0;i<3;i++) iss >> v[i];
verts_.push_back(v);
} else if (!line.compare(0, 3, "vn ")) {
iss >> trash >> trash;
vec3 n;
for (int i=0;i<3;i++) iss >> n[i];
norms_.push_back(n.normalize());
} else if (!line.compare(0, 3, "vt ")) {
iss >> trash >> trash;
vec2 uv;
for (int i=0;i<2;i++) iss >> uv[i];
uv_.push_back(uv);
} else if (!line.compare(0, 2, "f ")) {
int f,t,n;
iss >> trash;
int cnt = 0;
while (iss >> f >> trash >> t >> trash >> n) {
facet_vrt_.push_back(--f);
facet_tex_.push_back(--t);
facet_nrm_.push_back(--n);
cnt++;
}
if (3!=cnt) {
std::cerr << "Error: the obj file is supposed to be triangulated" << std::endl;
in.close();
return;
}
}
}
in.close();
std::cerr << "# v# " << nverts() << " f# " << nfaces() << " vt# " << uv_.size() << " vn# " << norms_.size() << std::endl;
load_texture(filename, "_diffuse.tga", diffusemap_);
load_texture(filename, "_nm_tangent.tga", normalmap_);
load_texture(filename, "_spec.tga", specularmap_);
}
int Model::nverts() const {
return verts_.size();
}
int Model::nfaces() const {
return facet_vrt_.size()/3;
}
vec3 Model::vert(const int i) const {
return verts_[i];
}
vec3 Model::vert(const int iface, const int nthvert) const {
return verts_[facet_vrt_[iface*3+nthvert]];
}
void Model::load_texture(std::string filename, const std::string suffix, TGAImage &img) {
size_t dot = filename.find_last_of(".");
if (dot==std::string::npos) return;
std::string texfile = filename.substr(0,dot) + suffix;
std::cerr << "texture file " << texfile << " loading " << (img.read_tga_file(texfile.c_str()) ? "ok" : "failed") << std::endl;
img.flip_vertically();
}
TGAColor Model::diffuse(const vec2 &uvf) const {
return diffusemap_.get(uvf[0]*diffusemap_.get_width(), uvf[1]*diffusemap_.get_height());
}
vec3 Model::normal(const vec2 &uvf) const {
TGAColor c = normalmap_.get(uvf[0]*normalmap_.get_width(), uvf[1]*normalmap_.get_height());
vec3 res;
for (int i=0; i<3; i++)
res[2-i] = c[i]/255.*2 - 1;
return res;
}
double Model::specular(const vec2 &uvf) const {
return specularmap_.get(uvf[0]*specularmap_.get_width(), uvf[1]*specularmap_.get_height())[0];
}
vec2 Model::uv(const int iface, const int nthvert) const {
return uv_[facet_tex_[iface*3+nthvert]];
}
vec3 Model::normal(const int iface, const int nthvert) const {
return norms_[facet_nrm_[iface*3+nthvert]];
}