fixed the memory garbage problem

This commit is contained in:
Anakin 2016-11-26 15:39:59 +01:00
parent 5c5b9ac2f1
commit 5ab2f2eaf9
3 changed files with 24 additions and 20 deletions

View File

@ -24,7 +24,7 @@ struct Segment {
std::uint32_t textureIndex = 0; std::uint32_t textureIndex = 0;
float* vertex = nullptr; float* vertex = nullptr;
float* uv = nullptr; float* uv = nullptr;
std::vector<std::vector<std::uint32_t>*> meshIndices; // indices into vertex array std::vector<std::vector<std::uint32_t>> meshIndices; // indices into vertex array
}; };
struct Modl { struct Modl {

View File

@ -434,7 +434,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize)); fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
int highBitCount(0); int highBitCount(0);
std::vector<uint32_t>* tempPoly = new std::vector<uint32_t>; std::vector<uint32_t> tempPoly; // = new std::vector<uint32_t>;
for (unsigned int i = 0; i < tempSize; i++) for (unsigned int i = 0; i < tempSize; i++)
{ {
@ -449,7 +449,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
tempValue = (std::uint16_t(tempValue << 1) >> 1); tempValue = (std::uint16_t(tempValue << 1) >> 1);
} }
tempPoly->push_back((std::uint32_t)tempValue); tempPoly.push_back((std::uint32_t)tempValue);
// new Polygon found // new Polygon found
if (highBitCount == 2) if (highBitCount == 2)
@ -461,18 +461,20 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
std::uint32_t saveData[2]; std::uint32_t saveData[2];
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
saveData[i] = tempPoly->back(); saveData[i] = tempPoly.back();
tempPoly->pop_back(); tempPoly.pop_back();
} }
// ..and save them in the new vector // ..and save them in the new vector
std::vector<uint32_t>* newPointer = new std::vector<uint32_t>; tempData->meshIndices.push_back(tempPoly);
tempPoly.clear();
for (int i = 1; i >= 0; i--) for (int i = 1; i >= 0; i--)
newPointer->push_back(saveData[i]); tempPoly.push_back(saveData[i]);
// save the old vector and set the pointer to the new one // save the old vector and set the pointer to the new one
tempData->meshIndices.push_back(tempPoly); //tempData->meshIndices.push_back(tempPoly);
tempPoly = newPointer; //tempPoly = newPointer;
} // if high bit set } // if high bit set
} // for all values } // for all values
@ -542,19 +544,19 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
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;
// for every triangle.. // for every triangle..
for (unsigned int i = 0; i < tempSize; i += 3) for (unsigned int i = 0; i < tempSize; i += 3)
{ {
tempPoly = new std::vector<uint32_t>; tempPoly.clear();
// ..get the 3 indices and save them // ..get the 3 indices and save them
for (int j = 0; j < 3; j++) for (int j = 0; j < 3; j++)
{ {
std::uint32_t tempValue; std::uint32_t tempValue;
fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint32_t)); fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint32_t));
tempPoly->push_back(tempValue); tempPoly.push_back(tempValue);
} }
tempData->meshIndices.push_back(tempPoly); tempData->meshIndices.push_back(tempPoly);
} }

View File

@ -126,10 +126,10 @@ void OpenGLController::deleteVectors()
while (!segmCuror->meshIndices.empty()) while (!segmCuror->meshIndices.empty())
{ {
std::vector<std::uint32_t>* meshCursor = segmCuror->meshIndices.back(); while (!segmCuror->meshIndices.back().empty())
meshCursor->clear(); segmCuror->meshIndices.back().pop_back();
segmCuror->meshIndices.pop_back(); segmCuror->meshIndices.pop_back();
delete meshCursor;
} }
delete segmCuror; delete segmCuror;
@ -350,7 +350,7 @@ void OpenGLController::updateScene()
// calculate the number of vertex // calculate the number of vertex
unsigned int vertexCount(0); unsigned int vertexCount(0);
for (auto& it : segIt->meshIndices) for (auto& it : segIt->meshIndices)
vertexCount += (it->size() - 2) * 3; vertexCount += (it.size() - 2) * 3;
glDrawArrays(GL_TRIANGLES, instanceOffset, vertexCount); glDrawArrays(GL_TRIANGLES, instanceOffset, vertexCount);
@ -393,10 +393,10 @@ void OpenGLController::loadMsh(const char * path)
{ {
for (auto& mshIt : segIt->meshIndices) // for every polygon for (auto& mshIt : segIt->meshIndices) // for every polygon
{ {
if (mshIt->size() >= 3) // multipoly if (mshIt.size() >= 3) // multipoly
{ {
// for every triangle of the multi polygon // for every triangle of the multi polygon
for (unsigned int tri = 0; tri < mshIt->size() - 2; tri++) for (unsigned int tri = 0; tri < mshIt.size() - 2; tri++)
{ {
// for every edge of the triangle // for every edge of the triangle
for (int triEdge = 0; triEdge < 3; triEdge++) for (int triEdge = 0; triEdge < 3; triEdge++)
@ -404,7 +404,7 @@ void OpenGLController::loadMsh(const char * path)
Vertex tempVertex; Vertex tempVertex;
// every edge has 3 coordinates // every edge has 3 coordinates
for (int j = 0; j < 3; j++) for (int j = 0; j < 3; j++)
tempVertex.position[j] = (GLfloat)segIt->vertex[(*mshIt)[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j]; tempVertex.position[j] = (GLfloat)segIt->vertex[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j];
// and 2 UV // and 2 UV
if (segIt->uv == NULL) if (segIt->uv == NULL)
@ -415,7 +415,7 @@ void OpenGLController::loadMsh(const char * path)
else else
{ {
for (int j = 0; j < 2; j++) for (int j = 0; j < 2; j++)
tempVertex.uv[j] = (GLfloat)segIt->uv[(*mshIt)[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j]; tempVertex.uv[j] = (GLfloat)segIt->uv[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j];
} }
tempBufferData.push_back(tempVertex); tempBufferData.push_back(tempVertex);
} }
@ -441,6 +441,8 @@ void OpenGLController::loadMsh(const char * path)
); );
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
tempBufferData.clear();
// get textures path // get textures path
std::string tempPath = path; std::string tempPath = path;