-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.cpp
50 lines (44 loc) · 1.22 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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "model.h"
Model::Model(const std::string& filename) {
std::ifstream in(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;
Vec3f v;
iss >> v.x >> v.y >> v.z;
verts.push_back(v);
} else if (!line.compare(0, 2, "f ")) {
std::vector<int> f;
int itrash, idx;
iss >> trash;
while (iss >> idx >> trash >> itrash >> trash >> itrash) {
idx--; // in wavefront obj all indices start at 1, not zero
f.push_back(idx);
}
faces.push_back(f);
}
}
std::cerr << "# v# " << verts.size() << " f# " << faces.size() << std::endl;
}
size_t Model::nverts() const {
return verts.size();
}
size_t Model::nfaces() const {
return faces.size();
}
std::vector<int> Model::face(size_t idx) const {
return faces[idx];
}
Vec3f Model::vert(size_t i) const {
return verts[i];
}