get all materials,

save the texture name for every modl,
program crashes at glfwPollEvents(); fix???
This commit is contained in:
Anakin 2016-10-22 13:49:45 +02:00
parent 03553616e7
commit c0a84c7513
3 changed files with 73 additions and 13 deletions

View File

@ -54,13 +54,14 @@ private:
std::list<Modl*> lModls;
std::fstream fsMesh;
std::list<std::string> lTextures;
std::vector<std::string> vTextures;
private:
void setModlDefault(Modl* model);
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t end);
void analyseMsh2Chunks(std::list<ChunkHeader*> &chunkList);
void analyseMatdChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
void analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
void analyseGeomChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
void analyseSegmChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);

View File

@ -2,7 +2,6 @@
#include <iostream>
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
@ -24,7 +23,7 @@ Object::Object(const char* path)
fsMesh.read(reinterpret_cast<char*>(&tempFileSize), sizeof(tempFileSize));
loadChunks(tempMainChunks, fsMesh.tellg(), tempFileSize);
// evaluate sub chunks (= find MSH2)
// evaluate HEDR subchunks (= find MSH2)
for (std::list<ChunkHeader*>::iterator it = tempMainChunks.begin(); it != tempMainChunks.end(); it++)
{
if (!strcmp("MSH2", (*it)->name))
@ -65,7 +64,6 @@ Object::~Object()
}
/////////////////////////////////////////////////////////////////////////
// private functions
@ -135,17 +133,51 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
{
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
{
Modl* tempModl = new Modl;
setModlDefault(tempModl);
if (!strcmp("MATL", (*it)->name))
{
// get all MATD from MATL list
std::list<ChunkHeader*> tempMatlChunks;
loadChunks(tempMatlChunks, (*it)->position, (*it)->size);
// evaluate MATL subchunks
for (std::list<ChunkHeader*>::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++)
{
// This shouldn't be anything else than MATD
if (!strcmp("MATD", (*it)->name))
{
// get all subchunks from MATD
std::list<ChunkHeader*> tempMatdChunks;
loadChunks(tempMatdChunks, (*it)->position, (*it)->size);
// analyse MATD subchunks
analyseMatdChunks(tempModl, tempMatdChunks);
// clean up
while (!tempMatdChunks.empty())
{
ChunkHeader* tempCursor = tempMatdChunks.front();
tempMatdChunks.pop_front();
delete tempCursor;
}
}
}
// clean up
while (!tempMatlChunks.empty())
{
ChunkHeader* tempCursor = tempMatlChunks.front();
tempMatlChunks.pop_front();
delete tempCursor;
}
continue;
}
if (!strcmp("MODL", (*it)->name))
{
Modl* tempModl = new Modl;
setModlDefault(tempModl);
// get all subchunks
std::list<ChunkHeader*> tempChunks;
loadChunks(tempChunks, (*it)->position, (*it)->size);
@ -161,9 +193,27 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
delete tempCursor;
}
continue;
}
// save Model data
lModls.push_back(tempModl);
}
}
void Object::analyseMatdChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
{
//TX1D, TX2D, TX3D
if (!strcmp("TX0D", (*it)->name))
{
fsMesh.seekg((*it)->position);
char* buffer = new char[(*it)->size + 1];
*buffer = { 0 };
fsMesh.read(buffer, (*it)->size);
vTextures.push_back(buffer);
delete buffer;
continue;
}
}
@ -186,6 +236,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
{
fsMesh.seekg((*it)->position);
char* buffer = new char[(*it)->size];
*buffer = { 0 };
fsMesh.read(buffer, (*it)->size);
dataDestination->parent = buffer;
delete buffer;
@ -196,6 +247,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
{
fsMesh.seekg((*it)->position);
char* buffer = new char[(*it)->size];
*buffer = { 0 };
fsMesh.read(buffer, (*it)->size);
dataDestination->name = buffer;
delete buffer;
@ -328,8 +380,15 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
if (!strcmp("MATI", (*it)->name))
{
fsMesh.seekg((*it)->position);
// material index index into MATL
// long int - 4 - material index
std::uint32_t tempIndex;
fsMesh.read(reinterpret_cast<char*>(&tempIndex), sizeof(tempIndex));
if (vTextures.size() < tempIndex - 1)
{
std::cout << "warning texture index <" << tempIndex << "> unknown" << std::endl;
dataDestination->texture = "";
continue;
}
dataDestination->texture = vTextures[tempIndex - 1];
continue;
}
@ -410,6 +469,7 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
{
fsMesh.seekg((*it)->position);
char* buffer = new char[(*it)->size];
*buffer = { 0 };
fsMesh.read(buffer, (*it)->size);
dataDestination->texture = buffer;
delete buffer;

View File

@ -309,11 +309,10 @@ void OpenGLController::loadMsh(const char * path)
}
catch (std::invalid_argument e)
{
GLubyte solidColor[4] = { 255,0,0,255 };
GLubyte solidColor[4] = { 255, 0, 0, 255};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)solidColor);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);