diff --git a/CREDITS b/CREDITS index a09bad345..febb4c29e 100644 --- a/CREDITS +++ b/CREDITS @@ -108,4 +108,7 @@ Contributes a Android-specific build issue: log the hardware architecture for AR Contributes a obj-fileparser fix: missing tokens in the obj-token list. - Roman Kharitonov -Contributes a fix for the configure script environment. \ No newline at end of file +Contributes a fix for the configure script environment. + +-rdb +Contributes a bundle of fixes and improvments for the bsp-importer. diff --git a/code/Q3BSPFileImporter.cpp b/code/Q3BSPFileImporter.cpp index 32ec5c3b7..a4731f2a9 100644 --- a/code/Q3BSPFileImporter.cpp +++ b/code/Q3BSPFileImporter.cpp @@ -93,7 +93,7 @@ static void extractIds( const std::string &rKey, int &rId1, int &rId2 ) } // ------------------------------------------------------------------------------------------------ -// Local helper fuction to normalize filenames. +// Local helper function to normalize filenames. static void normalizePathName( const std::string &rPath, std::string &rNormalizedPath ) { rNormalizedPath = ""; @@ -649,9 +649,8 @@ bool Q3BSPFileImporter::importTextureFromArchive( const Q3BSP::Q3BSPModel *pMode if ( NULL == pTexture ) return false; - std::string textureName = pTexture->strName; - textureName += ".jpg"; - if ( pArchive->Exists( textureName.c_str() ) ) + std::string textureName, ext; + if ( expandFile( pArchive, pTexture->strName, supportedExtensions, textureName, ext ) ) { IOStream *pTextureStream = pArchive->Open( textureName.c_str() ); if ( NULL != pTextureStream ) @@ -679,6 +678,15 @@ bool Q3BSPFileImporter::importTextureFromArchive( const Q3BSP::Q3BSPModel *pMode pMatHelper->AddProperty( &name, AI_MATKEY_TEXTURE_DIFFUSE( 0 ) ); mTextures.push_back( pTexture ); } + else + { + // If it doesn't exist in the archive, it is probably just a reference to an external file. + // We'll leave it up to the user to figure out which extension the file has. + aiString name; + strncpy( name.data, pTexture->strName, sizeof name.data ); + name.length = strlen( name.data ); + pMatHelper->AddProperty( &name, AI_MATKEY_TEXTURE_DIFFUSE( 0 ) ); + } } return res; @@ -706,19 +714,21 @@ bool Q3BSPFileImporter::importLightmap( const Q3BSP::Q3BSPModel *pModel, aiScene } aiTexture *pTexture = new aiTexture; - pTexture->mHeight = 0; - pTexture->mWidth = CE_BSP_LIGHTMAPWIDTH * CE_BSP_LIGHTMAPHEIGHT; - unsigned char *pData = new unsigned char[ pTexture->mWidth ]; - pTexture->pcData = reinterpret_cast( pData ); + pTexture->mWidth = CE_BSP_LIGHTMAPWIDTH; + pTexture->mHeight = CE_BSP_LIGHTMAPHEIGHT; + pTexture->pcData = new aiTexel[CE_BSP_LIGHTMAPWIDTH * CE_BSP_LIGHTMAPHEIGHT]; + + ::memcpy( pTexture->pcData, pLightMap->bLMapData, pTexture->mWidth ); + size_t p = 0; + for ( size_t i = 0; i < CE_BSP_LIGHTMAPWIDTH * CE_BSP_LIGHTMAPHEIGHT; ++i ) + { + pTexture->pcData[ i ].r = pLightMap->bLMapData[ p++ ]; + pTexture->pcData[ i ].g = pLightMap->bLMapData[ p++ ]; + pTexture->pcData[ i ].b = pLightMap->bLMapData[ p++ ]; + pTexture->pcData[ i ].a = 0xFF; + } - pTexture->achFormatHint[ 0 ] = 'b'; - pTexture->achFormatHint[ 1 ] = 'm'; - pTexture->achFormatHint[ 2 ] = 'p'; - pTexture->achFormatHint[ 3 ] = '\0'; - - memcpy( pTexture->pcData, pLightMap->bLMapData, pTexture->mWidth ); - aiString name; name.data[ 0 ] = '*'; name.length = 1 + ASSIMP_itoa10( name.data + 1, MAXLEN-1, mTextures.size() ); @@ -729,6 +739,38 @@ bool Q3BSPFileImporter::importLightmap( const Q3BSP::Q3BSPModel *pModel, aiScene return true; } +// ------------------------------------------------------------------------------------------------ +// Will search for a supported extension. +bool Q3BSPFileImporter::expandFile( Q3BSP::Q3BSPZipArchive *pArchive, const std::string &rFilename, + const std::vector &rExtList, std::string &rFile, + std::string &rExt ) +{ + ai_assert( NULL != pArchive ); + ai_assert( !rFilename.empty() ); + + if ( rExtList.empty() ) + { + rFile = rFilename; + rExt = ""; + return true; + } + + bool found = false; + for ( std::vector::const_iterator it = rExtList.begin(); it != rExtList.end(); ++it ) + { + const std::string textureName = rFilename + *it; + if ( pArchive->Exists( textureName.c_str() ) ) + { + rExt = *it; + rFile = textureName; + found = true; + break; + } + } + + return found; +} + // ------------------------------------------------------------------------------------------------ } // Namespace Assimp diff --git a/code/Q3BSPFileImporter.h b/code/Q3BSPFileImporter.h index 788ceb083..5ddab7939 100644 --- a/code/Q3BSPFileImporter.h +++ b/code/Q3BSPFileImporter.h @@ -54,9 +54,10 @@ struct Q3BSPModel; struct sQ3BSPFace; } - +// ------------------------------------------------------------------------------------------------ /** Loader to import BSP-levels from a PK3 archive or from a unpacked BSP-level. */ +// ------------------------------------------------------------------------------------------------ class Q3BSPFileImporter : BaseImporter { friend class Importer; @@ -97,6 +98,8 @@ private: bool importTextureFromArchive( const Q3BSP::Q3BSPModel *pModel, Q3BSP::Q3BSPZipArchive *pArchive, aiScene* pScene, Assimp::MaterialHelper *pMatHelper, int textureId ); bool importLightmap( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, Assimp::MaterialHelper *pMatHelper, int lightmapId ); + bool expandFile( Q3BSP::Q3BSPZipArchive *pArchive, const std::string &rFilename, const std::vector &rExtList, + std::string &rFile, std::string &rExt ); private: aiMesh *m_pCurrentMesh; @@ -105,6 +108,9 @@ private: std::vector mTextures; }; +// ------------------------------------------------------------------------------------------------ + } // Namespace Assimp + #endif // ASSIMP_Q3BSPFILEIMPORTER_H_INC