From 49c9786b0a9446cb2ce726930736ef6b5ad2dc4e Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 12 Sep 2014 15:40:50 +0200 Subject: [PATCH] update: avoid mesh generation on heap, when the mesh will be empty ( obj-loader ). Signed-off-by: Kim Kulling --- code/ObjFileImporter.cpp | 34 ++++++++++++++-------------------- code/ObjFileImporter.h | 4 ++-- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index 254ef05db..874dda707 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -230,16 +230,10 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile for ( unsigned int i=0; i< pObject->m_Meshes.size(); i++ ) { unsigned int meshId = pObject->m_Meshes[ i ]; - aiMesh *pMesh = new aiMesh; - createTopology( pModel, pObject, meshId, pMesh ); - if ( pMesh->mNumVertices > 0 ) - { + aiMesh *pMesh = createTopology( pModel, pObject, meshId ); + if( pMesh && pMesh->mNumFaces > 0 ) { MeshArray.push_back( pMesh ); } - else - { - delete pMesh; - } } // Create all nodes from the sub-objects stored in the current object @@ -272,22 +266,22 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile // ------------------------------------------------------------------------------------------------ // Create topology data -void ObjFileImporter::createTopology(const ObjFile::Model* pModel, - const ObjFile::Object* pData, - unsigned int uiMeshIndex, - aiMesh* pMesh ) +aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const ObjFile::Object* pData, + unsigned int uiMeshIndex ) { // Checking preconditions ai_assert( NULL != pModel ); if( NULL == pData ) { - return; + return NULL; } // Create faces ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ]; - ai_assert( NULL != pObjMesh ); - - pMesh->mNumFaces = 0; + if( !pObjMesh ) { + return NULL; + } + ai_assert( NULL != pObjMesh ); + aiMesh* pMesh = new aiMesh; for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) { ObjFile::Face* const inp = pObjMesh->m_Faces[ index ]; @@ -295,16 +289,14 @@ void ObjFileImporter::createTopology(const ObjFile::Model* pModel, if (inp->m_PrimitiveType == aiPrimitiveType_LINE) { pMesh->mNumFaces += inp->m_pVertices->size() - 1; pMesh->mPrimitiveTypes |= aiPrimitiveType_LINE; - } - else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) { + } else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) { pMesh->mNumFaces += inp->m_pVertices->size(); pMesh->mPrimitiveTypes |= aiPrimitiveType_POINT; } else { ++pMesh->mNumFaces; if (inp->m_pVertices->size() > 3) { pMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON; - } - else { + } else { pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE; } } @@ -353,6 +345,8 @@ void ObjFileImporter::createTopology(const ObjFile::Model* pModel, // Create mesh vertices createVertexArray(pModel, pData, uiMeshIndex, pMesh, uiIdxCount); + + return pMesh; } // ------------------------------------------------------------------------------------------------ diff --git a/code/ObjFileImporter.h b/code/ObjFileImporter.h index 814a65652..716cf65d1 100644 --- a/code/ObjFileImporter.h +++ b/code/ObjFileImporter.h @@ -91,8 +91,8 @@ private: aiNode *pParent, aiScene* pScene, std::vector &MeshArray); //! \brief Creates topology data like faces and meshes for the geometry. - void createTopology(const ObjFile::Model* pModel, const ObjFile::Object* pData, - unsigned int uiMeshIndex, aiMesh* pMesh); + aiMesh *createTopology( const ObjFile::Model* pModel, const ObjFile::Object* pData, + unsigned int uiMeshIndex ); //! \brief Creates vertices from model. void createVertexArray(const ObjFile::Model* pModel, const ObjFile::Object* pCurrentObject,