diff --git a/code/ObjFileData.h b/code/ObjFileData.h index 69fae0727..9fa3e2473 100644 --- a/code/ObjFileData.h +++ b/code/ObjFileData.h @@ -78,7 +78,7 @@ struct Face //! \param pVertices Pointer to assigned vertex indexbuffer //! \param pNormals Pointer to assigned normals indexbuffer //! \param pTexCoords Pointer to assigned texture indexbuffer - Face(std::vector *pVertices, + Face( std::vector *pVertices, std::vector *pNormals, std::vector *pTexCoords) : m_PrimitiveType( 2 ), @@ -93,18 +93,12 @@ struct Face //! \brief Destructor ~Face() { - if(m_pVertices) { - delete m_pVertices; - m_pVertices = NULL; - } - if(m_pNormals) { - delete m_pNormals; - m_pNormals = NULL; - } - if(m_pTexturCoords) { - delete m_pTexturCoords; - m_pTexturCoords = NULL; - } + delete m_pVertices; + m_pVertices = NULL; + delete m_pNormals; + m_pNormals = NULL; + delete m_pTexturCoords; + m_pTexturCoords = NULL; } }; @@ -205,7 +199,7 @@ struct Mesh unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ]; /// Material index. unsigned int m_uiMaterialIndex; - + /// True, if normals are stored. bool m_hasNormals; /// Constructor Mesh() : diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index 6b702d5dc..ea04c4cf7 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -101,14 +101,15 @@ void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, throw new ImportErrorException( "OBJ-file is too small."); // Allocate buffer and read file into it - m_Buffer.resize( fileSize ); - const size_t readsize = file->Read(&m_Buffer.front(), sizeof(char), fileSize); - assert (readsize == fileSize); + m_Buffer.resize( fileSize + 1 ); + m_Buffer[ fileSize ] = '\0'; + const size_t readsize = file->Read( &m_Buffer.front(), sizeof(char), fileSize ); + assert( readsize == fileSize ); // - std::string strDirectory(1,io.getOsSeparator()), strModelName; - std::string::size_type pos = pFile.find_last_of(io.getOsSeparator()); - if (pos != std::string::npos) + std::string strDirectory( 1, io.getOsSeparator() ), strModelName; + std::string::size_type pos = pFile.find_last_of( io.getOsSeparator() ); + if ( pos != std::string::npos ) { strDirectory = pFile.substr(0, pos); strModelName = pFile.substr(pos+1, pFile.size() - pos - 1); @@ -314,16 +315,8 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel, // Allocate buffer for texture coordinates if ( !pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0] ) { - // FIXME (@Kimmi): cleanup, I don't see the intention behind this - // for ( size_t i=0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++ ) - // { - // const unsigned int num_uv = pObjMesh->m_uiUVCoordinates[ i ]; - // if ( num_uv > 0 ) - // { - pMesh->mNumUVComponents[ 0 ] = 2; - pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ]; - // } - // } + pMesh->mNumUVComponents[ 0 ] = 2; + pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ]; } // Copy vertices, normals and textures into aiMesh instance diff --git a/code/ObjFileMtlImporter.cpp b/code/ObjFileMtlImporter.cpp index bea2c6488..d3f96f859 100644 --- a/code/ObjFileMtlImporter.cpp +++ b/code/ObjFileMtlImporter.cpp @@ -62,7 +62,7 @@ ObjFileMtlImporter::ObjFileMtlImporter( std::vector &buffer, ai_assert( NULL != m_pModel ); if ( NULL == m_pModel->m_pDefaultMaterial ) { - m_pModel->m_pDefaultMaterial = new ObjFile::Material(); + m_pModel->m_pDefaultMaterial = new ObjFile::Material; m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" ); } load(); @@ -220,7 +220,7 @@ void ObjFileMtlImporter::getFloatValue( float &value ) // Creates a material from loaded data. void ObjFileMtlImporter::createMaterial() { - std::string strName; + std::string strName( "" ); m_DataIt = getName( m_DataIt, m_DataItEnd, strName ); if ( m_DataItEnd == m_DataIt ) return; diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index cfe0846da..80dd01360 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -186,7 +186,7 @@ void ObjFileParser::copyNextWord(char *pBuffer, size_t length) { size_t index = 0; m_DataIt = getNextWord(m_DataIt, m_DataItEnd); - while (!isSpace(*m_DataIt) && m_DataIt != m_DataItEnd) + while ( !isSeparator(*m_DataIt) && m_DataIt != m_DataItEnd ) { pBuffer[index] = *m_DataIt; index++; @@ -293,7 +293,7 @@ void ObjFileParser::getFace() } iPos++; } - else if (isSpace(*pPtr)) + else if ( isSeparator(*pPtr) ) { iPos = 0; } @@ -374,7 +374,7 @@ void ObjFileParser::getMaterialDesc() return; char *pStart = &(*m_DataIt); - while ( !isSpace(*m_DataIt) && m_DataIt != m_DataItEnd ) + while ( !isSeparator(*m_DataIt) && m_DataIt != m_DataItEnd ) ++m_DataIt; // Get name @@ -452,7 +452,8 @@ void ObjFileParser::getMaterialLib() // Import material library data from file size_t size = pFile->FileSize(); - std::vector buffer(size); + std::vector buffer( size + 1 ); + buffer[ size ] = '\0'; pFile->Read( &buffer[ 0 ], sizeof( char ), size ); io->Close( pFile ); @@ -471,7 +472,7 @@ void ObjFileParser::getNewMaterial() char *pStart = &(*m_DataIt); std::string strMat(pStart, *m_DataIt); - while (isSpace(*m_DataIt)) + while ( isSeparator( *m_DataIt ) ) m_DataIt++; std::map::iterator it = m_pModel->m_MaterialMap.find( strMat ); if (it == m_pModel->m_MaterialMap.end()) @@ -514,12 +515,12 @@ void ObjFileParser::getGroupName() // Get next word from data buffer m_DataIt = getNextToken(m_DataIt, m_DataItEnd); m_DataIt = getNextWord(m_DataIt, m_DataItEnd); - if ( m_DataIt == m_DataItEnd ) + if ( isEndOfBuffer( m_DataIt, m_DataItEnd ) ) return; // Store groupname in group library char *pStart = &(*m_DataIt); - while (!isSpace(*m_DataIt)) + while ( !isSeparator(*m_DataIt) ) m_DataIt++; std::string strGroupName(pStart, &(*m_DataIt)); @@ -564,7 +565,7 @@ void ObjFileParser::getObjectName() if (m_DataIt == m_DataItEnd) return; char *pStart = &(*m_DataIt); - while (!isSpace(*m_DataIt)) + while (!isSeparator(*m_DataIt)) m_DataIt++; std::string strObjectName(pStart, &(*m_DataIt)); diff --git a/code/ObjTools.h b/code/ObjTools.h index 66c08b577..ac8c6f275 100644 --- a/code/ObjTools.h +++ b/code/ObjTools.h @@ -49,11 +49,23 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace Assimp { +/** @brief Returns true, if the last entry of the buffer is reached. + * @param it Iterator of current position. + * @param end Iterator with end of buffer. + * @return true, if the end of the buffer is reached. + */ +template +inline bool isEndOfBuffer( char_t it, char_t end ) +{ + end--; + return ( it == end ); +} + /** @brief Returns true, if token is a space on any supported platform * @param token Token to search in * @return true, if token is a space */ -inline bool isSpace( char token ) +inline bool isSeparator( char token ) { return ( token == ' ' || token == '\n' || @@ -79,9 +91,9 @@ inline bool isNewLine( char token ) template inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd ) { - while (pBuffer != pEnd) + while ( !isEndOfBuffer( pBuffer, pEnd ) ) { - if (!isSpace(*pBuffer)) + if ( !isSeparator( *pBuffer ) ) break; pBuffer++; } @@ -96,9 +108,9 @@ inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd ) template inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd ) { - while (pBuffer != pEnd) + while ( !isEndOfBuffer( pBuffer, pEnd ) ) { - if ( isSpace( *pBuffer ) ) + if ( isSeparator( *pBuffer ) ) break; pBuffer++; } @@ -114,14 +126,14 @@ inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd ) template inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine ) { - while ( it != end && *it != '\n' ) + while ( !isEndOfBuffer( it, end ) && *it != '\n' ) ++it; if ( it != end ) { ++it; ++uiLine; } - /* fix .. from time to time there are spaces at the beginning of a material line */ + // fix .. from time to time there are spaces at the beginning of a material line while ( it != end && (*it == '\t' || *it == ' ') ) ++it; return it; @@ -138,15 +150,15 @@ inline char_t getName( char_t it, char_t end, std::string &name ) { name = ""; it = getNextToken( it, end ); - if ( it == end ) + if ( isEndOfBuffer( it, end ) ) return end; - char *pStart = &(*it); - while ( !isSpace(*it) && it != end ) + char *pStart = &( *it ); + while ( !isEndOfBuffer( it, end ) && !isSeparator( *it ) ) ++it; // Get name - std::string strName(pStart, &(*it)); + std::string strName( pStart, &(*it) ); if ( strName.empty() ) return it; else @@ -167,7 +179,7 @@ inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length { size_t index = 0; it = getNextWord( it, end ); - while (!isSpace( *it ) && it != end ) + while ( !isSeparator( *it ) && !isEndOfBuffer( it, end ) ) { pBuffer[index] = *it ; index++; @@ -175,7 +187,7 @@ inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length break; ++it; } - pBuffer[index] = '\0'; + pBuffer[ index ] = '\0'; return it; } @@ -190,7 +202,6 @@ inline char_t getFloat( char_t it, char_t end, float &value ) { static const size_t BUFFERSIZE = 1024; char buffer[ BUFFERSIZE ]; - //memset( buffer, '\0', BUFFERSIZE ); it = CopyNextWord( it, end, buffer, BUFFERSIZE ); value = (float) fast_atof( buffer );