Object.cpp 14.4 KB
Newer Older
1
#include "Object.h"
Anakin's avatar
Anakin committed
2
#include <iostream>
3
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
	// jump into msh2 todo: search for MSH2 if there is a shadowvolume
18
19
20
21
22
23
24
	fsMesh.seekg(8);
	char tempChunkName[5] = { 0 };
	fsMesh.read(reinterpret_cast<char*>(&tempChunkName[0]), sizeof(tempChunkName) - 1);

	if (strcmp(tempChunkName, "MSH2"))
		throw std::invalid_argument(std::string("corrupted file MSH2 expected instead of ") += tempChunkName);

25
26
	std::uint32_t tempSize;
	fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
27

28
29
	// get all sub chunks from MSH2
	loadChunks(lChunkMsh2, fsMesh.tellg(), tempSize);
30

31
	// search for all MODL Chunks
Anakin's avatar
Anakin committed
32
	for (std::list<ChunkHeader*>::iterator it = lChunkMsh2.begin(); it != lChunkMsh2.end(); it++)
33
34
35
	{
		if (!strcmp("MODL", (*it)->name))
		{
Anakin's avatar
Anakin committed
36
			Modl* tempModl = new Modl;
Anakin's avatar
Anakin committed
37
			setModlDefault(tempModl);
Anakin's avatar
Anakin committed
38

Anakin's avatar
Anakin committed
39
40
			// get all subchunks
			std::list<ChunkHeader*> tempChunks;
Anakin's avatar
Anakin committed
41
42
43
			loadChunks(tempChunks, (*it)->position, (*it)->size);
			
			// evaluate MODL subchunks
Anakin's avatar
Anakin committed
44
			analyseModlChunks(tempModl, tempChunks);
Anakin's avatar
Anakin committed
45
46
47
48

			//clean up
			while (!tempChunks.empty())
			{
Anakin's avatar
Anakin committed
49
				ChunkHeader* tempCursor = tempChunks.front();
Anakin's avatar
Anakin committed
50
51
52
				tempChunks.pop_front();
				delete tempCursor;
			}
Anakin's avatar
Anakin committed
53
54
55

			// save Model data
			lModls.push_back(tempModl);
56
57
		}
	}
58

59
60
61
62
63
64
65
66
	// close file
	fsMesh.close();
}

Object::~Object()
{
	//delete Chunk list;	
}
67
68


69

70
71
/////////////////////////////////////////////////////////////////////////
// private functions
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
void Object::setModlDefault(Modl * model)
{
	model->name = "";
	model->parent = "";
	model->type = null;
	model->renderFlags = -1;
	model->tran.scale[0] = 0;
	model->tran.scale[1] = 0;
	model->tran.scale[2] = 0;
	model->tran.rotation[0] = 0;
	model->tran.rotation[1] = 0;
	model->tran.rotation[2] = 0;
	model->tran.rotation[3] = 0;
	model->tran.translation[0] = 0;
	model->tran.translation[1] = 0;
	model->tran.translation[2] = 0;
	model->swci.type = -1;
	model->swci.data1 = -1;
	model->swci.data2 = -1;
	model->swci.data3 = -1;
	model->texture = "";
	model->vertex = NULL;
	model->uv = NULL;
	model->mesh = NULL;
Anakin's avatar
Anakin committed
97
	model->meshSize = 0;
98
99
}

