Browse Source

- UPDATE : Add first version of bsp-loader with assimp-internal texture support.
- UPDATE : Actualize docu.

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@835 67173fc5-114c-0410-ac8e-9d2fd5bffc1f

kimmi 15 years ago
parent
commit
5ef9e63ac6
5 changed files with 169 additions and 20 deletions
  1. 3 0
      code/Q3BSPFileData.h
  2. 138 17
      code/Q3BSPFileImporter.cpp
  3. 6 2
      code/Q3BSPFileImporter.h
  4. 5 1
      code/Q3BSPFileParser.cpp
  5. 17 0
      code/Q3BSPZipArchive.cpp

+ 3 - 0
code/Q3BSPFileData.h

@@ -47,6 +47,9 @@ namespace Assimp
 namespace Q3BSP
 {
 
+static const unsigned int CE_BSP_LIGHTMAPWIDTH = 128;
+static const unsigned int CE_BSP_LIGHTMAPHEIGHT = 128;
+
 static const unsigned int CE_BSP_LIGHTMAPSIZE = 128*128*3;	///< = 128( width ) * 128 ( height ) * 3 ( channels / RGB ).
 static const int VERION_Q3LEVEL = 46;						///< Supported version.
 

+ 138 - 17
code/Q3BSPFileImporter.cpp

@@ -93,6 +93,7 @@ static void extractIds( const std::string &rKey, int &rId1, int &rId2 )
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Local helper fuction to normalize filenames.
 static void normalizePathName( const std::string &rPath, std::string &rNormalizedPath )
 {
 	rNormalizedPath = "";
@@ -114,7 +115,7 @@ static void normalizePathName( const std::string &rPath, std::string &rNormalize
 		{
 			if ( rNormalizedPath[j] == delimiters[ i ] )
 			{
-				rNormalizedPath[ j ] = sep[0];
+				rNormalizedPath[ j ] = sep[ 0 ];
 			}
 		}
 	}
@@ -125,7 +126,8 @@ static void normalizePathName( const std::string &rPath, std::string &rNormalize
 Q3BSPFileImporter::Q3BSPFileImporter() :
 	m_pCurrentMesh( NULL ),
 	m_pCurrentFace( NULL ),
-	m_MaterialLookupMap()
+	m_MaterialLookupMap(),
+	mTextures()
 {
 	// empty
 }
@@ -200,7 +202,7 @@ void Q3BSPFileImporter::InternReadFile(const std::string &rFile, aiScene* pScene
 	Q3BSPModel *pBSPModel = fileParser.getModel();
 	if ( NULL != pBSPModel )
 	{
-		CreateDataFromImport( pBSPModel, pScene );
+		CreateDataFromImport( pBSPModel, pScene, &Archive );
 	}
 }
 
@@ -255,7 +257,8 @@ bool Q3BSPFileImporter::findFirstMapInArchive( Q3BSPZipArchive &rArchive, std::s
 
 // ------------------------------------------------------------------------------------------------
 //	Creates the assimp specific data.
-void Q3BSPFileImporter::CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene )
+void Q3BSPFileImporter::CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, 
+											 Q3BSPZipArchive *pArchive )
 {
 	if ( NULL == pModel || NULL == pScene )
 		return;
@@ -273,7 +276,7 @@ void Q3BSPFileImporter::CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, a
 	CreateNodes( pModel, pScene, pScene->mRootNode );
 	
 	// Create the assigned materials
-	createMaterials( pModel, pScene );
+	createMaterials( pModel, pScene, pArchive );
 }
 
 // ------------------------------------------------------------------------------------------------
@@ -283,7 +286,9 @@ void Q3BSPFileImporter::CreateNodes( const Q3BSP::Q3BSPModel *pModel, aiScene* p
 {
 	ai_assert( NULL != pModel );
 	if ( NULL == pModel )
+	{
 		return;
+	}
 
 	unsigned int matIdx = 0;
 	std::vector<aiMesh*> MeshArray;
@@ -343,11 +348,15 @@ aiNode *Q3BSPFileImporter::CreateTopology( const Q3BSP::Q3BSPModel *pModel,
 {
 	size_t numVerts = countData( rArray );
 	if ( 0 == numVerts )
+	{
 		return NULL;
+	}
 	
 	size_t numFaces = countFaces( rArray );
 	if ( 0 == numFaces )
+	{
 		return NULL;
+	}
 
 	size_t numTriangles = countTriangles( rArray );
 	pMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
@@ -404,7 +413,9 @@ void Q3BSPFileImporter::createTriangleTopology( const Q3BSP::Q3BSPModel *pModel,
 	m_pCurrentFace = getNextFace( pMesh, rFaceIdx );
 	ai_assert( NULL != m_pCurrentFace );
 	if ( NULL == m_pCurrentFace )
+	{
 		return;
+	}
 
 	m_pCurrentFace->mNumIndices = 3;
 	m_pCurrentFace->mIndices = new unsigned int[ m_pCurrentFace->mNumIndices ];
@@ -452,13 +463,18 @@ void Q3BSPFileImporter::createTriangleTopology( const Q3BSP::Q3BSPModel *pModel,
 
 // ------------------------------------------------------------------------------------------------
 //	Creates all referenced materials.
-void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene )
+void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene,
+										Q3BSPZipArchive *pArchive )
 {
 	if ( m_MaterialLookupMap.empty() )
+	{
 		return;
+	}
 
 	pScene->mMaterials = new aiMaterial*[ m_MaterialLookupMap.size() ];
-
+	size_t texIdx( 0 );
+	aiString aiMatName;
+	int textureId( -1 ), lightmapId( -1 );
 	for ( FaceMapIt it = m_MaterialLookupMap.begin(); it != m_MaterialLookupMap.end();
 		++it )
 	{
@@ -468,12 +484,10 @@ void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScen
 			continue;
 		}
 
-		aiString aiMatName;
 		aiMatName.Set( matName );
 		Assimp::MaterialHelper *pMatHelper = new Assimp::MaterialHelper;
 		pMatHelper->AddProperty( &aiMatName, AI_MATKEY_NAME );
 
-		int textureId, lightmapId;
 		extractIds( matName, textureId, lightmapId );
 		
 		// Adding the texture
@@ -482,25 +496,31 @@ void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScen
 			sQ3BSPTexture *pTexture = pModel->m_Textures[ textureId ];
 			if ( NULL != pTexture )
 			{
-				std::string tmp( pTexture->strName ), texName( "" );
+				std::string tmp( "*" ), texName( "" );
+				tmp += pTexture->strName;
 				tmp += ".jpg";
 				normalizePathName( tmp, texName );
-				aiString textureName(  texName.c_str() );
-				pMatHelper->AddProperty( &textureName, AI_MATKEY_TEXTURE_DIFFUSE( 0 ) );
+				
+				if ( !importTextureFromArchive( pModel, pArchive, pScene, pMatHelper, textureId ) )
+				{
+				}
 			}
 
-			/*if ( 0 != pCurrentMaterial->textureSpecular.length )
-				mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));*/
-
 		}
-
+		if ( -1 != lightmapId )
+		{
+			importLightmap( pModel, pScene, pMatHelper, lightmapId );
+		}
 		pScene->mMaterials[ pScene->mNumMaterials ] = pMatHelper;
 		pScene->mNumMaterials++;
 	}
+	pScene->mNumTextures = mTextures.size();
+	pScene->mTextures = new aiTexture*[ pScene->mNumTextures ];
+	std::copy( mTextures.begin(), mTextures.end(), pScene->mTextures );
 }
 
 // ------------------------------------------------------------------------------------------------
-//	Counts the number of referenced verices
+//	Counts the number of referenced vertices.
 size_t Q3BSPFileImporter::countData( const std::vector<sQ3BSPFace*> &rArray ) const
 {
 	size_t numVerts = 0;
@@ -603,6 +623,107 @@ aiFace *Q3BSPFileImporter::getNextFace( aiMesh *pMesh, unsigned int &rFaceIdx )
 	return pFace;
 }
 
+// ------------------------------------------------------------------------------------------------
+//	Imports a texture file.
+bool Q3BSPFileImporter::importTextureFromArchive( const Q3BSP::Q3BSPModel *pModel,
+												 Q3BSP::Q3BSPZipArchive *pArchive, aiScene* pScene,
+												 Assimp::MaterialHelper *pMatHelper, int textureId )
+{
+	if ( NULL == pArchive || NULL == pArchive || NULL == pMatHelper )
+	{
+		return false;
+	}
+
+	if ( textureId < 0 || textureId >= static_cast<int>( pModel->m_Textures.size() ) )
+	{
+		return false;
+	}
+
+	bool res = true;
+	sQ3BSPTexture *pTexture = pModel->m_Textures[ textureId ];
+	if ( NULL == pTexture )
+		return false;
+
+	std::string textureName = pTexture->strName;
+	textureName += ".jpg";
+	if ( pArchive->Exists( textureName.c_str() ) )
+	{
+		IOStream *pTextureStream = pArchive->Open( textureName.c_str() );
+		if ( NULL != pTextureStream )
+		{
+			size_t texSize = pTextureStream->FileSize();
+			aiTexture *pTexture = new aiTexture;
+			pTexture->mHeight = 0;
+			pTexture->mWidth = texSize;
+			unsigned char *pData = new unsigned char[ pTexture->mWidth ];
+			size_t readSize = pTextureStream->Read( pData, sizeof( unsigned char ), pTexture->mWidth );
+			ai_assert( readSize == pTexture->mWidth );
+			pTexture->pcData = reinterpret_cast<aiTexel*>( pData );
+			pTexture->achFormatHint[ 0 ] = 'j';
+			pTexture->achFormatHint[ 1 ] = 'p';
+			pTexture->achFormatHint[ 2 ] = 'g';
+			pTexture->achFormatHint[ 2 ] = '\0';
+			res = true;
+
+			aiString name;
+			name.data[ 0 ] = '*';
+			name.length = 1 + ASSIMP_itoa10( name.data + 1, MAXLEN-1, mTextures.size() );
+
+			pArchive->Close( pTextureStream );
+
+			pMatHelper->AddProperty( &name, AI_MATKEY_TEXTURE_DIFFUSE( 0 ) );
+			mTextures.push_back( pTexture );
+		}
+	}
+
+	return res;
+}
+
+// ------------------------------------------------------------------------------------------------
+//	Imports a lightmap file.
+bool Q3BSPFileImporter::importLightmap( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, 
+									   Assimp::MaterialHelper *pMatHelper, int lightmapId )
+{
+	if ( NULL == pModel || NULL == pScene || NULL == pMatHelper )
+	{
+		return false;
+	}
+
+	if ( lightmapId < 0 || lightmapId >= static_cast<int>( pModel->m_Lightmaps.size() ) )
+	{
+		return false;
+	}
+
+	sQ3BSPLightmap *pLightMap = pModel->m_Lightmaps[ lightmapId ];
+	if ( NULL == pLightMap )
+	{
+		return false;
+	}
+
+	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<aiTexel*>( pData );
+	
+	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() );
+
+	pMatHelper->AddProperty( &name,AI_MATKEY_TEXTURE_LIGHTMAP( 1 ) );
+	mTextures.push_back( pTexture );
+
+	return true;
+}
+
 // ------------------------------------------------------------------------------------------------
 
 } // Namespace Assimp

+ 6 - 2
code/Q3BSPFileImporter.h

@@ -82,23 +82,27 @@ private:
 	void InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
 	void separateMapName( const std::string &rImportName, std::string &rArchiveName, std::string &rMapName );
 	bool findFirstMapInArchive( Q3BSP::Q3BSPZipArchive &rArchive, std::string &rMapName );
-	void CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene );
+	void CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, Q3BSP::Q3BSPZipArchive *pArchive );
 	void CreateNodes( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, aiNode *pParent );
 	aiNode *CreateTopology( const Q3BSP::Q3BSPModel *pModel, unsigned int materialIdx, 
 		std::vector<Q3BSP::sQ3BSPFace*> &rArray, aiMesh* pMesh );
 	void createTriangleTopology( const Q3BSP::Q3BSPModel *pModel, Q3BSP::sQ3BSPFace *pQ3BSPFace, aiMesh* pMesh, unsigned int &rFaceIdx, 
 		unsigned int &rVertIdx  );
