-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.cpp
125 lines (115 loc) · 3.76 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
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 "model.h"
#include <sstream>
#include <fstream>
#include <iostream>
Model::Model(const std::string filename)
{
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 ") == 0) {
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 ") == 0) {
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 ") == 0) {
iss >> trash >> trash;
vec2 uv;
for (int i = 0; i < 2; i++)
{
iss >> uv[i];
}
tex_coord.emplace_back(uv.x, 1 - uv.y);
}
else if (line.compare(0, 2, "f ") == 0) {
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# " << tex_coord.size() << " vn# " << norms.size() << std::endl;
load_texture(filename, "_diffuse.tga", diffusemap);
load_texture(filename, "_nm.tga", normalmap);
load_texture(filename, "_nm_tangent.tga", normal_tangent_map);
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;
}
vec2 Model::uv(const int iface, const int nthvert) const {
return tex_coord[facet_tex[iface * 3 + nthvert]];
}
vec3 Model::normal(const int iface, const int nthvert) const
{
return norms[facet_nrm[iface * 3 + nthvert]];
}
vec3 Model::normal(const vec2& uvf) const {
TGAColor c = normalmap.get(uvf[0] * normalmap.width(), uvf[1] * normalmap.height());
return vec3{ (double)c[2],(double)c[1],(double)c[0] }*2. / 255. - vec3{ 1,1,1 };
}
vec3 Model::normal_tangent(const vec2& uvf) const
{
TGAColor c = normal_tangent_map.get(uvf[0] * normal_tangent_map.width(), uvf[1] * normal_tangent_map.height());
return vec3{ (double)c[2],(double)c[1],(double)c[0] }*2. / 255. - vec3{ 1,1,1 };
}
const TGAColor& Model::diffuse(double u, double v)
{
auto& diff = diffusemap;
auto diffU = diff.width() * u;
auto diffY = diff.height() * v;
auto diffColor = diff.get(diffU, diffY);
return diffColor;
}
const TGAColor& Model::spec(double u, double v)
{
auto& diff = specularmap;
auto diffU = diff.width() * u;
auto diffY = diff.height() * v;
auto diffColor = diff.get(diffU, diffY);
return diffColor;
}