Anakin's avatar
Anakin committed
100
void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t end)
101
{
102
	// jump to first chunk
103
	fsMesh.seekg(start);
104

Anakin's avatar
Anakin committed
105
106
	do
	{
Anakin's avatar
Anakin committed
107
		ChunkHeader* tempHeader = new ChunkHeader();
108

109
110
111
		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();
112

113
		destination.push_back(tempHeader);
114

115
		fsMesh.seekg(tempHeader->size, std::ios_base::cur);
116

117
118
		// reached end
		if (fsMesh.tellg() - start == end)
Anakin's avatar
Anakin committed
119
			break;
120
121
122
123
124
125
126
127
128
129

		// 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);
130
131

	std::cout << "got all chunks, totaly found: " << destination.size() << std::endl;
Anakin's avatar
Anakin committed
132

133
134
}

Anakin's avatar
Anakin committed
135
136
137
138
139
140
141
142
143
144
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
145
			continue;
Anakin's avatar
Anakin committed
146
147
148
149
150
151
152
153
		}

		if (!strcmp("PRNT", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			char tempName[33] = { 0 };
			fsMesh.read(reinterpret_cast<char*>(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size);
			dataDestination->parent = tempName;
Anakin's avatar
Anakin committed
154
			continue;
Anakin's avatar
Anakin committed
155
156
157
158
159
		}

		if (!strcmp("NAME", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
160
161
162
			char* buffer = new char[(*it)->size];
			fsMesh.read(buffer, (*it)->size);
			dataDestination->name = buffer;
Anakin's avatar
Anakin committed
163
			continue;
Anakin's avatar
Anakin committed
164
165
166
167
168
169
		}

		if (!strcmp("FLGS", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags));
Anakin's avatar
Anakin committed
170
			continue;
Anakin's avatar
Anakin committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
		}

		if (!strcmp("TRAN", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[0]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[1]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[2]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[0]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[1]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[2]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[3]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[0]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[1]), sizeof(float));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[2]), sizeof(float));
Anakin's avatar
Anakin committed
186
			continue;
Anakin's avatar
Anakin committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
		}

		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
205
206

			continue;
Anakin's avatar
Anakin committed
207
208
209
210
211
212
213
214
215
		}

		if (!strcmp("SWCI", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.type), sizeof(dataDestination->swci.type));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data1), sizeof(dataDestination->swci.data1));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data2), sizeof(dataDestination->swci.data2));
			fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data3), sizeof(dataDestination->swci.data3));
Anakin's avatar
Anakin committed
216
			continue;
Anakin's avatar
Anakin committed
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
		}
	}

}

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
243
			continue;
Anakin's avatar
Anakin committed
244
		}
Anakin's avatar
Anakin committed
245
246
		
		if (!strcmp("CLTH", (*it)->name))
Anakin's avatar
Anakin committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
		{
			// 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
262
			continue;
Anakin's avatar
Anakin committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
		}
	}

}

void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
	{
		if (!strcmp("SHDW", (*it)->name))
		{
			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
			*/
Anakin's avatar
Anakin committed
286
			continue;
Anakin's avatar
Anakin committed
287
288
289
290
291
292
293
		}

		if (!strcmp("MATI", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
			// material index index into MATL
			// long int - 4 - material index
Anakin's avatar
Anakin committed
294
			continue;
Anakin's avatar
Anakin committed
295
296
297
298
		}

		if (!strcmp("POSL", (*it)->name))
		{
299
			readVertex(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
300
			continue;
Anakin's avatar
Anakin committed
301
302
303
304
305
306
307
308
		}

		if (!strcmp("NRML", (*it)->name))
		{
			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
309
			continue;
Anakin's avatar
Anakin committed
310
311
312
313
		}

		if (!strcmp("UV0L", (*it)->name))
		{
314
			readUV(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
315
			continue;
Anakin's avatar
Anakin committed
316
317
318
319
320
		}

		if (!strcmp("STRP", (*it)->name))
		{
			fsMesh.seekg((*it)->position);
Anakin's avatar
Anakin committed
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

			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)
			{
				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];

			}

Anakin's avatar
Anakin committed
346
			std::cout << "triangles: " << dataDestination->meshSize << std::endl;
Anakin's avatar
Anakin committed
347
			for (int i = 0; i < dataDestination->meshSize; i += 3)
Anakin's avatar
Anakin committed
348
				std::cout << dataDestination->mesh[i] << " <> " << dataDestination->mesh[i + 1] << " <> " << dataDestination->mesh[i + 2] << std::endl;
Anakin's avatar
Anakin committed
349
350
351

			continue;

Anakin's avatar
Anakin committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
			/*
			List of triangles strips. The start of a strip is indicated by 2 entries
			with a high bit set (0x8000 or 32768 added). Triangles are listed CW, CCW,
			CW, CCW... NOTE: In some meshes this chunk has a trailing short which is not
			calculated into the length/size of this chunk or the # of indices. This
			short can be ignored. If added to the last polygon it will break it as it
			always seems to be 0.
			long int - 4 - number of indicies into POSL
			short int[] - 2 each - index into POSL the indices will form polygons
			*/
		}
	}
}

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);
373
374
375
			char* buffer = new char[(*it)->size];
			fsMesh.read(buffer, (*it)->size);
			dataDestination->texture = buffer;
