diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index 1758e66..9307ffb 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -3,6 +3,7 @@ #include #include #include +#include enum Mtyp { null, @@ -23,7 +24,6 @@ struct Modl { std::string name; std::string parent; Mtyp type; - std::uint32_t zeroBaseIndex; std::uint32_t renderFlags; struct { float scale[3]; @@ -36,6 +36,11 @@ struct Modl { float data2; float data3; } swci; + std::string texture; + float* vertex; + float* uv; + std::uint32_t meshSize; + std::uint32_t* mesh; }; @@ -53,12 +58,19 @@ private: private: + void setModlDefault(Modl* model); void loadChunks(std::list &destination, std::streampos start, const std::uint32_t end); void analyseModlChunks(Modl* dataDestination, std::list &chunkList); void analyseGeomChunks(Modl* dataDestination, std::list &chunkList); void analyseSegmChunks(Modl* dataDestination, std::list &chunkList); void analyseClthChunks(Modl* dataDestination, std::list &chunkList); + void readVertex(Modl* dataDestination, std::streampos position); + void readUV(Modl* dataDestination, std::streampos position); + void readMesh(Modl* dataDestination, std::streampos position); + public: + std::vector getVertex() const; + std::vector getUV() const; }; diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 06ec2f2..841d300 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -69,6 +69,32 @@ Object::~Object() ///////////////////////////////////////////////////////////////////////// // private functions +void Object::setModlDefault(Modl * model) +{ + model->name = ""; + model->parent = ""; + model->type = null; + model->renderFlags = -1; + model->tran.scale[0] = 0; + model->tran.scale[1] = 0; + model->tran.scale[2] = 0; + model->tran.rotation[0] = 0; + model->tran.rotation[1] = 0; + model->tran.rotation[2] = 0; + model->tran.rotation[3] = 0; + model->tran.translation[0] = 0; + model->tran.translation[1] = 0; + model->tran.translation[2] = 0; + model->swci.type = -1; + model->swci.data1 = -1; + model->swci.data2 = -1; + model->swci.data3 = -1; + model->texture = ""; + model->vertex = NULL; + model->uv = NULL; + model->mesh = NULL; +} + void Object::loadChunks(std::list& destination, std::streampos start, const std::uint32_t end) { // jump to first chunk @@ -116,12 +142,6 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c dataDestination->type = (Mtyp)tempType; } - if (!strcmp("MNDX", (*it)->name)) - { - fsMesh.seekg((*it)->position); - fsMesh.read(reinterpret_cast(&dataDestination->zeroBaseIndex), sizeof(dataDestination->zeroBaseIndex)); - } - if (!strcmp("PRNT", (*it)->name)) { fsMesh.seekg((*it)->position); @@ -133,9 +153,9 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c if (!strcmp("NAME", (*it)->name)) { fsMesh.seekg((*it)->position); - char tempName[33] = { 0 }; - fsMesh.read(reinterpret_cast(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size); - dataDestination->name = tempName; + char* buffer = new char[(*it)->size]; + fsMesh.read(buffer, (*it)->size); + dataDestination->name = buffer; } if (!strcmp("FLGS", (*it)->name)) @@ -211,8 +231,7 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list& delete tempCursor; } } - - if (!strcmp("CLTH", (*it)->name)) + else if (!strcmp("CLTH", (*it)->name)) { // get all subchunks std::list tempClthChunks; @@ -263,10 +282,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& if (!strcmp("POSL", (*it)->name)) { - fsMesh.seekg((*it)->position); - // list of vertex coordinates - // long int - 4 - number of coordinates stored in this list - // float[3][] - 12 each - XYZ coordinates + readVertex(dataDestination, (*it)->position); } if (!strcmp("NRML", (*it)->name)) @@ -279,14 +295,12 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& if (!strcmp("UV0L", (*it)->name)) { - fsMesh.seekg((*it)->position); - // List of UV - // long int - 4 - number of UV - // float[2][] - 8 each - UV coordinate + readUV(dataDestination, (*it)->position); } if (!strcmp("STRP", (*it)->name)) { + readMesh(dataDestination, (*it)->position); fsMesh.seekg((*it)->position); /* List of triangles strips. The start of a strip is indicated by 2 entries @@ -309,42 +323,105 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list& if (!strcmp("CTEX", (*it)->name)) { fsMesh.seekg((*it)->position); - // texture name with extension (how long could it be??) - // ascii + char* buffer = new char[(*it)->size]; + fsMesh.read(buffer, (*it)->size); + dataDestination->texture = buffer; } if (!strcmp("CPOS", (*it)->name)) - { - fsMesh.seekg((*it)->position); - // list of Vertex coordinates - // long int (4) number of vertex - // float[3][] (12 each) XYZ coordinates - } + readVertex(dataDestination, (*it)->position); 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 - } + readUV(dataDestination, (*it)->position); 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 - } - + readMesh(dataDestination, (*it)->position); } } +void Object::readVertex(Modl* dataDestination, std::streampos position) +{ + std::uint32_t tempSize; + fsMesh.seekg(position); + fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); + + dataDestination->vertex = new float[tempSize * 3]; + + for (unsigned int i = 0; i < tempSize; i += 3) + { + fsMesh.read(reinterpret_cast(&dataDestination->vertex[i]), sizeof(float)); + fsMesh.read(reinterpret_cast(&dataDestination->vertex[i + 1]), sizeof(float)); + fsMesh.read(reinterpret_cast(&dataDestination->vertex[i + 2]), sizeof(float)); + } +} + +void Object::readUV(Modl* dataDestination, std::streampos position) +{ + std::uint32_t tempSize; + fsMesh.seekg(position); + fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); + + dataDestination->uv = new float[tempSize * 2]; + + for (unsigned int i = 0; i < tempSize; i += 2) + { + fsMesh.read(reinterpret_cast(&dataDestination->uv[i]), sizeof(float)); + fsMesh.read(reinterpret_cast(&dataDestination->uv[i + 1]), sizeof(float)); + } +} + +void Object::readMesh(Modl* dataDestination, std::streampos position) +{ + fsMesh.seekg(position); + fsMesh.read(reinterpret_cast(&dataDestination->meshSize), sizeof(dataDestination->meshSize)); + + dataDestination->mesh = new std::uint32_t[dataDestination->meshSize * 3]; + + for (unsigned int i = 0; i < dataDestination->meshSize; i += 3) + { + fsMesh.read(reinterpret_cast(&dataDestination->mesh[i]), sizeof(std::uint32_t)); + fsMesh.read(reinterpret_cast(&dataDestination->mesh[i + 1]), sizeof(std::uint32_t)); + fsMesh.read(reinterpret_cast(&dataDestination->mesh[i + 2]), sizeof(std::uint32_t)); + } + + + for (unsigned int i = 0; i < dataDestination->meshSize; i += 3) + std::cout << dataDestination->mesh[i] << " " << dataDestination->mesh[i + 1] << " " << dataDestination->mesh[i + 2] << std::endl; + +} + ///////////////////////////////////////////////////////////////////////// // public getter +std::vector Object::getVertex() const +{ + std::vector tempData; + + for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) + { + for (int i = 0; i < (*it)->meshSize; i++) + { + + int tempIndex = (*it)->mesh[i]; + tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i]]); + } + } + + return tempData; +} + +std::vector Object::getUV() const +{ + std::vector tempData; + + for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) + for (int i = 0; i < (*it)->meshSize; i++) + tempData.push_back((GLfloat)(*it)->uv[(*it)->mesh[i]]); + + return tempData; +} ///////////////////////////////////////////////////////////////////////// diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index fda26d4..49dac3a 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -8,6 +8,7 @@ #include "shader.hpp" #include "import.h" #include "Texture.h" +#include "Object.h" #define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr" #define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr" @@ -280,8 +281,21 @@ void OpenGLController::loadMsh(const char * path) gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler"); // get data - vfVertices = loadData(); - vfUV = loadUV(); + try + { + Object obj(path); + vfVertices = obj.getVertex(); + + vfUV = obj.getUV(); + } + catch (std::invalid_argument e) + { + MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); + exit(1); + } + + //vfVertices = loadData(); + //vfUV = loadUV(); glGenTextures(1, &gluiTextureID); glBindTexture(GL_TEXTURE_2D, gluiTextureID); diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp index e7d920d..05b03c9 100644 --- a/MshViewer/main.cpp +++ b/MshViewer/main.cpp @@ -28,7 +28,7 @@ openGL: OpenGLController *scene = OpenGLController::getInstance(); - scene->loadMsh("test.msh"); + scene->loadMsh("..\\Release\\Msh\\cube.msh"); do { scene->updateScene();