Object.cpp 14.8 KB
Newer Older
1
#include "Object.h"
Anakin's avatar
Anakin committed
2
#include <iostream>
Anakin's avatar
Anakin committed
3
#define PI (4.0*atan(1.0))
4
5
6
7
8


/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

Anakin's avatar
Anakin committed
9
Object::Object(const char* path)
10
{
Anakin's avatar
Anakin committed
11
	// open file
12
	fsMesh.open(path, std::ios::in | std::ios::binary);
13

Anakin's avatar
Anakin committed
14
15
	if (!fsMesh.is_open())
		throw std::invalid_argument(std::string("file not found: ") += path);
16

17
18
	// jump to file size information
	fsMesh.seekg(4);
19

20
21
	std::uint32_t tempFileSize;
	std::list<ChunkHeader*> tempMainChunks;
22

23
24
25
	// get all chunks under HEDR
	fsMesh.read(reinterpret_cast<char*>(&tempFileSize), sizeof(tempFileSize));
	loadChunks(tempMainChunks, fsMesh.tellg(), tempFileSize);
26

Anakin's avatar
Anakin committed
27
	// evaluate HEDR subchunks (= find MSH2)
28
	for (std::list<ChunkHeader*>::iterator it = tempMainChunks.begin(); it != tempMainChunks.end(); it++)
29
	{
30
		if (!strcmp("MSH2", (*it)->name))
31
		{
Anakin's avatar
Anakin committed
32
			// get all subchunks
33
34
			std::list<ChunkHeader*> tempMsh2Chunks;
			loadChunks(tempMsh2Chunks, (*it)->position, (*it)->size);
Anakin's avatar
Anakin committed
35

36
37
38
39
40
			// evaluate MSH2 subchunks
			analyseMsh2Chunks(tempMsh2Chunks);

			// clean up
			while (!tempMsh2Chunks.empty())
Anakin's avatar
Anakin committed
41
			{
42
43
				ChunkHeader* tempCursor = tempMsh2Chunks.front();
				tempMsh2Chunks.pop_front();
Anakin's avatar
Anakin committed
44
45
				delete tempCursor;
			}
46
			continue;
47
48
		}
	}
49

50
51
52
53
54
55
56
57
	// clean up
	while (!tempMainChunks.empty())
	{
		ChunkHeader* tempCursor = tempMainChunks.front();
		tempMainChunks.pop_front();
		delete tempCursor;
	}

58
59
60
61
62
63
	// close file
	fsMesh.close();
}

Object::~Object()
{
Anakin's avatar
Anakin committed
64
65
66
67
68
	// clear texture list
	vTextures.clear();

	// clear Model list (don't delete the elements)
	vModls.clear();
69
}
70
71


72
73
/////////////////////////////////////////////////////////////////////////
// private functions
74

75
76
77
78
79
80
void Object::setModlDefault(Modl * model)
{
	model->name = "";
	model->parent = "";
	model->type = null;
	model->renderFlags = -1;
Anakin's avatar
Anakin committed
81
	model->m4x4Translation = glm::mat4(1.0f);
82
83
84
85
	model->texture = "";
	model->vertex = NULL;
	model->uv = NULL;
	model->mesh = NULL;
Anakin's avatar
Anakin committed
86
	model->meshSize = 0;
87
88
}

Anakin's avatar
Anakin committed
89
void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t end)
90
{
91
	// jump to first chunk
92
	fsMesh.seekg(start);
93

Anakin's avatar
Anakin committed
94
95
	do
	{
Anakin's avatar
Anakin committed
96
		ChunkHeader* tempHeader = new ChunkHeader();
97

98
99
100
		fsMesh.read(reinterpret_cast<char*>(&tempHeader->name[0]), sizeof(tempHeader->name) - 1);
		fsMesh.read(reinterpret_cast<char*>(&tempHeader->size), sizeof(tempHeader->size));
		tempHeader->position = fsMesh.tellg();
101

102
		destination.push_back(tempHeader);
103

104
		fsMesh.seekg(tempHeader->size, std::ios_base::cur);
105

106
107
		// reached end
		if (fsMesh.tellg() - start == end)
Anakin's avatar
Anakin committed
108
			break;
109
110
111
112
113
114
115
116
117
118

		// error. Maybe the size information is corrupted
		if (!fsMesh.good())
		{
			std::cout << "WARNING: corrupted file. Trying to continue" << std::endl;
			fsMesh.clear();
			break;
		}

	} while (true);
119
120
}