Anakin's avatar
Anakin committed
376
			continue;
Anakin's avatar
Anakin committed
377
378
379
		}

		if (!strcmp("CPOS", (*it)->name))
Anakin's avatar
Anakin committed
380
		{
381
			readVertex(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
382
383
			continue;
		}
Anakin's avatar
Anakin committed
384
385

		if (!strcmp("CUV0", (*it)->name))
Anakin's avatar
Anakin committed
386
		{
387
			readUV(dataDestination, (*it)->position);
Anakin's avatar
Anakin committed
388
389
			continue;
		}
Anakin's avatar
Anakin committed
390
391

		if (!strcmp("CMSH", (*it)->name))
Anakin's avatar
Anakin committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
		{
			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;
		}
406
407
408
409
410
411
412
413
414
415
416
417
	}

}

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
418
	for (unsigned int i = 0; i < tempSize * 3; i += 3)
419
420
421
422
423
	{
		fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i]), sizeof(float));
		fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i + 1]), sizeof(float));
		fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i + 2]), sizeof(float));
	}
Anakin's avatar
Anakin committed
424
425
426
427
428
429

	std::cout << "Vertex number: " << tempSize << std::endl;
	for (int i = 0; i < tempSize * 3; i += 3)
		std::cout << dataDestination->vertex[i] << " <> " << dataDestination->vertex[i + 1] << " <> " << dataDestination->vertex[i + 2] << std::endl;


430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
}

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];

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

Anakin's avatar
Anakin committed
446
447
448
	std::cout << "UV" << std::endl;
	for (int i = 0; i < dataDestination->meshSize; i += 3)
		std::cout << dataDestination->mesh[i] << " - " << dataDestination->mesh[i + 1] << " - " << dataDestination->mesh[i + 2] << std::endl;
449

Anakin's avatar
Anakin committed
450
451
}

452

Anakin's avatar
Anakin committed
453
454
/////////////////////////////////////////////////////////////////////////
// public getter
455

456
457
458
459
460
461
std::vector<GLfloat> Object::getVertex() const
{
	std::vector<GLfloat> tempData;

	for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++)
	{
Anakin's avatar
Anakin committed
462
		for (unsigned int i = 0; i < (*it)->meshSize; i++)
463
		{
Anakin's avatar
Anakin committed
464
465
466
			tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] * 3]);
			tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] * 3 + 1]);
			tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] * 3 + 2]);
467
468
469
470
471
472
473
474
475
476
477
		}
	}

	return tempData;
}

std::vector<GLfloat> Object::getUV() const
{
	std::vector<GLfloat> tempData;

	for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++)
Anakin's avatar
Anakin committed
478
479
	{
		if ((*it)->uv == NULL)
Anakin's avatar
Anakin committed
480
481
482
		{
			for (unsigned int i = 0; i < (*it)->meshSize; i++)
				tempData.push_back(1.0);
Anakin's avatar
Anakin committed
483
			continue;
Anakin's avatar
Anakin committed
484
485
		}
		for (unsigned int i = 0; i < (*it)->meshSize; i++)
Anakin's avatar
Anakin committed
486
		{
487
			tempData.push_back((GLfloat)(*it)->uv[(*it)->mesh[i]]);
Anakin's avatar
Anakin committed
488
489
		}
	}
490
491
492

	return tempData;
}
493
494
495
496
497


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