-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
72 lines (65 loc) · 2.18 KB
/
utils.cpp
File metadata and controls
72 lines (65 loc) · 2.18 KB
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
#include "utils.hpp"
namespace trz
{
std::vector<std::string> split(std::string &s, std::string delimiter)
{
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos)
{
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
res.push_back(s.substr(pos_start));
return res;
}
bool load(trz::mesh &object, std::ifstream infile)
{
if (!infile.is_open())
{
return false;
}
std::string line;
std::vector<vec3> vertices;
std::vector<vec3> normals;
std::vector<vec3> texture;
while (std::getline(infile, line))
{
std::vector<std::string> s = split(line, " ");
if (s[0][0] == 'v')
{
vec3 vertex{strtof(s[1].c_str(), NULL), strtof(s[2].c_str(), NULL), strtof(s[3].c_str(), NULL)};
if (s[0][1] == 'n')
{
normals.push_back(vertex);
}
else if (s[0][1] == 't')
{
vertices.push_back(vertex);
}
else
{
vertices.push_back(vertex);
}
}
else if (s[0] == "f")
{
triangle triangle;
triangle.normal = {0};
for (int i = 0; i < 3; i++)
{
std::vector<std::string> indices = split(s[1 + i], "/");
triangle.p[i] = vertices[strtof(indices[0].c_str(), NULL) - 1];
// TODO: texture
// TODO: for now we average the normals (flat shading)
triangle.normal = triangle.normal + normals[strtof(indices[2].c_str(), NULL) - 1];
}
triangle.normal = normalizeVec(triangle.normal);
object.tris.push_back(triangle);
}
}
return true;
}
} // namespace trz