-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjLoader.cpp
443 lines (296 loc) · 11.1 KB
/
objLoader.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "objLoader.hpp"
objLoaderClass::objLoaderClass()
{
this->temp_vertices = nullptr;
this->temp_texCoords = nullptr;
this->temp_normals = nullptr;
}
objLoaderClass::~objLoaderClass()
{
}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// //
// PUBLIC FUNCTIONS //
// //
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
void objLoaderClass::importObjFile(ObjectClass *Object)
{
float GreatestX = FLT_MIN, LowestX = FLT_MAX; // Used to create quadtree-grid.
float GreatestY = FLT_MIN, LowestY = FLT_MAX; //
float GreatestZ = FLT_MIN, LowestZ = FLT_MAX; //
input_file.open(*Object->getFileName());
if (!input_file.is_open())
{
MessageBox(NULL, L"Failed to open a .obj file.", L"ERROR: 'importObjFile()'", MB_OK);
}
else
{
std::string currentString; // Storing text read in from file; once at start loop
char currentCharacter; // Discards single characters
std::string throwAwayLine; // Discards lines
float float_holder[3]; // Holds x,y,z values / u,v coordinates
int faceVertexIndices[3]; /// These three variables are
int faceTexCoordIndices[3]; /// all used to temporarily store
int faceNormalIndices[3]; /// values to be written in z,y,x order.
int indexHolder = 0; // Index Value, which resets for each new 'category'
std::string categoryHolder = "empty"; // Keeps track of current type of data (v, vt, vn, f)
/// currentString = "LOOP_ITERATION_FINISHED";
// -- Each of these are all safety measures
// -- to combat abnormaly written text files.
while (!input_file.eof()) // Runs until end of file is reached
{
input_file >> currentString;
if (currentString == "#") // Comments are discarded
getline(input_file, throwAwayLine);
// -- OPENING, READING AND STORING VALUES FROM A 'MATERIAL FILE'
else if (currentString == "mtllib")
{
input_file >> currentString;
Object->getObjectData()->materialFileName = currentString;
currentString = "obj_files/" + currentString;
std::ifstream mlt_file;
mlt_file.open(currentString);
if (!mlt_file.is_open())
{
MessageBox(NULL, L"Failed to open a .mtl file.", L"ERROR: 'importObjFile()'", MB_OK);
}
else while (!mlt_file.eof())
{
mlt_file >> currentString;
if (currentString == "newmtl")
{
mlt_file >> currentString;
Object->getObjectData()->materialName = currentString;
currentString = "LOOP_ITERATION_FINISHED";
}
else if (currentString == "Texture")
{
mlt_file >> currentString;
// Converting to a 'wide string' so that the Texture loader can function
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
Object->getObjectData()->textureFileName = converter.from_bytes(currentString);
currentString = "LOOP_ITERATION_FINISHED";
}
else if (currentString == "illum")
{
mlt_file >> Object->getObjectData()->illumValue;
currentString = "LOOP_ITERATION_FINISHED";
}
else if (currentString == "Kd")
{
for (int i = 0; i < 3; i++)
{
mlt_file >> float_holder[i];
}
Object->getObjectData()->KdValues = { float_holder[0], float_holder[1], float_holder[2] };
currentString = "LOOP_ITERATION_FINISHED";
}
else if (currentString == "Ka")
{
for (int j = 0; j < 3; j++)
{
mlt_file >> float_holder[j];
}
Object->getObjectData()->KaValues = { float_holder[0], float_holder[1], float_holder[2] };
currentString = "LOOP_ITERATION_FINISHED";
}
else if (currentString == "Tf")
{
for (int k = 0; k < 3; k++)
{
mlt_file >> float_holder[k];
}
Object->getObjectData()->TfValues = { float_holder[0], float_holder[1], float_holder[2] };
currentString = "LOOP_ITERATION_FINISHED";
}
else if (currentString == "Ni")
{
mlt_file >> Object->getObjectData()->NiValue;
currentString = "LOOP_ITERATION_FINISHED";
}
}
if (!mlt_file.is_open())
std::string currentString = "Opps!"; // This is for DEBUGGING purposes; leave this breakpoint
if (mlt_file.is_open())
mlt_file.close();
currentString = "LOOP_ITERATION_FINISHED";
}
else if (currentString == "\n"); // Do nothing; restart 'while loop'
// -- ACQUIRING VERTEX COUNT AS WELL AS ALLOCATING MEMORY --
else if (currentString == "vc")
{
input_file >> Object->getObjectData()->vertexCount;
this->temp_vertices = new DirectX::XMFLOAT3[Object->getObjectData()->vertexCount];
currentString = "LOOP_ITERATION_FINISHED";
}
// -- ACQUIRING TEXCOORD COUNT AS WELL AS ALLOCATING MEMORY --
else if (currentString == "vtc")
{
input_file >> Object->getObjectData()->texCoordCount;
this->temp_texCoords = new DirectX::XMFLOAT2[Object->getObjectData()->texCoordCount];
currentString = "LOOP_ITERATION_FINISHED";
}
// -- ACQUIRING TEXCOORD COUNT AS WELL AS ALLOCATING MEMORY --
else if (currentString == "vnc")
{
input_file >> Object->getObjectData()->normalCount;
this->temp_normals = new DirectX::XMFLOAT3[Object->getObjectData()->normalCount];
currentString = "LOOP_ITERATION_FINISHED";
}
// -- ACQUIRING FACE COUNT AS WELL AS ALLOCATING ITS MEMORY --
else if (currentString == "fc")
{
input_file >> Object->getObjectData()->faceCount;
(*Object->getVerticeData()) = new VertexDataClass[(Object->getObjectData()->faceCount) * 3];
for (int i = 0; i < (Object->getObjectData()->faceCount) * 3; i++)
{
(*Object->getVerticeData())[i].normal = { 0, 0, 0 };
(*Object->getVerticeData())[i].texCoord = { 0, 0 };
(*Object->getVerticeData())[i].position = { 0, 0, 0 };
}
currentString = "LOOP_ITERATION_FINISHED";
}
// -- READING AND STORING ALL VERTEX POINTS --
else if (currentString == "v")
{
if (categoryHolder != "v")
{
indexHolder = 0;
categoryHolder = "v";
}
input_file >> currentString; // X
float_holder[0] = std::stod(currentString);
input_file >> currentString; // Y
float_holder[1] = std::stod(currentString);
input_file >> currentString; // Z
float_holder[2] = std::stod(currentString);
float_holder[2] *= -1.0f; // Converting from right-handed to left-handed system
this->temp_vertices[indexHolder] = { float_holder[0], float_holder[1], float_holder[2] };
indexHolder++;
currentString = "LOOP_ITERATION_FINISHED";
}
// -- READING AND STORING ALL TEX COORDS --
else if (currentString == "vt")
{
if (categoryHolder != "vt")
{
indexHolder = 0;
categoryHolder = "vt";
}
input_file >> currentString; // U
float_holder[0] = std::stod(currentString);
input_file >> currentString; // V
float_holder[1] = std::stod(currentString);
this->temp_texCoords[indexHolder] = { float_holder[0], float_holder[1] };
indexHolder++;
currentString = "LOOP_ITERATION_FINISHED";
}
// -- READING AND STORING ALL VERTEX NORMALS --
else if (currentString == "vn")
{
if (categoryHolder != "vn")
{
indexHolder = 0;
categoryHolder = "vn";
}
input_file >> currentString; // X
float_holder[0] = std::stod(currentString);
input_file >> currentString; // Y
float_holder[1] = std::stod(currentString);
input_file >> currentString; // Z
float_holder[2] = std::stod(currentString);
float_holder[2] *= -1.0f; // Converting from right-handed to left-handed system
this->temp_normals[indexHolder] = { float_holder[0], float_holder[1], float_holder[2] };
indexHolder++;
currentString = "LOOP_ITERATION_FINISHED";
}
// -- READING IN ALL FACES, AND THEREBY STORING DATA --
else if (currentString == "f")
{
if (categoryHolder != "f")
{
indexHolder = 0;
categoryHolder = "f";
}
for (int i = 0; i < 3; i++) // Reads 3 points of each face, temporarily storing data
{
input_file >> faceVertexIndices[i];
faceVertexIndices[i]--;
input_file >> currentCharacter;
input_file >> faceTexCoordIndices[i];
faceTexCoordIndices[i]--;
input_file >> currentCharacter;
input_file >> faceNormalIndices[i];
faceNormalIndices[i]--;
}
for (int j = 2; j >= 0; j--) // Storing the to-be-used data, read in backwards
{
(*Object->getVerticeData())[indexHolder].position = this->temp_vertices[faceVertexIndices[j]];
(*Object->getVerticeData())[indexHolder].texCoord = this->temp_texCoords[faceTexCoordIndices[j]];
(*Object->getVerticeData())[indexHolder].normal = this->temp_normals[faceNormalIndices[j]];
// Find the greatest/lowest x-y values for the object, which will be used in QuadTree creation.
if ((*Object->getVerticeData())[indexHolder].position.x > GreatestX) {
GreatestX = (*Object->getVerticeData())[indexHolder].position.x;
}
if ((*Object->getVerticeData())[indexHolder].position.x < LowestX) {
LowestX = (*Object->getVerticeData())[indexHolder].position.x;
}
if ((*Object->getVerticeData())[indexHolder].position.y > GreatestY) {
GreatestY = (*Object->getVerticeData())[indexHolder].position.y;
}
if ((*Object->getVerticeData())[indexHolder].position.y < LowestY) {
LowestY = (*Object->getVerticeData())[indexHolder].position.y;
}
if ((*Object->getVerticeData())[indexHolder].position.z > GreatestZ) {
GreatestZ = (*Object->getVerticeData())[indexHolder].position.z;
}
if ((*Object->getVerticeData())[indexHolder].position.z < LowestZ) {
LowestZ = (*Object->getVerticeData())[indexHolder].position.z;
}
indexHolder++;
}
currentString = "LOOP_ITERATION_FINISHED";
}
// -- READING IN ALL SIDES, CURRENTLY BEING DISCARDED --
else if (currentString == "s")
{
getline(input_file, throwAwayLine);
currentString = "LOOP_ITERATION_FINISHED";
}
// -- READING IN THE MATERIAL NAME --
else if (currentString == "g")
{
input_file >> currentString;
Object->getObjectData()->materialName = currentString;
currentString = "LOOP_ITERATION_FINISHED";
}
/*
"delete temp_X" is not needed since they conatin no memory when
this line is reached.
*/
}
}
Object->SetGreatestX(GreatestX);
Object->SetLowestX(LowestX);
Object->SetGreatestY(GreatestY);
Object->SetLowestY(LowestY);
Object->SetGreatestZ(GreatestZ);
Object->SetLowestZ(LowestZ);
if (!input_file.is_open())
std::string currentString = "OOPS!"; // This is for DEBUGGING purposes; leave this breakpoint
if (input_file.is_open())
input_file.close();
}
VertexDataClass* *objLoaderClass::getVerticeData()
{
return &this->bufferData;
}
ObjectDataClass *objLoaderClass::getObjectData()
{
return &this->ObjectData;
}
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// //
// PRIVATE FUNCTIONS //
// //
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+