Newer
Older
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
#pragma once
#include "ObjectLoader.h"
#include <sstream>
#include "Vertex.h"
namespace util {
std::vector<shapes::Triangle> loadObj(
std::istream& in, const std::shared_ptr<material::Material>& material) {
std::vector<Vertex> vertices;
std::vector<Vec3> v;
std::vector<Vec3> vt;
std::vector<Vec3> vn;
std::string lineStr;
while (std::getline(in, lineStr)) {
std::istringstream lineSS(lineStr);
std::string lineType;
lineSS >> lineType;
// vertex
if (lineType == "v") {
float x = 0, y = 0, z = 0;
lineSS >> x >> y >> z;
v.push_back(Vec3(x, y, z));
}
// texture
if (lineType == "vt") {
float u = 0, v = 0;
lineSS >> u >> v;
vt.push_back(Vec3(u, v, 0));
}
// normal
if (lineType == "vn") {
float i = 0, j = 0, k = 0;
lineSS >> i >> j >> k;
vn.push_back(Vec3(i, j, k).normalize());
}
// polygon
if (lineType == "f") {
std::string refStr;
while (lineSS >> refStr) {
std::istringstream ref(refStr);
std::string vStr, vtStr, vnStr;
std::getline(ref, vStr, '/');
std::getline(ref, vtStr, '/');
std::getline(ref, vnStr, '/');
int currentv = atoi(vStr.c_str());
int currentvt = atoi(vtStr.c_str());
int currentvn = atoi(vnStr.c_str());
currentv = (currentv >= 0 ? currentv : v.size() + currentv);
currentvt =
(currentvt >= 0 ? currentvt : vt.size() + currentvt);
currentvn =
(currentvn >= 0 ? currentvn : vn.size() + currentvn);
Vertex vert;
vert.position = v[currentv - 1];
vert.texcoord = v[currentvt - 1];
vert.normal = v[currentvn - 1];
vertices.push_back(vert);
}
}
}
std::vector<shapes::Triangle> triangles;
for (int i = 0; i < vertices.size(); i += 3) {
triangles.push_back(
{vertices[i + 0], vertices[i + 1], vertices[i + 2], material});
}
return triangles;
}
} // namespace util