121
122
123
124
125
126
void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
{
	for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
	{
		if (!strcmp("MATL", (*it)->name))
		{
127
128
129
130
131
			// "useless" information how many MATD follow
			fsMesh.seekg((*it)->position);
			std::uint32_t tempMatdCount;
			fsMesh.read(reinterpret_cast<char*>(&tempMatdCount), sizeof(std::uint32_t));

Anakin's avatar
Anakin committed
132
133
			// get all MATD from MATL list
			std::list<ChunkHeader*> tempMatlChunks;
134
			loadChunks(tempMatlChunks, fsMesh.tellg(), (*it)->size - 4);
Anakin's avatar
Anakin committed
135
136
137
138
139
140
141
142
143
144
145
146

			// evaluate MATL subchunks
			for (std::list<ChunkHeader*>::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++)
			{
				// This shouldn't be anything else than MATD
				if (!strcmp("MATD", (*it)->name))
				{
					// get all subchunks from MATD
					std::list<ChunkHeader*> tempMatdChunks;
					loadChunks(tempMatdChunks, (*it)->position, (*it)->size);

					// analyse MATD subchunks
Anakin's avatar
Anakin committed
147
					analyseMatdChunks(tempMatdChunks);
Anakin's avatar
Anakin committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

					// clean up
					while (!tempMatdChunks.empty())
					{
						ChunkHeader* tempCursor = tempMatdChunks.front();
						tempMatdChunks.pop_front();
						delete tempCursor;
					}
				}
			}

			// clean up
			while (!tempMatlChunks.empty())
			{
				ChunkHeader* tempCursor = tempMatlChunks.front();
				tempMatlChunks.pop_front();
				delete tempCursor;
			}
166
167
168
169
170
171

			continue;
		}

		if (!strcmp("MODL", (*it)->name))
		{
Anakin's avatar
Anakin committed
172
173
174
			Modl* tempModl = new Modl;
			setModlDefault(tempModl);

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
			// get all subchunks
			std::list<ChunkHeader*> tempChunks;
			loadChunks(tempChunks, (*it)->position, (*it)->size);

			// evaluate MODL subchunks
			analyseModlChunks(tempModl, tempChunks);

			//clean up
			while (!tempChunks.empty())
			{
				ChunkHeader* tempCursor = tempChunks.front();
				tempChunks.pop_front();
				delete tempCursor;
			}

Anakin's avatar
Anakin committed
190
			// save Model data
Anakin's avatar
Anakin committed
191
			vModls.push_back(tempModl);
Anakin's avatar
Anakin committed
192

Anakin's avatar
Anakin committed
193
194
195
196
			continue;
		}
	}
}
197

