Reduce memory consumption in JoinVerticesProcess::ProcessMesh() significantly

kimkulling/add_md5_parser_check_issue-5257^2
ockeymm 2023-09-26 13:20:13 +08:00 committed by Kim Kulling
parent 7a26433846
commit 7a4d8ec412
1 changed files with 29 additions and 29 deletions

View File

@ -145,7 +145,7 @@ bool areVerticesEqual(
} }
template<class XMesh> template<class XMesh>
void updateXMeshVertices(XMesh *pMesh, std::vector<Vertex> &uniqueVertices) { void updateXMeshVertices(XMesh *pMesh, std::vector<int> &uniqueVertices) {
// replace vertex data with the unique data sets // replace vertex data with the unique data sets
pMesh->mNumVertices = (unsigned int)uniqueVertices.size(); pMesh->mNumVertices = (unsigned int)uniqueVertices.size();
@ -157,52 +157,52 @@ void updateXMeshVertices(XMesh *pMesh, std::vector<Vertex> &uniqueVertices) {
// Position, if present (check made for aiAnimMesh) // Position, if present (check made for aiAnimMesh)
if (pMesh->mVertices) { if (pMesh->mVertices) {
delete [] pMesh->mVertices; aiVector3D *oldVertices = pMesh->mVertices;
pMesh->mVertices = new aiVector3D[pMesh->mNumVertices]; pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
for (unsigned int a = 0; a < pMesh->mNumVertices; a++) { for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
pMesh->mVertices[a] = uniqueVertices[a].position; pMesh->mVertices[a] = oldVertices[uniqueVertices[a]];
} delete [] oldVertices;
} }
// Normals, if present // Normals, if present
if (pMesh->mNormals) { if (pMesh->mNormals) {
delete [] pMesh->mNormals; aiVector3D *oldNormals = pMesh->mNormals;
pMesh->mNormals = new aiVector3D[pMesh->mNumVertices]; pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
for( unsigned int a = 0; a < pMesh->mNumVertices; a++) { for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
pMesh->mNormals[a] = uniqueVertices[a].normal; pMesh->mNormals[a] = oldNormals[uniqueVertices[a]];
} delete oldNormals;
} }
// Tangents, if present // Tangents, if present
if (pMesh->mTangents) { if (pMesh->mTangents) {
delete [] pMesh->mTangents; aiVector3D *oldTangents = pMesh->mTangents;
pMesh->mTangents = new aiVector3D[pMesh->mNumVertices]; pMesh->mTangents = new aiVector3D[pMesh->mNumVertices];
for (unsigned int a = 0; a < pMesh->mNumVertices; a++) { for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
pMesh->mTangents[a] = uniqueVertices[a].tangent; pMesh->mTangents[a] = oldTangents[uniqueVertices[a]];
} delete oldTangents;
} }
// Bitangents as well // Bitangents as well
if (pMesh->mBitangents) { if (pMesh->mBitangents) {
delete [] pMesh->mBitangents; aiVector3D *oldBitangents = pMesh->mBitangents;
pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices]; pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices];
for (unsigned int a = 0; a < pMesh->mNumVertices; a++) { for (unsigned int a = 0; a < pMesh->mNumVertices; a++)
pMesh->mBitangents[a] = uniqueVertices[a].bitangent; pMesh->mBitangents[a] = oldBitangents[uniqueVertices[a]];
} delete oldBitangents;
} }
// Vertex colors // Vertex colors
for (unsigned int a = 0; pMesh->HasVertexColors(a); a++) { for (unsigned int a = 0; pMesh->HasVertexColors(a); a++) {
delete [] pMesh->mColors[a]; aiColor4D *oldColors = pMesh->mColors[a];
pMesh->mColors[a] = new aiColor4D[pMesh->mNumVertices]; pMesh->mColors[a] = new aiColor4D[pMesh->mNumVertices];
for( unsigned int b = 0; b < pMesh->mNumVertices; b++) { for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
pMesh->mColors[a][b] = uniqueVertices[b].colors[a]; pMesh->mColors[a][b] = oldColors[uniqueVertices[b]];
} delete [] oldColors;
} }
// Texture coords // Texture coords
for (unsigned int a = 0; pMesh->HasTextureCoords(a); a++) { for (unsigned int a = 0; pMesh->HasTextureCoords(a); a++) {
delete [] pMesh->mTextureCoords[a]; aiVector3D *oldTextureCoords = pMesh->mTextureCoords[a];
pMesh->mTextureCoords[a] = new aiVector3D[pMesh->mNumVertices]; pMesh->mTextureCoords[a] = new aiVector3D[pMesh->mNumVertices];
for (unsigned int b = 0; b < pMesh->mNumVertices; b++) { for (unsigned int b = 0; b < pMesh->mNumVertices; b++)
pMesh->mTextureCoords[a][b] = uniqueVertices[b].texcoords[a]; pMesh->mTextureCoords[a][b] = oldTextureCoords[uniqueVertices[b]];
} delete [] oldTextureCoords;
} }
} }
@ -270,7 +270,7 @@ int JoinVerticesProcess::ProcessMesh( aiMesh* pMesh, unsigned int meshIndex) {
} }
// We'll never have more vertices afterwards. // We'll never have more vertices afterwards.
std::vector<Vertex> uniqueVertices; std::vector<int> uniqueVertices;
uniqueVertices.reserve( pMesh->mNumVertices); uniqueVertices.reserve( pMesh->mNumVertices);
// For each vertex the index of the vertex it was replaced by. // For each vertex the index of the vertex it was replaced by.
@ -311,7 +311,7 @@ int JoinVerticesProcess::ProcessMesh( aiMesh* pMesh, unsigned int meshIndex) {
const bool hasAnimMeshes = pMesh->mNumAnimMeshes > 0; const bool hasAnimMeshes = pMesh->mNumAnimMeshes > 0;
// We'll never have more vertices afterwards. // We'll never have more vertices afterwards.
std::vector<std::vector<Vertex>> uniqueAnimatedVertices; std::vector<std::vector<int>> uniqueAnimatedVertices;
if (hasAnimMeshes) { if (hasAnimMeshes) {
uniqueAnimatedVertices.resize(pMesh->mNumAnimMeshes); uniqueAnimatedVertices.resize(pMesh->mNumAnimMeshes);
for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) { for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
@ -345,10 +345,10 @@ int JoinVerticesProcess::ProcessMesh( aiMesh* pMesh, unsigned int meshIndex) {
//keep track of its index and increment 1 //keep track of its index and increment 1
replaceIndex[a] = newIndex++; replaceIndex[a] = newIndex++;
// add the vertex to the unique vertices // add the vertex to the unique vertices
uniqueVertices.push_back(v); uniqueVertices.push_back(a);
if (hasAnimMeshes) { if (hasAnimMeshes) {
for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) { for (unsigned int animMeshIndex = 0; animMeshIndex < pMesh->mNumAnimMeshes; animMeshIndex++) {
uniqueAnimatedVertices[animMeshIndex].emplace_back(pMesh->mAnimMeshes[animMeshIndex], a); uniqueAnimatedVertices[animMeshIndex].emplace_back(a);
} }
} }
} else{ } else{