-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.cpp
82 lines (73 loc) · 2.17 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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "model.h"
Model::Model(const char *filename) : verts_(), faces_() {
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());
std::string trash;
if (!line.compare(0, 2, "v ")) {
iss >> trash;
Vec3f v;
for (int i=0;i<3;i++) {
iss >> v.raw[i];
}
verts_.push_back(v);
} else if (!line.compare(0, 4, "vt ")) {
iss >> trash;
Vec3f v;
for (int i=0;i<3;i++) {
iss >> v.raw[i];
}
vertTextures_.push_back(v);
} else if (!line.compare(0, 4, "vn ")) {
iss >> trash;
Vec3f v;
for (int i=0;i<3;i++) {
iss >> v.raw[i];
}
vertNormals_.push_back(v);
} else if (!line.compare(0, 2, "f ")) {
std::vector<std::vector<int>> f;
int idx, idy, idz;
char ctrash;
iss >> ctrash;
while (iss >> idx >> ctrash >> idy >> ctrash >> idz) {
// idx--; // in wavefront obj all indices start at 1, not zero
// idy--;
// idz--;
std::vector<int> faceVertex{ --idx, --idy, --idz };
f.push_back(faceVertex);
}
faces_.push_back(f);
}
}
std::cerr << "# v# " << verts_.size() << " vt# " << vertTextures_.size() << " vn# " << vertNormals_.size() << " f# " << faces_.size() << std::endl;
}
Model::~Model() {
}
int Model::getTotalVertices() {
return (int)verts_.size();
}
int Model::getTotalTextureVertices() {
return (int)vertTextures_.size();
}
int Model::getTotalFaces() {
return (int)faces_.size();
}
std::vector<std::vector<int>> Model::getFaceByIndex(int idx) {
return faces_[idx];
}
Vec3f Model::getVertexByIndex(int i) {
return verts_[i];
}
Vec3f Model::getTextureVertexByIndex(int i) {
return vertTextures_[i];
}