-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjLoader.cpp
More file actions
77 lines (67 loc) · 2.45 KB
/
ObjLoader.cpp
File metadata and controls
77 lines (67 loc) · 2.45 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
73
74
75
76
77
#include "stdafx.h"
#include "ObjLoader.h"
#include <iterator>
#include <iostream>
void ObjLoader::Load(string filename, Vertex*& vertices_out, int& vertices_count_out, DWORD*& indices_out, int& indices_count_out,
BoundingVolume* & boundingVolume) {
std::ifstream input(filename);
vector<XMFLOAT3> positions;
vector<XMFLOAT2> texcoord;
vector<Vertex> verticesVector;
vector<DWORD> indices;
int counter = 0;
for (std::string line; getline(input, line);) {
std::stringstream ss(line);
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
std::copy(vstrings.begin(), vstrings.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
string first = vstrings.at(0);
if (first == "o") {
continue;
// Log::Info("Load Object: " + static_cast<string>(vstrings.at(1)));
}
if (first == "v") {
positions.push_back({std::stof(vstrings.at(1)), std::stof(vstrings.at(2)), std::stof(vstrings.at(3))});
} else if (first == "vt") {
texcoord.push_back({std::stof(vstrings.at(1)), 1.0f - std::stof(vstrings.at(2))});
} else if (first == "f") {
vector<int> mapping;
for (size_t i = 1; i <= 3; i++) {
mapping = split(vstrings.at(i));
Vertex v = {positions.at(mapping[0] - 1), {0, 0, 0, 0}, texcoord.at(mapping[1] - 1)};
vector<Vertex>::iterator it = std::find(verticesVector.begin(), verticesVector.end(), v);
if (it == verticesVector.end()) {
verticesVector.push_back(v);
indices.push_back(counter);
counter++;
} else {
int index = std::distance(verticesVector.begin(), it);
indices.push_back(index);
}
}
}
}
Vertex* v = new Vertex[verticesVector.size()];
for (int i = 0; i < verticesVector.size(); ++i) {
v[i] = verticesVector[i];
}
vertices_out = v;
vertices_count_out = verticesVector.size();
DWORD* i = new DWORD[indices.size()];
for (int j = 0; j < indices.size(); ++j) {
i[j] = indices[j];
}
indices_out = i;
indices_count_out = indices.size();
boundingVolume = new BoundingVolume(verticesVector, indices);
}
vector<int> ObjLoader::split(string strToSplit) {
std::stringstream ss(strToSplit);
std::string item;
std::vector<int> splittedStrings;
while (std::getline(ss, item, '/')) {
splittedStrings.push_back(std::stof(item));
}
return splittedStrings;
}