From 57df0a2e1525d0f1603aecf03a6bec609f7e838b Mon Sep 17 00:00:00 2001 From: Anakin Date: Sun, 20 Nov 2016 12:26:23 +0100 Subject: [PATCH] use different variables to store the data. The aim is to handle even not triangulated mesh files. At the moment there is a problem with the UV using the new method --- MshViewer/Header/Object.h | 5 ++- MshViewer/Source/Object.cpp | 40 +++++++---------- MshViewer/Source/OpenGlController.cpp | 62 ++++++++++++++++++++++++++- MshViewer/main.cpp | 2 +- 4 files changed, 80 insertions(+), 29 deletions(-) diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h index 4eece53..66a0860 100644 --- a/MshViewer/Header/Object.h +++ b/MshViewer/Header/Object.h @@ -24,8 +24,9 @@ struct Segment { std::uint32_t textureIndex = 0; float* vertex = nullptr; float* uv = nullptr; - std::uint32_t* mesh = nullptr; - std::uint32_t meshSize = 0; + std::uint32_t* mesh = nullptr; // not used when using vector + std::uint32_t meshSize = 0; //not used when using vector + std::vector*> meshIndices; // indices into vertex array }; struct Modl { diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp index 1642a43..743d5f4 100644 --- a/MshViewer/Source/Object.cpp +++ b/MshViewer/Source/Object.cpp @@ -373,23 +373,6 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& for (std::list::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 - * / - continue; - }*/ - if (!strcmp("MATI", (*it)->name)) { fsMesh.seekg((*it)->position); @@ -418,7 +401,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& continue; } - if (!strcmp("STRP", (*it)->name)) + /*if (!strcmp("STRP", (*it)->name)) { // jump to the data section and read the size; fsMesh.seekg((*it)->position); @@ -452,15 +435,19 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& } continue; - } + }*/ ///////////////////////////////////////////////////////////////////////////////////////// /////DEV ZONE if (!strcmp("STRP", (*it)->name)) { - // this needs to be a member - std::vector*> tempVector; + // don't get null, bone, shadowMesh and hidden mesh indices + if (dataDestination->type == null || + dataDestination->type == bone || + dataDestination->type == shadowMesh || + dataDestination->renderFlags == 1) + continue; // jump to the data section and read the size; fsMesh.seekg((*it)->position); @@ -484,6 +471,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& tempPoly->push_back((std::uint32_t)tempValue); + // new Polygon found if (highBitCount == 2) { // reset highBitCount @@ -493,7 +481,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& std::uint32_t saveData[2]; for (int i = 0; i < 2; i++) { - saveData[0] = tempPoly->back(); + saveData[i] = tempPoly->back(); tempPoly->pop_back(); } @@ -503,13 +491,17 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list& newPointer->push_back(saveData[i]); // save the old vector and set the pointer to the new one - tempVector.push_back(tempPoly); + tempData->meshIndices.push_back(tempPoly); tempPoly = newPointer; } // if high bit set } // for all values + // save the last values (where no 2 high bits follow); - tempVector.push_back(tempPoly); + tempData->meshIndices.push_back(tempPoly); + + // kick the first element, it's empty as a reason of the algo above; + tempData->meshIndices.erase(tempData->meshIndices.begin()); continue; } diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index 9750e26..74871d8 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -120,8 +120,17 @@ void OpenGLController::deleteVectors() cursor->segmLst.pop_back(); delete segmCuror->uv; - delete segmCuror->mesh; + delete segmCuror->mesh; // not used when using vector delete segmCuror->vertex; + + while (!segmCuror->meshIndices.empty()) + { + std::vector* meshCursor = segmCuror->meshIndices.back(); + meshCursor->clear(); + segmCuror->meshIndices.pop_back(); + delete meshCursor; + } + delete segmCuror; } @@ -368,7 +377,7 @@ void OpenGLController::loadMsh(const char * path) // collect vertex data of all models std::vector tempBufferData; - for (auto& modIt : vModels) + /*for (auto& modIt : vModels) { // don't draw bones, nulls, shadow mesh and hidden things if (modIt->type == null || modIt->type == bone || modIt->type == shadowMesh || modIt->renderFlags == 1) @@ -397,7 +406,56 @@ void OpenGLController::loadMsh(const char * path) tempBufferData.push_back(tempVertex); } } + }*/ + +///////////////////////////////////////////////////////////////////////////////////////// +/////DEV ZONE + for (auto& modIt : vModels) + { + for (auto& segIt : modIt->segmLst) + { + for (auto& mshIt : segIt->meshIndices) + { + if (mshIt->size() == 3) // already triangulated + { + // for every edge of the polygon (in that case 3) + for (int i = 0; i < 3; i++) + { + Vertex tempVertex; + // every edge has 3 coordinates + for (int j = 0; j < 3; j++) + tempVertex.position[j] = (GLfloat)segIt->vertex[(*mshIt)[i] * 3 + j]; + + // and 2 UV + if (segIt->uv == NULL) + { + tempVertex.uv[0] = 1.0; + tempVertex.uv[1] = 1.0; + } + else + { + for (int j = 0; j < 2; j++) + tempVertex.uv[j] = (GLfloat)segIt->uv[(*mshIt)[i] * 3 + j]; + } + tempBufferData.push_back(tempVertex); + } + } + else if (mshIt->size() > 3) // needs to be triangulated + { + //triangulate; + MessageBox(NULL, "Ooops!", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); + } + else // this shouldn't happen (polygon with less then 3 vertex) + { + deleteVectors(); + MessageBox(NULL, "You have polygons with less then 3 vertices!", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); + return; + } + } + } } + // think of delete the data from obj here. It's no longer needed +//////////////////////////////////////////////////////////////////////////////////////// // fill the buffer glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp index 4a166c9..c5c2666 100644 --- a/MshViewer/main.cpp +++ b/MshViewer/main.cpp @@ -17,7 +17,7 @@ int main(int argc, char** argv) else scene = OpenGLController::getInstance(); - scene->loadMsh("..\\Release\\Msh\\cluster.msh"); + scene->loadMsh("..\\Release\\Msh\\cubeTex.msh"); do { scene->updateScene();