Object.cpp 5.28 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))
		{
Anakin's avatar
Anakin committed
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
			modl* tempModl = new modl;
			tempModl->size = (*it)->size;
			tempModl->position = (*it)->position;

			std::list<chunkHeader*> tempChunks;

			loadChunks(tempChunks, (*it)->position, (*it)->size);
			
			// evaluate MODL subchunks
			for (std::list<chunkHeader*>::iterator it = tempChunks.begin(); it != tempChunks.end(); it++)
			{
				if (!strcmp("MTYP", (*it)->name))
				{
					fsMesh.seekg((*it)->position);
					std::uint32_t tempType;
					fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType));
					tempModl->type = (mtyp)tempType;
				}

				if (!strcmp("MNDX", (*it)->name))
				{
					fsMesh.seekg((*it)->position);
					fsMesh.read(reinterpret_cast<char*>(&tempModl->zeroBaseIndex), sizeof(tempModl->zeroBaseIndex));
				}

				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);
					tempModl->parent = tempName;
				}

				if (!strcmp("NAME", (*it)->name))
				{
					fsMesh.seekg((*it)->position);
					char tempName[33] = { 0 };
					fsMesh.read(reinterpret_cast<char*>(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size);
					tempModl->name = tempName;
				}

				if (!strcmp("FLGS", (*it)->name))
				{
					fsMesh.seekg((*it)->position);
					fsMesh.read(reinterpret_cast<char*>(&tempModl->renderFlags), sizeof(tempModl->renderFlags));
				}

				if (!strcmp("TRAN", (*it)->name))
				{
					fsMesh.seekg((*it)->position);
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.scale[0]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.scale[1]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.scale[2]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.rotation[0]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.rotation[1]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.rotation[2]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.rotation[3]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.translation[0]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.translation[1]), sizeof(float));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->tran.translation[2]), sizeof(float));
				}

				if (!strcmp("GEOM", (*it)->name))
				{

				}

				if (!strcmp("SWCI", (*it)->name))
				{
					fsMesh.seekg((*it)->position);
					fsMesh.read(reinterpret_cast<char*>(&tempModl->swci.type), sizeof(tempModl->swci.type));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->swci.data1), sizeof(tempModl->swci.data1));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->swci.data2), sizeof(tempModl->swci.data2));
					fsMesh.read(reinterpret_cast<char*>(&tempModl->swci.data3), sizeof(tempModl->swci.data3));
				}
			}
			
			lModls.push_back(tempModl);

			//clean up
			while (!tempChunks.empty())
			{
				chunkHeader* tempCursor = tempChunks.front();
				tempChunks.pop_front();
				delete tempCursor;
			}
122
123
		}
	}
124

125
126
127
128
129
130
131
132
	// close file
	fsMesh.close();
}

Object::~Object()
{
	//delete Chunk list;	
}
133
134


135

136
137
/////////////////////////////////////////////////////////////////////////
// private functions
138

139
void Object::loadChunks(std::list<chunkHeader*>& destination, std::streampos start, const std::uint32_t end)
140
{
141
	// jump to first chunk
142
	fsMesh.seekg(start);
143

Anakin's avatar
Anakin committed
144
145
	do
	{
146
		chunkHeader* tempHeader = new chunkHeader();
147

148
149
150
		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();
151

152
		destination.push_back(tempHeader);
153

154
		fsMesh.seekg(tempHeader->size, std::ios_base::cur);
155

156
157
		// reached end
		if (fsMesh.tellg() - start == end)
Anakin's avatar
Anakin committed
158
			break;
159
160
161
162
163
164
165
166
167
168

		// 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);
169
170

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

172
173
174
}


Anakin's avatar
Anakin committed
175
176
/////////////////////////////////////////////////////////////////////////
// public getter
177
178
179
180
181
182



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