From e138a02dd5f31c7cc602b6f2e3edcd4ffa51975b Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 9 Jul 2015 20:15:44 +0200 Subject: [PATCH] Obj: fix issue 121 - set group names as the mesh names. --- code/ObjFileData.h | 19 ++++++++++--------- code/ObjFileImporter.cpp | 13 +++++++------ code/ObjFileParser.cpp | 12 ++++++------ code/ObjFileParser.h | 4 ++-- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/code/ObjFileData.h b/code/ObjFileData.h index faa54553b..36c694c5a 100644 --- a/code/ObjFileData.h +++ b/code/ObjFileData.h @@ -219,10 +219,10 @@ struct Material // ------------------------------------------------------------------------------------------------ //! \struct Mesh //! \brief Data structure to store a mesh -struct Mesh -{ +struct Mesh { static const unsigned int NoMaterial = ~0u; - + /// The name for the mesh + std::string m_name; /// Array with pointer to all stored faces std::vector m_Faces; /// Assigned material @@ -235,13 +235,14 @@ struct Mesh unsigned int m_uiMaterialIndex; /// True, if normals are stored. bool m_hasNormals; + /// Constructor - Mesh() : - m_pMaterial(NULL), - m_uiNumIndices(0), - m_uiMaterialIndex( NoMaterial ), - m_hasNormals(false) - { + Mesh( const std::string &name ) + : m_name( name ) + , m_pMaterial(NULL) + , m_uiNumIndices(0) + , m_uiMaterialIndex( NoMaterial ) + , m_hasNormals(false) { memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS); } diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index 72f045d11..48135ca95 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -289,6 +289,10 @@ aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const Obj } ai_assert( NULL != pObjMesh ); aiMesh* pMesh = new aiMesh; + if( !pObjMesh->m_name.empty() ) { + pMesh->mName.Set( pObjMesh->m_name ); + } + for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) { ObjFile::Face *const inp = pObjMesh->m_Faces[ index ]; @@ -311,19 +315,16 @@ aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const Obj } unsigned int uiIdxCount( 0u ); - if ( pMesh->mNumFaces > 0 ) - { + if ( pMesh->mNumFaces > 0 ) { pMesh->mFaces = new aiFace[ pMesh->mNumFaces ]; - if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial ) - { + if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial ) { pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex; } unsigned int outIndex( 0 ); // Copy all data from all stored meshes - for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) - { + for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) { ObjFile::Face* const inp = pObjMesh->m_Faces[ index ]; if (inp->m_PrimitiveType == aiPrimitiveType_LINE) { for(size_t i = 0; i < inp->m_pVertices->size() - 1; ++i) { diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index 6f9170e8c..e4927feae 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -440,7 +440,7 @@ void ObjFileParser::getFace(aiPrimitiveType type) // Assign face to mesh if ( NULL == m_pModel->m_pCurrentMesh ) { - createMesh(); + createMesh( "defaultobject" ); } // Store the face @@ -496,7 +496,7 @@ void ObjFileParser::getMaterialDesc() m_pModel->m_pCurrentMaterial = (*it).second; if ( needsNewMesh( strName )) { - createMesh(); + createMesh( strName ); } m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strName ); } @@ -585,7 +585,7 @@ void ObjFileParser::getNewMaterial() // Set new material if ( needsNewMesh( strMat ) ) { - createMesh(); + createMesh( strMat ); } m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strMat ); } @@ -714,7 +714,7 @@ void ObjFileParser::createObject(const std::string &objName) m_pModel->m_pCurrent->m_strObjName = objName; m_pModel->m_Objects.push_back( m_pModel->m_pCurrent ); - createMesh(); + createMesh( objName ); if( m_pModel->m_pCurrentMaterial ) { @@ -725,10 +725,10 @@ void ObjFileParser::createObject(const std::string &objName) } // ------------------------------------------------------------------- // Creates a new mesh -void ObjFileParser::createMesh() +void ObjFileParser::createMesh( const std::string &meshName ) { ai_assert( NULL != m_pModel ); - m_pModel->m_pCurrentMesh = new ObjFile::Mesh; + m_pModel->m_pCurrentMesh = new ObjFile::Mesh( meshName ); m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh ); unsigned int meshId = m_pModel->m_Meshes.size()-1; if ( NULL != m_pModel->m_pCurrent ) diff --git a/code/ObjFileParser.h b/code/ObjFileParser.h index 43afcfe7b..b7e69876f 100644 --- a/code/ObjFileParser.h +++ b/code/ObjFileParser.h @@ -113,9 +113,9 @@ private: /// Parse object name void getObjectName(); /// Creates a new object. - void createObject(const std::string &strObjectName); + void createObject( const std::string &strObjectName ); /// Creates a new mesh. - void createMesh(); + void createMesh( const std::string &meshName ); /// Returns true, if a new mesh instance must be created. bool needsNewMesh( const std::string &rMaterialName ); /// Error report in token