-	void createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene );
+	void createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, Q3BSP::Q3BSPZipArchive *pArchive );
 	size_t countData( const std::vector<Q3BSP::sQ3BSPFace*> &rArray ) const;
 	size_t countFaces( const std::vector<Q3BSP::sQ3BSPFace*> &rArray ) const;
 	size_t countTriangles( const std::vector<Q3BSP::sQ3BSPFace*> &rArray ) const;
 	void createMaterialMap( const Q3BSP::Q3BSPModel *pModel);
 	aiFace *getNextFace( aiMesh *pMesh, unsigned int &rFaceIdx );
+	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 );
 
 private:
 	aiMesh *m_pCurrentMesh;
 	aiFace *m_pCurrentFace;
 	FaceMap m_MaterialLookupMap;
+	std::vector<aiTexture*> mTextures;
 };
 
 } // Namespace Assimp

+ 5 - 1
code/Q3BSPFileParser.cpp

@@ -112,12 +112,16 @@ bool Q3BSPFileParser::readData( const std::string &rMapName )
 bool Q3BSPFileParser::parseFile()
 {
 	if ( m_Data.empty() )
+	{
 		return false;
+	}
 
 	if ( !validateFormat() )
+	{
 		return false;
+	}
 
-	// Import the dictionary
+	// Imports the dictionary of the level
 	getLumps();
 
 	// Conunt data and prepare model data 

+ 17 - 0
code/Q3BSPZipArchive.cpp

@@ -49,6 +49,7 @@ namespace Q3BSP
 {
 
 // ------------------------------------------------------------------------------------------------
+//	Constructor.
 Q3BSPZipArchive::Q3BSPZipArchive( const std::string& rFile ) :
 	m_ZipFileHandle( NULL ),
 	m_FileList(),
@@ -65,40 +66,53 @@ Q3BSPZipArchive::Q3BSPZipArchive( const std::string& rFile ) :
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Destructor.
 Q3BSPZipArchive::~Q3BSPZipArchive()
 {
 	if ( NULL != m_ZipFileHandle )
+	{
 		unzClose( m_ZipFileHandle );
+	}
 	m_ZipFileHandle = NULL;
 	m_FileList.clear();
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Returns true, if the archive is already open.
 bool Q3BSPZipArchive::isOpen() const
 {
 	return ( NULL != m_ZipFileHandle );
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Returns true, if the filename is part of the archive.
 bool Q3BSPZipArchive::Exists( const char* pFile ) const
 {
 	ai_assert( NULL != pFile );
+	if ( NULL == pFile )
+	{
+		return false;
+	}
 
 	std::string rFile( pFile );
 	std::vector<std::string>::const_iterator it = std::find( m_FileList.begin(), m_FileList.end(), rFile );
 	if ( m_FileList.end() == it )
+	{
 		return false;
+	}
 
 	return true;
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Returns the separator delimiter.
 char Q3BSPZipArchive::getOsSeparator() const
 {
 	return '/';
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Opens a file, which is part of the archive.
 IOStream *Q3BSPZipArchive::Open( const char* pFile, const char* pMode )
 {
 	ai_assert( NULL != pFile );
@@ -115,6 +129,7 @@ IOStream *Q3BSPZipArchive::Open( const char* pFile, const char* pMode )
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Close a filestream.
 void Q3BSPZipArchive::Close( IOStream *pFile )
 {
 	ai_assert( NULL != pFile );
@@ -132,12 +147,14 @@ void Q3BSPZipArchive::Close( IOStream *pFile )
 	}
 }
 // ------------------------------------------------------------------------------------------------
+//	Returns the file-list of the archive.
 void Q3BSPZipArchive::getFileList( std::vector<std::string> &rFileList )
 {
 	rFileList = m_FileList;
 }
 
 // ------------------------------------------------------------------------------------------------
+//	Maps the archive content.
 bool Q3BSPZipArchive::mapArchive()
 {
 	if ( NULL == m_ZipFileHandle )