Anakin's avatar
Anakin committed
198
void Object::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
Anakin's avatar
Anakin committed
199
200
201
202
203
204
205
206
207
208
209
210
{
	for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
	{
		//TX1D, TX2D, TX3D
		if (!strcmp("TX0D", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			char* buffer = new char[(*it)->size + 1];
			*buffer = { 0 };
			fsMesh.read(buffer, (*it)->size);
			vTextures.push_back(buffer);
			delete buffer;
211
212
213
214
215
			continue;
		}
	}
}

Anakin's avatar
Anakin committed
216
217
218
219
220
221
222
223
224
225
void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& chunkList)
{
	for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
	{
		if (!strcmp("MTYP", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			std::uint32_t tempType;
			fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType));
			dataDestination->type = (Mtyp)tempType;
Anakin's avatar
Anakin committed
226
			continue;
Anakin's avatar
Anakin committed
227
228
229
230
231
		}

		if (!strcmp("PRNT", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
232
			char* buffer = new char[(*it)->size];
Anakin's avatar
Anakin committed
233
			*buffer = { 0 };
234
235
236
			fsMesh.read(buffer, (*it)->size); 
			dataDestination->parent = buffer;
			delete buffer;
Anakin's avatar
Anakin committed
237
			continue;
Anakin's avatar
Anakin committed
238
239
240
241
242
		}

		if (!strcmp("NAME", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
243
			char* buffer = new char[(*it)->size];
Anakin's avatar
Anakin committed
244
			*buffer = { 0 };
245
246
			fsMesh.read(buffer, (*it)->size);
			dataDestination->name = buffer;
247
			delete buffer;
Anakin's avatar
Anakin committed
248
			continue;
Anakin's avatar
Anakin committed
249
250
251
252
253
254
		}

		if (!strcmp("FLGS", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags));
Anakin's avatar
Anakin committed
255
			continue;
Anakin's avatar
Anakin committed
256
257
258
259
		}

		if (!strcmp("TRAN", (*it)->name))
		{
Anakin's avatar
Anakin committed
260
261
262
263
			float tempScale[3];
			float tempRotation[4];
			float tempTrans[3];

Anakin's avatar
Anakin committed
264
			fsMesh.seekg((*it)->position);
Anakin's avatar
Anakin committed
265
266
267
268
269
270
271
272
273
274

			fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&tempScale[2]), sizeof(float));

			fsMesh.read(reinterpret_cast<char*>(&tempRotation[0]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&tempRotation[1]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float));

Anakin's avatar
Anakin committed
275
276
277
278
279
280
281
			//calculate x,y,z rotation
			tempRotation[0] = atan2(2 * (tempRotation[0] * tempRotation[1] + tempRotation[2] * tempRotation[3]),
									1 - 2 * (pow(tempRotation[1], 2) + pow(tempRotation[2], 2)));
			tempRotation[1] = asin(2 * (tempRotation[0] * tempRotation[2] - tempRotation[3] * tempRotation[1]));
			tempRotation[2] = atan2(2 * (tempRotation[0] * tempRotation[3] + tempRotation[1] * tempRotation[2]),
									1 - 2 * (pow(tempRotation[2], 2) + pow(tempRotation[3], 2))) - PI;

Anakin's avatar
Anakin committed
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
			fsMesh.read(reinterpret_cast<char*>(&tempTrans[0]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&tempTrans[1]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&tempTrans[2]), sizeof(float));

			dataDestination->m4x4Translation = glm::scale(
				dataDestination->m4x4Translation,
				glm::vec3(tempScale[0], tempScale[1], tempScale[2])
			);

			dataDestination->m4x4Translation = glm::translate(
				dataDestination->m4x4Translation,
				glm::vec3(tempTrans[0], tempTrans[1], tempTrans[2])
			);

			dataDestination->m4x4Translation = glm::rotate(
				dataDestination->m4x4Translation,
				tempRotation[0],
				glm::vec3(1, 0, 0)
			);

			dataDestination->m4x4Translation = glm::rotate(
				dataDestination->m4x4Translation,
				tempRotation[1],
				glm::vec3(0, 1, 0)
			);

			dataDestination->m4x4Translation = glm::rotate(
				dataDestination->m4x4Translation,
				tempRotation[2],
				glm::vec3(0, 0, 1)
			);

Anakin's avatar
Anakin committed
314
			continue;
Anakin's avatar
Anakin committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
		}

		if (!strcmp("GEOM", (*it)->name))
		{
			// get all subchunks
			std::list<ChunkHeader*> tempGeomChunks;
			loadChunks(tempGeomChunks, (*it)->position, (*it)->size);

			// evaluate GEOM subchunks
			analyseGeomChunks(dataDestination, tempGeomChunks);

			// clean up
			while (!tempGeomChunks.empty())
			{
				ChunkHeader* tempCursor = tempGeomChunks.front();
				tempGeomChunks.pop_front();
				delete tempCursor;
			}
Anakin's avatar
Anakin committed
333
334

			continue;
Anakin's avatar
Anakin committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
		}
	}
}

