- 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
pull/1/head
kimmi 2010-11-05 18:45:42 +00:00
parent 8a336e8876
commit 5ef9e63ac6
5 changed files with 169 additions and 20 deletions

View File

@ -47,6 +47,9 @@ namespace Assimp
namespace Q3BSP 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 unsigned int CE_BSP_LIGHTMAPSIZE = 128*128*3; ///< = 128( width ) * 128 ( height ) * 3 ( channels / RGB ).
static const int VERION_Q3LEVEL = 46; ///< Supported version. static const int VERION_Q3LEVEL = 46; ///< Supported version.

View File

@ -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 ) static void normalizePathName( const std::string &rPath, std::string &rNormalizedPath )
{ {
rNormalizedPath = ""; rNormalizedPath = "";
@ -125,7 +126,8 @@ static void normalizePathName( const std::string &rPath, std::string &rNormalize
Q3BSPFileImporter::Q3BSPFileImporter() : Q3BSPFileImporter::Q3BSPFileImporter() :
m_pCurrentMesh( NULL ), m_pCurrentMesh( NULL ),
m_pCurrentFace( NULL ), m_pCurrentFace( NULL ),
m_MaterialLookupMap() m_MaterialLookupMap(),
mTextures()
{ {
// empty // empty
} }
@ -200,7 +202,7 @@ void Q3BSPFileImporter::InternReadFile(const std::string &rFile, aiScene* pScene
Q3BSPModel *pBSPModel = fileParser.getModel(); Q3BSPModel *pBSPModel = fileParser.getModel();
if ( NULL != pBSPModel ) 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. // 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 ) if ( NULL == pModel || NULL == pScene )
return; return;
@ -273,7 +276,7 @@ void Q3BSPFileImporter::CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, a
CreateNodes( pModel, pScene, pScene->mRootNode ); CreateNodes( pModel, pScene, pScene->mRootNode );
// Create the assigned materials // 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 ); ai_assert( NULL != pModel );
if ( NULL == pModel ) if ( NULL == pModel )
{
return; return;
}
unsigned int matIdx = 0; unsigned int matIdx = 0;
std::vector<aiMesh*> MeshArray; std::vector<aiMesh*> MeshArray;
@ -343,11 +348,15 @@ aiNode *Q3BSPFileImporter::CreateTopology( const Q3BSP::Q3BSPModel *pModel,
{ {
size_t numVerts = countData( rArray ); size_t numVerts = countData( rArray );
if ( 0 == numVerts ) if ( 0 == numVerts )
{
return NULL; return NULL;
}
size_t numFaces = countFaces( rArray ); size_t numFaces = countFaces( rArray );
if ( 0 == numFaces ) if ( 0 == numFaces )
{
return NULL; return NULL;
}
size_t numTriangles = countTriangles( rArray ); size_t numTriangles = countTriangles( rArray );
pMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE; pMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
@ -404,7 +413,9 @@ void Q3BSPFileImporter::createTriangleTopology( const Q3BSP::Q3BSPModel *pModel,
m_pCurrentFace = getNextFace( pMesh, rFaceIdx ); m_pCurrentFace = getNextFace( pMesh, rFaceIdx );
ai_assert( NULL != m_pCurrentFace ); ai_assert( NULL != m_pCurrentFace );
if ( NULL == m_pCurrentFace ) if ( NULL == m_pCurrentFace )
{
return; return;
}
m_pCurrentFace->mNumIndices = 3; m_pCurrentFace->mNumIndices = 3;
m_pCurrentFace->mIndices = new unsigned int[ m_pCurrentFace->mNumIndices ]; m_pCurrentFace->mIndices = new unsigned int[ m_pCurrentFace->mNumIndices ];
@ -452,13 +463,18 @@ void Q3BSPFileImporter::createTriangleTopology( const Q3BSP::Q3BSPModel *pModel,
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Creates all referenced materials. // 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() ) if ( m_MaterialLookupMap.empty() )
{
return; return;
}
pScene->mMaterials = new aiMaterial*[ m_MaterialLookupMap.size() ]; 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(); for ( FaceMapIt it = m_MaterialLookupMap.begin(); it != m_MaterialLookupMap.end();
++it ) ++it )
{ {
@ -468,12 +484,10 @@ void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScen
continue; continue;
} }
aiString aiMatName;
aiMatName.Set( matName ); aiMatName.Set( matName );
Assimp::MaterialHelper *pMatHelper = new Assimp::MaterialHelper; Assimp::MaterialHelper *pMatHelper = new Assimp::MaterialHelper;
pMatHelper->AddProperty( &aiMatName, AI_MATKEY_NAME ); pMatHelper->AddProperty( &aiMatName, AI_MATKEY_NAME );
int textureId, lightmapId;
extractIds( matName, textureId, lightmapId ); extractIds( matName, textureId, lightmapId );
// Adding the texture // Adding the texture
@ -482,25 +496,31 @@ void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScen
sQ3BSPTexture *pTexture = pModel->m_Textures[ textureId ]; sQ3BSPTexture *pTexture = pModel->m_Textures[ textureId ];
if ( NULL != pTexture ) if ( NULL != pTexture )
{ {
std::string tmp( pTexture->strName ), texName( "" ); std::string tmp( "*" ), texName( "" );
tmp += pTexture->strName;
tmp += ".jpg"; tmp += ".jpg";
normalizePathName( tmp, texName ); 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->mMaterials[ pScene->mNumMaterials ] = pMatHelper;
pScene->mNumMaterials++; 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 Q3BSPFileImporter::countData( const std::vector<sQ3BSPFace*> &rArray ) const
{ {
size_t numVerts = 0; size_t numVerts = 0;
@ -603,6 +623,107 @@ aiFace *Q3BSPFileImporter::getNextFace( aiMesh *pMesh, unsigned int &rFaceIdx )
return pFace; 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 } // Namespace Assimp

View File

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

View File

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

View File

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