-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshLoader.cpp
150 lines (108 loc) · 2.59 KB
/
MeshLoader.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
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
#include "MeshLoader.h"
MeshLoader* MeshLoader::m_Instance = NULL;
MeshLoader* MeshLoader::Instance()
{
if (!m_Instance)
m_Instance = new MeshLoader();
return m_Instance;
}
vector<string> MeshLoader::whiteSpaceRegex(string line)
{
regex ws_re("(\ )+");
vector<string> result{
sregex_token_iterator(line.begin(), line.end(), ws_re, -1), {}
};
return result;
}
void MeshLoader::LoadVertices(ifstream& inFile, Mesh& mesh)
{
vector<string> result;
string line;
getline(inFile, line);
mesh.numVertices = stoi(line);
mesh.indexedVertices = new Vertex[mesh.numVertices];
for (int i = 0; i < mesh.numVertices; i++)
{
getline(inFile, line);
result = whiteSpaceRegex(line);
if (result.size() == 3)
{
mesh.indexedVertices[i].x = stof(result[0]);
mesh.indexedVertices[i].y = stof(result[1]);
mesh.indexedVertices[i].z = stof(result[2]);
}
}
}
void MeshLoader::LoadNormals(ifstream& inFile, Mesh& mesh)
{
vector<string> result;
string line;
getline(inFile, line);
mesh.numNormals = stoi(line);
mesh.indexedNormals = new Vector3[mesh.numNormals];
for (int i = 0; i < mesh.numNormals; i++)
{
getline(inFile, line);
result = whiteSpaceRegex(line);
if (result.size() == 3)
{
mesh.indexedNormals[i].x = stof(result[0]);
mesh.indexedNormals[i].y = stof(result[1]);
mesh.indexedNormals[i].z = stof(result[2]);
}
}
}
void MeshLoader::LoadUVs(ifstream& inFile, Mesh& mesh)
{
vector<string> result;
string line;
getline(inFile, line);
mesh.numTexCoords = stoi(line);
mesh.TexCoords = new TexCoord[mesh.numTexCoords];
for (int i = 0; i < mesh.numTexCoords; i++)
{
getline(inFile, line);
result = whiteSpaceRegex(line);
if (result.size() == 2)
{
mesh.TexCoords[i].u = stof(result[0]);
mesh.TexCoords[i].v = stof(result[1]);
}
}
}
void MeshLoader::LoadIndices(ifstream& inFile, Mesh& mesh)
{
vector<string> result;
string line;
getline(inFile, line);
mesh.numIndices = stoi(line);
mesh.indices = new GLushort[mesh.numIndices];
for (int i = 0; i < mesh.numIndices; i += 3)
{
getline(inFile, line);
result = whiteSpaceRegex(line);
if (result.size() == 3)
{
mesh.indices[i] = stoi(result[0]);
mesh.indices[i + 1] = stoi(result[1]);
mesh.indices[i + 2] = stoi(result[2]);
}
}
}
Mesh* MeshLoader::Load(char* path)
{
Mesh* mesh = new Mesh();
ifstream inFile;
inFile.open(path);
if (!inFile.good())
{
cerr << "Can't open texture file " << path << endl;
delete mesh;
return nullptr;
}
LoadVertices(inFile, *mesh);
LoadUVs(inFile, *mesh);
LoadNormals(inFile, *mesh);
LoadIndices(inFile, *mesh);
return mesh;
}