Skip to content
Snippets Groups Projects
Commit b2f174d1 authored by Anakin's avatar Anakin
Browse files

clean up code:

removed useless variables,
moved code into new functions
parent 548150f3
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,7 @@
#include <fstream>
#include <string>
enum mtyp {
enum Mtyp {
null,
dynamicMesh,
cloth,
......@@ -13,18 +13,16 @@ enum mtyp {
shadowMesh = 6
};
struct chunkHeader {
struct ChunkHeader {
char name[5];
std::uint32_t size;
std::streampos position;
};
struct modl {
struct Modl {
std::string name;
std::uint32_t size;
std::streampos position;
std::string parent;
mtyp type;
Mtyp type;
std::uint32_t zeroBaseIndex;
std::uint32_t renderFlags;
struct {
......@@ -49,13 +47,17 @@ public:
private:
std::list<chunkHeader*> lChunkMsh2;
std::list<modl*> lModls;
std::list<ChunkHeader*> lChunkMsh2;
std::list<Modl*> lModls;
std::fstream fsMesh;
private:
void loadChunks(std::list<chunkHeader*> &destination, std::streampos start, const std::uint32_t end);
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t end);
void analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
void analyseGeomChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
void analyseSegmChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
void analyseClthChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
public:
......
......@@ -29,33 +29,97 @@ Object::Object(const char* path)
loadChunks(lChunkMsh2, fsMesh.tellg(), tempSize);
// search for all MODL Chunks
for (std::list<chunkHeader*>::iterator it = lChunkMsh2.begin(); it != lChunkMsh2.end(); it++)
for (std::list<ChunkHeader*>::iterator it = lChunkMsh2.begin(); it != lChunkMsh2.end(); it++)
{
if (!strcmp("MODL", (*it)->name))
{
modl* tempModl = new modl;
tempModl->size = (*it)->size;
tempModl->position = (*it)->position;
std::list<chunkHeader*> tempChunks;
Modl* tempModl = new Modl;
// get all subchunks
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++)
analyseModlChunks(tempModl, tempChunks);
//clean up
while (!tempChunks.empty())
{
ChunkHeader* tempCursor = tempChunks.front();
tempChunks.pop_front();
delete tempCursor;
}
// save Model data
lModls.push_back(tempModl);
}
}
// close file
fsMesh.close();
}
Object::~Object()
{
//delete Chunk list;
}
/////////////////////////////////////////////////////////////////////////
// private functions
void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t end)
{
// jump to first chunk
fsMesh.seekg(start);
do
{
ChunkHeader* tempHeader = new ChunkHeader();
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();
destination.push_back(tempHeader);
fsMesh.seekg(tempHeader->size, std::ios_base::cur);
// reached end
if (fsMesh.tellg() - start == end)
break;
// 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);
std::cout << "got all chunks, totaly found: " << destination.size() << std::endl;
}
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));
tempModl->type = (mtyp)tempType;
dataDestination->type = (Mtyp)tempType;
}
if (!strcmp("MNDX", (*it)->name))
{
fsMesh.seekg((*it)->position);
fsMesh.read(reinterpret_cast<char*>(&tempModl->zeroBaseIndex), sizeof(tempModl->zeroBaseIndex));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->zeroBaseIndex), sizeof(dataDestination->zeroBaseIndex));
}
if (!strcmp("PRNT", (*it)->name))
......@@ -63,7 +127,7 @@ Object::Object(const char* path)
fsMesh.seekg((*it)->position);
char tempName[33] = { 0 };
fsMesh.read(reinterpret_cast<char*>(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size);
tempModl->parent = tempName;
dataDestination->parent = tempName;
}
if (!strcmp("NAME", (*it)->name))
......@@ -71,103 +135,209 @@ Object::Object(const char* path)
fsMesh.seekg((*it)->position);
char tempName[33] = { 0 };
fsMesh.read(reinterpret_cast<char*>(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size);
tempModl->name = tempName;
dataDestination->name = tempName;
}
if (!strcmp("FLGS", (*it)->name))
{
fsMesh.seekg((*it)->position);
fsMesh.read(reinterpret_cast<char*>(&tempModl->renderFlags), sizeof(tempModl->renderFlags));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->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));
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));
}
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;
}
}
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));
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));
}
}
lModls.push_back(tempModl);
}
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 (!tempChunks.empty())
while (!tempSegmChunks.empty())
{
chunkHeader* tempCursor = tempChunks.front();
tempChunks.pop_front();
ChunkHeader* tempCursor = tempSegmChunks.front();
tempSegmChunks.pop_front();
delete tempCursor;
}
}
}
// close file
fsMesh.close();
}
if (!strcmp("CLTH", (*it)->name))
{
// get all subchunks
std::list<ChunkHeader*> tempClthChunks;
loadChunks(tempClthChunks, (*it)->position, (*it)->size);
Object::~Object()
// evaluate CLTH subchunks
analyseClthChunks(dataDestination, tempClthChunks);
// clean up
while (!tempClthChunks.empty())
{
//delete Chunk list;
ChunkHeader* tempCursor = tempClthChunks.front();
tempClthChunks.pop_front();
delete tempCursor;
}
}
}
}
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
*/
/////////////////////////////////////////////////////////////////////////
// private functions
}
void Object::loadChunks(std::list<chunkHeader*>& destination, std::streampos start, const std::uint32_t end)
if (!strcmp("MATI", (*it)->name))
{
// jump to first chunk
fsMesh.seekg(start);
fsMesh.seekg((*it)->position);
// material index index into MATL
// long int - 4 - material index
}
do
if (!strcmp("POSL", (*it)->name))
{
chunkHeader* tempHeader = new chunkHeader();
fsMesh.seekg((*it)->position);
// list of vertex coordinates
// long int - 4 - number of coordinates stored in this list
// float[3][] - 12 each - XYZ coordinates
}
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();
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
}
destination.push_back(tempHeader);
if (!strcmp("UV0L", (*it)->name))
{
fsMesh.seekg((*it)->position);
// List of UV
// long int - 4 - number of UV
// float[2][] - 8 each - UV coordinate
}
fsMesh.seekg(tempHeader->size, std::ios_base::cur);
if (!strcmp("STRP", (*it)->name))
{
fsMesh.seekg((*it)->position);
/*
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
*/
}
}
}
// reached end
if (fsMesh.tellg() - start == end)
break;
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);
// texture name with extension (how long could it be??)
// ascii
}
// error. Maybe the size information is corrupted
if (!fsMesh.good())
if (!strcmp("CPOS", (*it)->name))
{
std::cout << "WARNING: corrupted file. Trying to continue" << std::endl;
fsMesh.clear();
break;
fsMesh.seekg((*it)->position);
// list of Vertex coordinates
// long int (4) number of vertex
// float[3][] (12 each) XYZ coordinates
}
} while (true);
if (!strcmp("CUV0", (*it)->name))
{
fsMesh.seekg((*it)->position);
// list of UV coordinates
// long int (4) number of UV Coordinates
// float[2][] (8 each) UV coordinate
}
std::cout << "got all chunks, totaly found: " << destination.size() << std::endl;
if (!strcmp("CMSH", (*it)->name))
{
fsMesh.seekg((*it)->position);
// cloth tirangles
// long int (4) number of points
// long int[3][] (16 each) triangle points defined CCW
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment