using c++11 loops,

reading bbox for each geom,
added some TODO
use the bbox information
This commit is contained in:
Anakin 2016-11-28 14:04:09 +01:00
parent 5ab2f2eaf9
commit ef2c341a1a
4 changed files with 81 additions and 72 deletions

View File

@ -14,6 +14,12 @@ enum Mtyp {
shadowMesh = 6 shadowMesh = 6
}; };
struct Bbox {
float quaternion[4];
float center[3];
float extents[3];
};
struct ChunkHeader { struct ChunkHeader {
char name[5]; char name[5];
std::uint32_t size; std::uint32_t size;
@ -33,6 +39,7 @@ struct Modl {
Mtyp type = null; Mtyp type = null;
std::int32_t renderFlags = -1; std::int32_t renderFlags = -1;
glm::mat4 m4x4Translation = glm::mat4(1.0f); glm::mat4 m4x4Translation = glm::mat4(1.0f);
Bbox boundingBox;
std::vector<Segment*> segmLst; std::vector<Segment*> segmLst;
}; };
@ -49,7 +56,6 @@ private:
std::fstream fsMesh; std::fstream fsMesh;
std::vector<std::string> vTextures; std::vector<std::string> vTextures;
private: 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 analyseMsh2Chunks(std::list<ChunkHeader*> &chunkList); void analyseMsh2Chunks(std::list<ChunkHeader*> &chunkList);

View File

@ -107,28 +107,28 @@ void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos sta
void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList) void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("MATL", (*it)->name)) if (!strcmp("MATL", it->name))
{ {
// "useless" information how many MATD follow // "useless" information how many MATD follow
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
std::uint32_t tempMatdCount; std::uint32_t tempMatdCount;
fsMesh.read(reinterpret_cast<char*>(&tempMatdCount), sizeof(std::uint32_t)); fsMesh.read(reinterpret_cast<char*>(&tempMatdCount), sizeof(std::uint32_t));
// get all MATD from MATL list // get all MATD from MATL list
std::list<ChunkHeader*> tempMatlChunks; std::list<ChunkHeader*> tempMatlChunks;
loadChunks(tempMatlChunks, fsMesh.tellg(), (*it)->size - 4); loadChunks(tempMatlChunks, fsMesh.tellg(), it->size - 4);
// evaluate MATL subchunks // evaluate MATL subchunks
for (std::list<ChunkHeader*>::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++) for (auto& it : tempMatlChunks)
{ {
// This shouldn't be anything else than MATD // This shouldn't be anything else than MATD
if (!strcmp("MATD", (*it)->name)) if (!strcmp("MATD", it->name))
{ {
// get all subchunks from MATD // get all subchunks from MATD
std::list<ChunkHeader*> tempMatdChunks; std::list<ChunkHeader*> tempMatdChunks;
loadChunks(tempMatdChunks, (*it)->position, (*it)->size); loadChunks(tempMatdChunks, it->position, it->size);
vTextures.push_back(""); vTextures.push_back("");
@ -156,13 +156,13 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
continue; continue;
} }
if (!strcmp("MODL", (*it)->name)) if (!strcmp("MODL", it->name))
{ {
Modl* tempModl = new Modl; Modl* tempModl = new Modl;
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempChunks; std::list<ChunkHeader*> tempChunks;
loadChunks(tempChunks, (*it)->position, (*it)->size); loadChunks(tempChunks, it->position, it->size);
// evaluate MODL subchunks // evaluate MODL subchunks
analyseModlChunks(tempModl, tempChunks); analyseModlChunks(tempModl, tempChunks);
@ -185,15 +185,15 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
void Object::analyseMatdChunks(std::list<ChunkHeader*>& chunkList) void Object::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
//TX1D, TX2D, TX3D //TX1D, TX2D, TX3D
if (!strcmp("TX0D", (*it)->name)) if (!strcmp("TX0D", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size + 1]; char* buffer = new char[it->size + 1];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
vTextures.back() = buffer; vTextures.back() = buffer;
delete buffer; delete buffer;
continue; continue;
@ -203,53 +203,53 @@ void Object::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& chunkList) void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("MTYP", (*it)->name)) if (!strcmp("MTYP", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
std::uint32_t tempType; std::uint32_t tempType;
fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType)); fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType));
dataDestination->type = (Mtyp)tempType; dataDestination->type = (Mtyp)tempType;
continue; continue;
} }
if (!strcmp("PRNT", (*it)->name)) if (!strcmp("PRNT", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size]; char* buffer = new char[it->size];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
dataDestination->parent = buffer; dataDestination->parent = buffer;
delete buffer; delete buffer;
continue; continue;
} }
if (!strcmp("NAME", (*it)->name)) if (!strcmp("NAME", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size]; char* buffer = new char[it->size];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
dataDestination->name = buffer; dataDestination->name = buffer;
delete buffer; delete buffer;
continue; continue;
} }
if (!strcmp("FLGS", (*it)->name)) if (!strcmp("FLGS", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags)); fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags));
continue; continue;
} }
if (!strcmp("TRAN", (*it)->name)) if (!strcmp("TRAN", it->name))
{ {
float tempScale[3]; float tempScale[3];
float tempRotation[4]; float tempRotation[4];
float tempTrans[3]; float tempTrans[3];
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float));
@ -260,6 +260,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float));
//TODO: make a function for this
//calculate x,y,z rotation //calculate x,y,z rotation
tempRotation[0] = atan2(2 * (tempRotation[0] * tempRotation[1] + tempRotation[2] * tempRotation[3]), tempRotation[0] = atan2(2 * (tempRotation[0] * tempRotation[1] + tempRotation[2] * tempRotation[3]),
1 - 2 * (pow(tempRotation[1], 2) + pow(tempRotation[2], 2))); 1 - 2 * (pow(tempRotation[1], 2) + pow(tempRotation[2], 2)));
@ -302,11 +303,11 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
continue; continue;
} }
if (!strcmp("GEOM", (*it)->name)) if (!strcmp("GEOM", it->name))
{ {
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempGeomChunks; std::list<ChunkHeader*> tempGeomChunks;
loadChunks(tempGeomChunks, (*it)->position, (*it)->size); loadChunks(tempGeomChunks, it->position, it->size);
// evaluate GEOM subchunks // evaluate GEOM subchunks
analyseGeomChunks(dataDestination, tempGeomChunks); analyseGeomChunks(dataDestination, tempGeomChunks);
@ -326,28 +327,32 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList) void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("BBOX", (*it)->name)) if (!strcmp("BBOX", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
std::uint32_t tempValue;
fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(tempValue));
/* // read in the quaternion
float[4] 16 Quaternion Rotation in XYZW. for (int i = 0; i < 4; i++)
float[3] 12 Center of the BBox. fsMesh.read(reinterpret_cast<char*>(&dataDestination->boundingBox.quaternion[i]), sizeof(float));
float[3] 12 Extents of the BBox(width / 2, height / 2, depth / 2).
float 4 Bounding sphere radius.*/ //read in the center
for (int i = 0; i < 3; i++)
fsMesh.read(reinterpret_cast<char*>(&dataDestination->boundingBox.center[i]), sizeof(float));
//read in the extents
for (int i = 0; i < 3; i++)
fsMesh.read(reinterpret_cast<char*>(&dataDestination->boundingBox.extents[i]), sizeof(float));
continue; continue;
} }
if (!strcmp("SEGM", (*it)->name)) if (!strcmp("SEGM", it->name))
{ {
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempSegmChunks; std::list<ChunkHeader*> tempSegmChunks;
loadChunks(tempSegmChunks, (*it)->position, (*it)->size); loadChunks(tempSegmChunks, it->position, it->size);
// evaluate SEGM subchunks // evaluate SEGM subchunks
analyseSegmChunks(dataDestination, tempSegmChunks); analyseSegmChunks(dataDestination, tempSegmChunks);
@ -362,11 +367,11 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
continue; continue;
} }
if (!strcmp("CLTH", (*it)->name)) if (!strcmp("CLTH", it->name))
{ {
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempClthChunks; std::list<ChunkHeader*> tempClthChunks;
loadChunks(tempClthChunks, (*it)->position, (*it)->size); loadChunks(tempClthChunks, it->position, it->size);
// evaluate CLTH subchunks // evaluate CLTH subchunks
analyseClthChunks(dataDestination, tempClthChunks); analyseClthChunks(dataDestination, tempClthChunks);
@ -387,24 +392,24 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
{ {
Segment* tempData = new Segment; Segment* tempData = new Segment;
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("MATI", (*it)->name)) if (!strcmp("MATI", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&tempData->textureIndex), sizeof(tempData->textureIndex)); fsMesh.read(reinterpret_cast<char*>(&tempData->textureIndex), sizeof(tempData->textureIndex));
continue; continue;
} }
if (!strcmp("POSL", (*it)->name)) if (!strcmp("POSL", it->name))
{ {
readVertex(tempData, (*it)->position); readVertex(tempData, it->position);
continue; continue;
} }
/*if (!strcmp("NRML", (*it)->name)) /*if (!strcmp("NRML", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
std::uint32_t tempSize; std::uint32_t tempSize;
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize)); fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
// List of normals // List of normals
@ -413,13 +418,13 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
continue; continue;
}*/ }*/
if (!strcmp("UV0L", (*it)->name)) if (!strcmp("UV0L", it->name))
{ {
readUV(tempData, (*it)->position); readUV(tempData, it->position);
continue; continue;
} }
if (!strcmp("STRP", (*it)->name)) if (!strcmp("STRP", it->name))
{ {
// don't get null, bone, shadowMesh and hidden mesh indices // don't get null, bone, shadowMesh and hidden mesh indices
if (dataDestination->type == null || if (dataDestination->type == null ||
@ -430,7 +435,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
// jump to the data section and read the size; // jump to the data section and read the size;
std::uint32_t tempSize; std::uint32_t tempSize;
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize)); fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
int highBitCount(0); int highBitCount(0);
@ -472,9 +477,6 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
for (int i = 1; i >= 0; i--) for (int i = 1; i >= 0; i--)
tempPoly.push_back(saveData[i]); tempPoly.push_back(saveData[i]);
// save the old vector and set the pointer to the new one
//tempData->meshIndices.push_back(tempPoly);
//tempPoly = newPointer;
} // if high bit set } // if high bit set
} // for all values } // for all values
@ -494,14 +496,14 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
{ {
Segment* tempData = new Segment; Segment* tempData = new Segment;
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("CTEX", (*it)->name)) if (!strcmp("CTEX", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size]; char* buffer = new char[it->size];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
bool tempFound(false); bool tempFound(false);
@ -525,23 +527,23 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
continue; continue;
} }
if (!strcmp("CPOS", (*it)->name)) if (!strcmp("CPOS", it->name))
{ {
readVertex(tempData, (*it)->position); readVertex(tempData, it->position);
continue; continue;
} }
if (!strcmp("CUV0", (*it)->name)) if (!strcmp("CUV0", it->name))
{ {
readUV(tempData, (*it)->position); readUV(tempData, it->position);
continue; continue;
} }
if (!strcmp("CMSH", (*it)->name)) if (!strcmp("CMSH", it->name))
{ {
// jump to the data section and read the size; // jump to the data section and read the size;
std::uint32_t tempSize; std::uint32_t tempSize;
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize)); fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
std::vector<uint32_t> tempPoly; std::vector<uint32_t> tempPoly;

View File

@ -109,6 +109,7 @@ void OpenGLController::processInit()
void OpenGLController::deleteVectors() void OpenGLController::deleteVectors()
{ {
//TODO: does .clear() work, too??
if (vModels != NULL) if (vModels != NULL)
{ {
while (!vModels->empty()) while (!vModels->empty())

View File

@ -17,7 +17,7 @@ int main(int argc, char** argv)
else else
scene = OpenGLController::getInstance(); scene = OpenGLController::getInstance();
scene->loadMsh("..\\Release\\Msh\\quadPoly.msh"); scene->loadMsh("..\\Release\\Msh\\multiModTex.msh");
do { do {
scene->updateScene(); scene->updateScene();