-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOBJLoader.cpp
More file actions
155 lines (137 loc) · 3.85 KB
/
Copy pathOBJLoader.cpp
File metadata and controls
155 lines (137 loc) · 3.85 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "OBJLoader.h"
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
#include <ostream>
#include "Vertex.h"
#include "Vector3f.h"
#include "Vector2f.h"
namespace
{
void processVertex(std::vector<unsigned int>& index, std::vector<Vertex>& v, std::vector<Vector2f>& vt, std::vector<Vector3f>& vn, std::vector<unsigned int>& indices);
}
RawEntity OBJLoader::loadFile(std::string path)
{
std::vector<Vertex> v;
std::vector<Vector2f> vt;
std::vector<Vector3f> vn;
std::vector<unsigned int> indices;
std::ifstream file(path);
std::string line;
std::vector<float> tokenFloatList;
std::vector<unsigned int> tokenUintList;
// process v, vt, vn information
while (std::getline(file, line))
{
std::string token;
std::stringstream tokenizer(line);
std::string identifier = "";
if (line.rfind("f ", 0) == 0)
break;
else if (!(line.rfind("v ", 0) == 0 || line.rfind("vt ", 0) == 0 || line.rfind("vn ", 0) == 0))
continue;
else
tokenizer >> identifier;
while (tokenizer >> token)
tokenFloatList.push_back(std::stof(token));
if (identifier == "v")
{
Vector3f vec3(tokenFloatList[0], tokenFloatList[1], tokenFloatList[2]);
v.push_back(Vertex(vec3));
}
else if (identifier == "vt")
{
Vector2f vec2(tokenFloatList[0], tokenFloatList[1]);
vt.push_back(vec2);
}
else if (identifier == "vn")
{
Vector3f vec3(tokenFloatList[0], tokenFloatList[1], tokenFloatList[2]);
vn.push_back(vec3);
}
tokenFloatList.clear();
}
//process f information
do {
std::string token;
std::stringstream tokenizer(line);
if (line.rfind("f ", 0) == 0)
tokenizer.ignore();
else
continue;
while (tokenizer >> token)
{
std::stringstream innerTokenizer(token);
std::string innerToken = "";
while (std::getline(innerTokenizer, innerToken, '/'))
{
tokenUintList.push_back(std::stoul(innerToken));
}
processVertex(tokenUintList, v, vt, vn, indices);
tokenUintList.clear();
}
} while (std::getline(file, line));
// organize v vt vn into a final std::vector
std::vector<float> finalVertexList;
for (Vertex& vertex : v)
{
Vector3f& vec3Pos = vertex.position;
Vector2f& vec2Tex = vt.at(vertex.textureIndex);
Vector3f& vec3Norm = vn.at(vertex.normalIndex);
//float fArray[] = { vec3Pos.x, vec3Pos.y, vec3Pos.z, vec2Tex.x, vec2Tex.y };
float fArray[] = { vec3Pos.x, vec3Pos.y, vec3Pos.z, vec2Tex.x, vec2Tex.y, vec3Norm.x, vec3Norm.y, vec3Norm.z };
for (float f : fArray)
finalVertexList.push_back(f);
}
return RawEntity(std::move(finalVertexList), std::move(indices));
}
namespace
{
void processVertex(std::vector<unsigned int>& index, std::vector<Vertex>& v, std::vector<Vector2f>& vt, std::vector<Vector3f>& vn, std::vector<unsigned int>& indices)
{
unsigned int vertexIndex = index[0] - 1;
unsigned int textureIndex = index[1] - 1;
unsigned int normalIndex = index[2] - 1;
bool flag = false;
Vertex& vertex = v.at(vertexIndex);
if (!vertex.isSet())
{
vertex.textureIndex = textureIndex;
vertex.normalIndex = normalIndex;
indices.push_back(vertexIndex);
flag = true;
}
else if (vertex.textureIndex == textureIndex && vertex.normalIndex == normalIndex)
{
indices.push_back(vertexIndex);
flag = true;
}
else
{
if (!vertex.duplicateIndices.empty())
{
for (unsigned int duplicateIndex : vertex.duplicateIndices)
{
Vertex& duplicateVertex = v.at(duplicateIndex);
if (duplicateVertex.textureIndex == textureIndex && duplicateVertex.normalIndex == normalIndex)
{
indices.push_back(duplicateIndex);
flag = true;
break;
}
}
}
}
if (!flag)
{
indices.push_back(v.size());
vertex.duplicateIndices.push_back(v.size());
Vertex newVertex = Vertex(vertex.position);
newVertex.textureIndex = textureIndex;
newVertex.normalIndex = normalIndex;
v.push_back(newVertex);
}
}
}