void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
	{
		if (!strcmp("SEGM", (*it)->name))
		{
			// get all subchunks
			std::list<ChunkHeader*> tempSegmChunks;
			loadChunks(tempSegmChunks, (*it)->position, (*it)->size);

			// evaluate SEGM subchunks
			analyseSegmChunks(dataDestination, tempSegmChunks);

			// clean up
			while (!tempSegmChunks.empty())
			{
				ChunkHeader* tempCursor = tempSegmChunks.front();
				tempSegmChunks.pop_front();
				delete tempCursor;
			}
Anakin's avatar
Anakin committed
359
			continue;
Anakin's avatar
Anakin committed
360
		}
Anakin's avatar
Anakin committed
361
362
		
		if (!strcmp("CLTH", (*it)->name))
Anakin's avatar
Anakin committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
		{
			// get all subchunks
			std::list<ChunkHeader*> tempClthChunks;
			loadChunks(tempClthChunks, (*it)->position, (*it)->size);

			// evaluate CLTH subchunks
			analyseClthChunks(dataDestination, tempClthChunks);

			// clean up
			while (!tempClthChunks.empty())
			{
				ChunkHeader* tempCursor = tempClthChunks.front();
				tempClthChunks.pop_front();
				delete tempCursor;
			}
Anakin's avatar
Anakin committed
378
			continue;
Anakin's avatar
Anakin committed
379
380
381
382
383
384
385
386
		}
	}
}

