Object.cpp 2.42 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
32
33
34
35
	for (std::list<chunkHeader*>::iterator it = lChunkMsh2.begin(); it != lChunkMsh2.end(); it++)
	{
		if (!strcmp("MODL", (*it)->name))
		{
36
37
38
			std::list<chunkHeader*>* tempModlList = new std::list<chunkHeader*>;
			loadChunks(*tempModlList, (*it)->position, (*it)->size);
			lChunkModls.push_back(tempModlList);
39
40
		}
	}
41

42
43
44
45
46
47
48
49
	// close file
	fsMesh.close();
}

Object::~Object()
{
	//delete Chunk list;	
}
50
51


52

53
54
/////////////////////////////////////////////////////////////////////////
// private functions
55

56
void Object::loadChunks(std::list<chunkHeader*>& destination, std::streampos start, const std::uint32_t end)
57
{
58
	// jump to first chunk
59
	fsMesh.seekg(start);
60

Anakin's avatar
Anakin committed
61
62
	do
	{
63
		chunkHeader* tempHeader = new chunkHeader();
64

65
66
67
		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();
68

69
		destination.push_back(tempHeader);
70

71
		fsMesh.seekg(tempHeader->size, std::ios_base::cur);
72

73
74
		// reached end
		if (fsMesh.tellg() - start == end)
Anakin's avatar
Anakin committed
75
			break;
76
77
78
79
80
81
82
83
84
85

		// 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);
86
87

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

89
90
91
}


Anakin's avatar
Anakin committed
92
93
/////////////////////////////////////////////////////////////////////////
// public getter
94
95
96
97
98
99



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