This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavefront_loader.cpp
More file actions
executable file
·307 lines (271 loc) · 8.09 KB
/
Copy pathwavefront_loader.cpp
File metadata and controls
executable file
·307 lines (271 loc) · 8.09 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "wavefront_loader.hpp"
#include <fstream>
#include <cassert>
#include <sstream>
#include <span>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <chrono>
#include <atomic>
#include <memory>
#include <iostream>
static std::vector<std::string> GenerateInputBuffer(const std::string_view XStr, const char Char)
{
std::vector<std::string> InputBuffer;
size_t LastI = 0;
for (size_t i = 0; i < XStr.length(); i++)
{
if (XStr[i] == Char || i == XStr.length() - 1)
{
auto x = std::string(XStr.substr(LastI, (i + 1) - LastI));
std::erase(x, Char);
InputBuffer.push_back(x);
LastI = i;
}
}
return InputBuffer;
}
struct TriangleIndices
{
std::vector<uint32_t> PositionIndices = std::vector<uint32_t>(3);
std::vector<uint32_t> NormalIndices = std::vector<uint32_t>(3);
std::vector<uint32_t> TextureCoordIndices = std::vector<uint32_t>(3);
};
static void ProcessTrianglesIntoObject(
tnr::m3d::wavefront::tnrObject& Object,
const std::span<TriangleIndices>& TrianglesOutput,
const std::vector<tnr::m3d::wavefront::tnrObject::vec3<float>>& PositionsSource,
const std::vector<tnr::m3d::wavefront::tnrObject::vec3<float>>& NormalsSource,
const std::vector<tnr::m3d::wavefront::tnrObject::vec2<float>>& TextureCoordsSource,
const bool ShouldFlipY
)
{
Object.Positions.reserve(TrianglesOutput.size() * 3);
Object.Normals.reserve(TrianglesOutput.size() * 3);
Object.TextureCoords.reserve(TrianglesOutput.size() * 3);
for (const auto& Triangle : TrianglesOutput)
{
for (const auto Index : Triangle.PositionIndices)
{
auto Position = PositionsSource[Index];
if (ShouldFlipY)
{
Position.y *= -1.0f;
}
Object.Positions.push_back(Position);
}
for (const auto Index : Triangle.NormalIndices)
{
Object.Normals.push_back(NormalsSource[Index]);
}
for (const auto Index : Triangle.TextureCoordIndices)
{
Object.TextureCoords.push_back(TextureCoordsSource[Index]);
}
}
}
namespace tnr::m3d::wavefront
{
void tnrWavefrontLoader::LoadMaterials(const std::string& MTLFile)
{
std::ifstream File(MTLFile);
std::string Cache;
tnrMaterial MaterialCache;
while (std::getline(File, Cache, '\n'))
{
if (Cache.length() < 1) continue;
if (Cache[0] == '#') continue;
auto InputBuffer = GenerateInputBuffer(Cache, ' ');
if (InputBuffer[0] == "newmtl")
{
if (MaterialCache.MaterialName.length() > 0)
{
this->Materials.push_back(MaterialCache);
}
MaterialCache = tnrMaterial
{
.MaterialName = InputBuffer[1],
.AmbientColor = {},
.AlbedoColor = {},
.SpecularColor = {},
.EmissionColor = {},
.SpecularFactor = 0,
.RefractionFactor = 0,
.AlphaFactor = 0,
.AlbedoMap = std::nullopt,
.NormalMap = std::nullopt
};
}
else if (InputBuffer[0] == "Ka")
{
MaterialCache.AmbientColor =
{
std::stof(InputBuffer[1]),
std::stof(InputBuffer[2]),
std::stof(InputBuffer[3])
};
}
else if (InputBuffer[0] == "Kd")
{
MaterialCache.AlbedoColor =
{
std::stof(InputBuffer[1]),
std::stof(InputBuffer[2]),
std::stof(InputBuffer[3])
};
}
else if (InputBuffer[0] == "Ks")
{
MaterialCache.SpecularColor =
{
std::stof(InputBuffer[1]),
std::stof(InputBuffer[2]),
std::stof(InputBuffer[3])
};
}
else if (InputBuffer[0] == "Ns")
{
MaterialCache.SpecularFactor = std::stof(InputBuffer[1]);
}
else if (InputBuffer[0] == "Ni")
{
MaterialCache.RefractionFactor = std::stof(InputBuffer[1]);
}
else if (InputBuffer[0] == "map_Kd")
{
MaterialCache.AlbedoMap = std::optional<tnrMaterial::tnrTextureInfo>(tnrMaterial::tnrTextureInfo{ InputBuffer[1] });
}
else if (InputBuffer[0] == "map_Ks")
{
MaterialCache.SpecularMap = std::optional<tnrMaterial::tnrTextureInfo>(tnrMaterial::tnrTextureInfo{ InputBuffer[1] });
}
else if (InputBuffer[0] == "map_bump" || InputBuffer[0] == "bump")
{
MaterialCache.NormalMap = std::optional<tnrMaterial::tnrTextureInfo>(tnrMaterial::tnrTextureInfo{ InputBuffer[1] });
}
}
if (MaterialCache.MaterialName.length() > 0)
{
this->Materials.push_back(MaterialCache);
}
}
void tnrWavefrontLoader::LoadObject(const std::string& ObjFile)
{
auto WholeStartTime = std::chrono::system_clock::now();
const bool ShouldFlipY = this->Flags & tnrWavefrontOpenFlag::FLIP_POSITION_Y_AXIS;
std::ifstream File(ObjFile);
std::string LineCache;
tnrObject ObjectCache;
std::vector<tnrObject::vec3<float>> Positions;
std::vector<tnrObject::vec3<float>> Normals;
std::vector<tnrObject::vec2<float>> TextureCoords;
std::vector<TriangleIndices> TrianglesOutput;
while (std::getline(File, LineCache, '\n'))
{
if (LineCache.length() < 1) continue;
if (LineCache[0] == '#') continue;
//auto InputBuffer = SequenceLine(LineCache);
auto InputBuffer = GenerateInputBuffer(LineCache, ' ');
if (InputBuffer[0] == "o")
{
if (ObjectCache.ObjectName.length() > 0)
{
ProcessTrianglesIntoObject(ObjectCache, TrianglesOutput, Positions, Normals, TextureCoords, ShouldFlipY);
this->Objects.push_back(ObjectCache);
}
TrianglesOutput.clear();
ObjectCache = tnrObject
{
.ObjectName = InputBuffer[1],
.MaterialName = "",
.Positions = std::vector<tnrObject::vec3<float>>(),
.Normals = std::vector<tnrObject::vec3<float>>()
};
}
else if (InputBuffer[0] == "v")
{
tnr::m3d::wavefront::tnrObject::vec3<float> Cache
{
.x = std::stof(InputBuffer[1]),
.y = std::stof(InputBuffer[2]),
.z = std::stof(InputBuffer[3])
};
Positions.push_back(Cache);
}
else if (InputBuffer[0] == "vn")
{
tnr::m3d::wavefront::tnrObject::vec3<float> Cache
{
.x = std::stof(InputBuffer[1]),
.y = std::stof(InputBuffer[2]),
.z = std::stof(InputBuffer[3])
};
Normals.push_back(Cache);
}
else if (InputBuffer[0] == "vt")
{
tnr::m3d::wavefront::tnrObject::vec2<float> Cache
{
.x = std::stof(InputBuffer[1]),
.y = std::stof(InputBuffer[2])
};
TextureCoords.push_back(Cache);
}
else if (InputBuffer[0] == "usemtl")
{
ObjectCache.MaterialName = InputBuffer[1];
}
else if (InputBuffer[0] == "f")
{
const auto ProcessedInputVertex0 = GenerateInputBuffer(InputBuffer[1], '/');
const auto ProcessedInputVertex1 = GenerateInputBuffer(InputBuffer[2], '/');
const auto ProcessedInputVertex2 = GenerateInputBuffer(InputBuffer[3], '/');
const TriangleIndices TriangleCache
{
.PositionIndices
{
std::stoul(ProcessedInputVertex0[0]) - 1,
std::stoul(ProcessedInputVertex1[0]) - 1,
std::stoul(ProcessedInputVertex2[0]) - 1
},
.NormalIndices
{
std::stoul(ProcessedInputVertex0[2]) - 1,
std::stoul(ProcessedInputVertex1[2]) - 1,
std::stoul(ProcessedInputVertex2[2]) - 1
},
.TextureCoordIndices
{
std::stoul(ProcessedInputVertex0[1]) - 1,
std::stoul(ProcessedInputVertex1[1]) - 1,
std::stoul(ProcessedInputVertex2[1]) - 1
}
};
TrianglesOutput.push_back(TriangleCache);
}
}
if (ObjectCache.ObjectName.length() > 0)
{
ProcessTrianglesIntoObject(ObjectCache, TrianglesOutput, Positions, Normals, TextureCoords, ShouldFlipY);
this->Objects.push_back(ObjectCache);
}
}
tnrWavefrontLoader::tnrWavefrontLoader(const tnrWavefrontOpenFlag OpenFlags, const std::string ModelFile, const std::string MaterialFile)
{
this->Flags = OpenFlags;
if (!(this->Flags & tnrWavefrontOpenFlag::DONT_LOAD_MATERIALS))
{
this->LoadMaterials(MaterialFile);
}
this->LoadObject(ModelFile);
}
const std::vector<tnrObject>& tnrWavefrontLoader::GetLoadedObjects() const
{
return this->Objects;
}
const std::vector<tnrMaterial>& tnrWavefrontLoader::GetLoadedMaterials() const
{
return this->Materials;
}
}