void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
	{
387
		/*if (!strcmp("SHDW", (*it)->name))
Anakin's avatar
Anakin committed
388
389
390
391
392
393
394
395
396
397
398
399
		{
			fsMesh.seekg((*it)->position);
			/* shadow mesh geometry

			long int - 4 - number of vertex positions
			float[3][] - 12 each - vertex positions (XYZ)
			long int - 4 - number of edges
			short int[4][] - 8 each - edge the following 4 entries from one edge
			> short int - 2 - vertex index of this edge, referes to the vertex list
			> short int - 2 - Reference into an edge. Defines the target vertex (the local edge vertex of the referenced edge) to which the edge should be dran from the local vertex
			> short int - 2 - Second reference into an edge. In all example .msh files I've seen this always refers to the same vertex as the first edge reference
			> short int - 2 - MAX_VALUE of short integers (65535). Indicates the end of this edge
400
			* /
Anakin's avatar
Anakin committed
401
			continue;
402
		}*/
Anakin's avatar
Anakin committed
403
404
405
406

		if (!strcmp("MATI", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
Anakin's avatar
Anakin committed
407
408
			std::uint32_t tempIndex;
			fsMesh.read(reinterpret_cast<char*>(&tempIndex), sizeof(tempIndex));
409
			if (vTextures.size() <= tempIndex)
Anakin's avatar
Anakin committed
410
411
412
413
414
			{
				std::cout << "warning texture index <" << tempIndex << "> unknown" << std::endl;
				dataDestination->texture = "";
				continue;
			}
415
			dataDestination->texture = vTextures[tempIndex];
Anakin's avatar
Anakin committed
416
			continue;
Anakin's avatar
Anakin committed
417
418
419
420
		}

		if (!strcmp("POSL", (*it)->name))
		{
421
			readVertex(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
422
			continue;
Anakin's avatar
Anakin committed
423
424
		}

425
		/*if (!strcmp("NRML", (*it)->name))
Anakin's avatar
Anakin committed
426
427
428
429
430
		{
			fsMesh.seekg((*it)->position);
			// List of normals
			// long int - 4 - number of normal vectores stored in this list
			// float[3][] - 12 each - UVW vector for each vertex
Anakin's avatar
Anakin committed
431
			continue;
432
		}*/
Anakin's avatar
Anakin committed
433
434
435

		if (!strcmp("UV0L", (*it)->name))
		{
436
			readUV(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
437
			continue;
Anakin's avatar
Anakin committed
438
439
440
441
442
		}

		if (!strcmp("STRP", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
Anakin's avatar
Anakin committed
443
444
445
446

			fsMesh.seekg((*it)->position);
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->meshSize), sizeof(dataDestination->meshSize));

Anakin's avatar
Anakin committed
447
			dataDestination->mesh = new std::uint32_t[dataDestination->meshSize];
Anakin's avatar
Anakin committed
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467

			for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
			{
				std::uint16_t tempValue[3];
				fsMesh.read(reinterpret_cast<char*>(&tempValue[0]), sizeof(std::uint16_t));
				fsMesh.read(reinterpret_cast<char*>(&tempValue[1]), sizeof(std::uint16_t));
				fsMesh.read(reinterpret_cast<char*>(&tempValue[2]), sizeof(std::uint16_t));

				if (!(tempValue[0] >> 15 && tempValue[1] >> 15 && !(tempValue[2] >> 15)))
					throw std::invalid_argument("invalid file. go and triangulate!");

				tempValue[0] = (std::uint16_t(tempValue[0] << 1) >> 1);
				tempValue[1] = (std::uint16_t(tempValue[1] << 1) >> 1);

				dataDestination->mesh[i] = (std::uint32_t)tempValue[0];
				dataDestination->mesh[i + 1] = (std::uint32_t)tempValue[1];
				dataDestination->mesh[i + 2] = (std::uint32_t)tempValue[2];
			}

			continue;
Anakin's avatar
Anakin committed
468
469
470
471
472
473
474
475
476
477
478
		}
	}
}

void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
	{
		if (!strcmp("CTEX", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
479
			char* buffer = new char[(*it)->size];
Anakin's avatar
Anakin committed
480
			*buffer = { 0 };
481
482
			fsMesh.read(buffer, (*it)->size);
			dataDestination->texture = buffer;
483
			delete buffer;
Anakin's avatar
Anakin committed
484
			continue;
Anakin's avatar
Anakin committed
485
486
487
		}

		if (!strcmp("CPOS", (*it)->name))
Anakin's avatar
Anakin committed
488
		{
489
			readVertex(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
490
491
			continue;
		}
Anakin's avatar
Anakin committed
492
493

		if (!strcmp("CUV0", (*it)->name))
Anakin's avatar
Anakin committed
494
		{
495
			readUV(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
496
497
			continue;
		}
Anakin's avatar
Anakin committed
498
499

		if (!strcmp("CMSH", (*it)->name))
Anakin's avatar
Anakin committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
		{
			fsMesh.seekg((*it)->position);
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->meshSize), sizeof(dataDestination->meshSize));

			dataDestination->mesh = new std::uint32_t[dataDestination->meshSize * 3];

			for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
			{
				fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i]), sizeof(std::uint32_t));
				fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i + 1]), sizeof(std::uint32_t));
				fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i + 2]), sizeof(std::uint32_t));
			}
			continue;
		}
514
515
516
517
518
519
520
521
522
523
524
	}
}

void Object::readVertex(Modl* dataDestination, std::streampos position)
{
	std::uint32_t tempSize;
	fsMesh.seekg(position);
	fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));

	dataDestination->vertex = new float[tempSize * 3];

Anakin's avatar
Anakin committed
525
	for (unsigned int i = 0; i < tempSize * 3; i++)
526
527
528
529
530
531
532
533
534
535
536
		fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i]), sizeof(float));
}

void Object::readUV(Modl* dataDestination, std::streampos position)
{
	std::uint32_t tempSize;
	fsMesh.seekg(position);
	fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));

	dataDestination->uv = new float[tempSize * 2];

Anakin's avatar
Anakin committed
537
	for (unsigned int i = 0; i < tempSize * 2; i++)
538
		fsMesh.read(reinterpret_cast<char*>(&dataDestination->uv[i]), sizeof(float));
Anakin's avatar
Anakin committed
539
540
}

541

Anakin's avatar
Anakin committed
542
543
/////////////////////////////////////////////////////////////////////////
// public getter
544

Anakin's avatar
Anakin committed
545
546
547
548
549
std::vector<Modl*> Object::getModels() const
{
	return vModls;
}

550
551
552
553

/////////////////////////////////////////////////////////////////////////
// public functions