-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmodel.cpp
45 lines (40 loc) · 1.25 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
#include <fstream>
#include <sstream>
#include "model.h"
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 ")) {
iss >> trash;
vec3 v;
for (int i : {0,1,2}) iss >> v[i];
verts.push_back(v);
} else if (!line.compare(0, 2, "f ")) {
int f,t,n, cnt = 0;
iss >> trash;
while (iss >> f >> trash >> t >> trash >> n) {
facet_vrt.push_back(--f);
cnt++;
}
if (3!=cnt) {
std::cerr << "Error: the obj file is supposed to be triangulated" << std::endl;
return;
}
}
}
std::cerr << "# v# " << nverts() << " f# " << nfaces() << std::endl;
}
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]];
}