diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index 9307ffb..f1a17f7 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -66,7 +66,6 @@ private: 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: diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 841d300..c116d52 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -93,6 +93,7 @@ void Object::setModlDefault(Modl * model) model->vertex = NULL; model->uv = NULL; model->mesh = NULL; + model->meshSize = 0; } void Object::loadChunks(std::list& destination, std::streampos start, const std::uint32_t end) @@ -140,6 +141,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c std::uint32_t tempType; fsMesh.read(reinterpret_cast(&tempType), sizeof(tempType)); dataDestination->type = (Mtyp)tempType; + continue; } if (!strcmp("PRNT", (*it)->name)) @@ -148,6 +150,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c char tempName[33] = { 0 }; fsMesh.read(reinterpret_cast(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size); dataDestination->parent = tempName; + continue; } if (!strcmp("NAME", (*it)->name)) @@ -156,12 +159,14 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c char* buffer = new char[(*it)->size]; fsMesh.read(buffer, (*it)->size); dataDestination->name = buffer; + continue; } if (!strcmp("FLGS", (*it)->name)) { fsMesh.seekg((*it)->position); fsMesh.read(reinterpret_cast(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags)); + continue; } if (!strcmp("TRAN", (*it)->name)) @@ -177,6 +182,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c fsMesh.read(reinterpret_cast(&dataDestination->tran.translation[0]), sizeof(float)); fsMesh.read(reinterpret_cast(&dataDestination->tran.translation[1]), sizeof(float)); fsMesh.read(reinterpret_cast(&dataDestination->tran.translation[2]), sizeof(float)); + continue; } if (!strcmp("GEOM", (*it)->name)) @@ -195,6 +201,8 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c tempGeomChunks.pop_front(); delete tempCursor; } + + continue; } if (!strcmp("SWCI", (*it)->name)) @@ -204,6 +212,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list& c fsMesh.read(reinterpret_cast(&dataDestination->swci.data1), sizeof(dataDestination->swci.data1)); fsMesh.read(reinterpret_cast(&dataDestination->swci.data2), sizeof(dataDestination->swci.data2)); fsMesh.read(reinterpret_cast(&dataDestination->swci.data3), sizeof(dataDestination->swci.data3)); + continue; } } @@ -230,8 +239,10 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list& tempSegmChunks.pop_front(); delete tempCursor; } + continue; } - else if (!strcmp("CLTH", (*it)->name)) + + if (!strcmp("CLTH", (*it)->name)) { // get all subchunks std::list tempClthChunks; @@ -247,6 +258,7 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list& tempClthChunks.pop_front(); delete tempCursor; } + continue; } } @@ -270,7 +282,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& > 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 */ - + continue; } if (!strcmp("MATI", (*it)->name)) @@ -278,11 +290,13 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& fsMesh.seekg((*it)->position); // material index index into MATL // long int - 4 - material index + continue; } if (!strcmp("POSL", (*it)->name)) { readVertex(dataDestination, (*it)->position); + continue; } if (!strcmp("NRML", (*it)->name)) @@ -291,17 +305,49 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& // List of normals // long int - 4 - number of normal vectores stored in this list // float[3][] - 12 each - UVW vector for each vertex + continue; } if (!strcmp("UV0L", (*it)->name)) { readUV(dataDestination, (*it)->position); + continue; } if (!strcmp("STRP", (*it)->name)) { - readMesh(dataDestination, (*it)->position); fsMesh.seekg((*it)->position); + + fsMesh.seekg((*it)->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) + { + std::uint16_t tempValue[3]; + fsMesh.read(reinterpret_cast(&tempValue[0]), sizeof(std::uint16_t)); + fsMesh.read(reinterpret_cast(&tempValue[1]), sizeof(std::uint16_t)); + fsMesh.read(reinterpret_cast(&tempValue[2]), sizeof(std::uint16_t)); + + if (!(tempValue[0] >> 15 && tempValue[1] >> 15 && !(tempValue[2] >> 15))) + throw std::invalid_argument("invalid file. go and triangulate!"); + + tempValue[0] = (std::uint16_t(tempValue[0] << 1) >> 1); + tempValue[1] = (std::uint16_t(tempValue[1] << 1) >> 1); + + dataDestination->mesh[i] = (std::uint32_t)tempValue[0]; + dataDestination->mesh[i + 1] = (std::uint32_t)tempValue[1]; + dataDestination->mesh[i + 2] = (std::uint32_t)tempValue[2]; + + } + + std::cout << "vertices" << std::endl; + for (int i = 0; i < dataDestination->meshSize; i += 3) + std::cout << dataDestination->mesh[i] << " - " << dataDestination->mesh[i + 1] << " - " << dataDestination->mesh[i + 2] << std::endl; + + continue; + /* 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, @@ -326,16 +372,36 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list& char* buffer = new char[(*it)->size]; fsMesh.read(buffer, (*it)->size); dataDestination->texture = buffer; + continue; } if (!strcmp("CPOS", (*it)->name)) + { readVertex(dataDestination, (*it)->position); + continue; + } if (!strcmp("CUV0", (*it)->name)) + { readUV(dataDestination, (*it)->position); + continue; + } if (!strcmp("CMSH", (*it)->name)) - readMesh(dataDestination, (*it)->position); + { + fsMesh.seekg((*it)->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)); + } + continue; + } } } @@ -369,25 +435,10 @@ void Object::readUV(Modl* dataDestination, std::streampos position) 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; + std::cout << "UV" << std::endl; + for (int i = 0; i < dataDestination->meshSize; i += 3) + std::cout << dataDestination->mesh[i] << " - " << dataDestination->mesh[i + 1] << " - " << dataDestination->mesh[i + 2] << std::endl; } @@ -401,11 +452,11 @@ std::vector Object::getVertex() const for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) { - for (int i = 0; i < (*it)->meshSize; i++) + for (int i = 0; i < (*it)->meshSize; i += 3) { - - int tempIndex = (*it)->mesh[i]; tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i]]); + tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] + 1]); + tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] + 2]); } } @@ -417,8 +468,14 @@ std::vector Object::getUV() const std::vector tempData; for (std::list::const_iterator it = lModls.begin(); it != lModls.end(); it++) + { + if ((*it)->uv == NULL) + continue; 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 49dac3a..6060d7a 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -286,7 +286,8 @@ void OpenGLController::loadMsh(const char * path) Object obj(path); vfVertices = obj.getVertex(); - vfUV = obj.getUV(); + vfUV = loadUV(); + //vfUV = obj.getUV(); } catch (std::invalid_argument e) { diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp index 05b03c9..bcda1d0 100644 --- a/MshViewer/main.cpp +++ b/MshViewer/main.cpp @@ -9,7 +9,7 @@ int main(int argc, char** argv) { - //goto openGL; + goto openGL; try { Object obj("..\\Release\\Msh\\cube.msh");