Q3BSPFileImporter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ---------------------------------------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ---------------------------------------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
  35. #include "DefaultIOSystem.h"
  36. #include "Q3BSPFileImporter.h"
  37. #include "Q3BSPZipArchive.h"
  38. #include "Q3BSPFileParser.h"
  39. #include "Q3BSPFileData.h"
  40. # ifdef ASSIMP_BUILD_NO_OWN_ZLIB
  41. # include <zlib.h>
  42. # else
  43. # include "../contrib/zlib/zlib.h"
  44. # endif
  45. #include "../include/aiTypes.h"
  46. #include "../include/aiMesh.h"
  47. #include <vector>
  48. namespace Assimp
  49. {
  50. using namespace Q3BSP;
  51. static const std::string Q3BSPExtention = "pk3";
  52. // ------------------------------------------------------------------------------------------------
  53. static void createKey( int id1, int id2, std::string &rKey )
  54. {
  55. std::stringstream str;
  56. str << id1 << "." << id2;
  57. rKey = str.str();
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. static void extractIds( const std::string &rKey, int &rId1, int &rId2 )
  61. {
  62. rId1 = -1;
  63. rId2 = -1;
  64. if ( rKey.empty() )
  65. return;
  66. std::string::size_type pos = rKey.find( "." );
  67. if ( std::string::npos == pos )
  68. return;
  69. std::string tmp1 = rKey.substr( 0, pos - 1 );
  70. std::string tmp2 = rKey.substr( pos + 1, rKey.size() - pos - 1 );
  71. rId1 = atoi( tmp1.c_str() );
  72. rId2 = atoi( tmp2.c_str() );
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. Q3BSPFileImporter::Q3BSPFileImporter() :
  76. m_pCurrentMesh( NULL )
  77. {
  78. // empty
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. Q3BSPFileImporter::~Q3BSPFileImporter()
  82. {
  83. // empty
  84. }
  85. // ------------------------------------------------------------------------------------------------
  86. bool Q3BSPFileImporter::CanRead( const std::string& rFile, IOSystem* pIOHandler, bool checkSig ) const
  87. {
  88. bool isBSPData = false;
  89. if ( checkSig )
  90. isBSPData = SimpleExtensionCheck( rFile, Q3BSPExtention.c_str() );
  91. return isBSPData;
  92. }
  93. // ------------------------------------------------------------------------------------------------
  94. void Q3BSPFileImporter::GetExtensionList(std::set<std::string>& extensions)
  95. {
  96. extensions.insert( Q3BSPExtention );
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. void Q3BSPFileImporter::InternReadFile(const std::string &rFile, aiScene* pScene, IOSystem* pIOHandler)
  100. {
  101. Q3BSPZipArchive Archive( rFile );
  102. if ( !Archive.isOpen() )
  103. {
  104. throw new DeadlyImportError( "Failed to open file " + rFile + "." );
  105. }
  106. std::string archiveName( "" ), mapName( "" );
  107. separateMapName( rFile, archiveName, mapName );
  108. if ( mapName.empty() )
  109. {
  110. if ( !findFirstMapInArchive( Archive, mapName ) )
  111. {
  112. return;
  113. }
  114. }
  115. Q3BSPFileParser fileParser( mapName, &Archive );
  116. Q3BSPModel *pBSPModel = fileParser.getModel();
  117. if ( NULL != pBSPModel )
  118. {
  119. CreateDataFromImport( pBSPModel, pScene );
  120. }
  121. }
  122. // ------------------------------------------------------------------------------------------------
  123. void Q3BSPFileImporter::separateMapName( const std::string &rImportName, std::string &rArchiveName,
  124. std::string &rMapName )
  125. {
  126. rArchiveName = "";
  127. rMapName = "";
  128. if ( rImportName.empty() )
  129. return;
  130. std::string::size_type pos = rImportName.rfind( "," );
  131. if ( std::string::npos == pos )
  132. {
  133. rArchiveName = rImportName;
  134. return;
  135. }
  136. rArchiveName = rImportName.substr( 0, pos );
  137. rMapName = rImportName.substr( pos, rImportName.size() - pos - 1 );
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. bool Q3BSPFileImporter::findFirstMapInArchive( Q3BSPZipArchive &rArchive, std::string &rMapName )
  141. {
  142. rMapName = "";
  143. std::vector<std::string> fileList;
  144. rArchive.getFileList( fileList );
  145. if ( fileList.empty() )
  146. return false;
  147. for ( std::vector<std::string>::iterator it = fileList.begin(); it != fileList.end();
  148. ++it )
  149. {
  150. std::string::size_type pos = (*it).find( "maps/" );
  151. if ( std::string::npos != pos )
  152. {
  153. std::string::size_type extPos = (*it).find( ".bsp" );
  154. if ( std::string::npos != extPos )
  155. {
  156. rMapName = *it;
  157. return true;
  158. }
  159. }
  160. }
  161. return false;
  162. }
  163. // ------------------------------------------------------------------------------------------------
  164. void Q3BSPFileImporter::CreateDataFromImport( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene )
  165. {
  166. if ( NULL == pModel || NULL == pScene )
  167. return;
  168. pScene->mRootNode = new aiNode;
  169. if ( !pModel->m_ModelName.empty() )
  170. {
  171. pScene->mRootNode->mName.Set( pModel->m_ModelName );
  172. }
  173. createMaterialMap( pModel );
  174. CreateNodes( pModel, pScene, pScene->mRootNode );
  175. createMaterials( pModel, pScene );
  176. }
  177. // ------------------------------------------------------------------------------------------------
  178. void Q3BSPFileImporter::CreateNodes( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene, aiNode *pParent )
  179. {
  180. ai_assert( NULL != pModel );
  181. if ( NULL == pModel )
  182. return;
  183. unsigned int matIdx = 0;
  184. std::vector<aiMesh*> MeshArray;
  185. std::vector<aiNode*> NodeArray;
  186. for ( FaceMapIt it = m_MaterialLookupMap.begin(); it != m_MaterialLookupMap.end(); ++it )
  187. {
  188. std::vector<Q3BSP::sQ3BSPFace*> *pArray = (*it).second;
  189. size_t numVerts = countData( *pArray );
  190. if ( 0 != numVerts )
  191. {
  192. aiMesh* pMesh = new aiMesh;
  193. aiNode *pNode = CreateTopology( pModel, matIdx, *pArray, pMesh );
  194. if ( NULL != pNode )
  195. {
  196. NodeArray.push_back( pNode );
  197. MeshArray.push_back( pMesh );
  198. }
  199. else
  200. {
  201. delete pMesh;
  202. }
  203. matIdx++;
  204. }
  205. }
  206. pScene->mNumMeshes = MeshArray.size();
  207. pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ];
  208. for ( size_t i=0; i<MeshArray.size(); ++i )
  209. {
  210. aiMesh *pMesh = MeshArray[ i ];
  211. for ( size_t j=0; j<pMesh->mNumFaces; j++ )
  212. {
  213. aiFace *face = &pMesh->mFaces[j];
  214. if ( NULL == face->mIndices )
  215. {
  216. printf("error\n");
  217. }
  218. ai_assert( NULL != face->mIndices );
  219. }
  220. pScene->mMeshes[ i ] = pMesh;
  221. }
  222. pParent->mNumChildren = MeshArray.size();
  223. pParent->mChildren = new aiNode*[ pScene->mRootNode->mNumChildren ];
  224. for ( size_t i=0; i<NodeArray.size(); i++ )
  225. {
  226. aiNode *pNode = NodeArray[ i ];
  227. pNode->mParent = pParent;
  228. pParent->mChildren[ i ] = pNode;
  229. pParent->mChildren[ i ]->mMeshes[ 0 ] = i;
  230. }
  231. }
  232. // ------------------------------------------------------------------------------------------------
  233. //
  234. aiNode *Q3BSPFileImporter::CreateTopology( const Q3BSP::Q3BSPModel *pModel,
  235. unsigned int materialIdx,
  236. std::vector<sQ3BSPFace*> &rArray,
  237. aiMesh* pMesh )
  238. {
  239. size_t numVerts = countData( rArray );
  240. if ( 0 == numVerts )
  241. return NULL;
  242. size_t numFaces = countFaces( rArray );
  243. if ( 0 == numFaces )
  244. return NULL;
  245. pMesh->mFaces = new aiFace[ numFaces ];
  246. pMesh->mNumFaces = numFaces;
  247. pMesh->mNumVertices = numVerts;
  248. pMesh->mVertices = new aiVector3D[ numVerts ];
  249. pMesh->mNormals = new aiVector3D[ numVerts ];
  250. pMesh->mTextureCoords[ 0 ] = new aiVector3D[ numVerts ];
  251. pMesh->mTextureCoords[ 1 ] = new aiVector3D[ numVerts ];
  252. pMesh->mMaterialIndex = materialIdx;
  253. unsigned int faceIdx = 0;
  254. unsigned int vertIdx = 0;
  255. aiVector3D *pVec3( NULL );
  256. aiVector3D normal;
  257. for ( std::vector<sQ3BSPFace*>::const_iterator it = rArray.begin(); it != rArray.end();
  258. ++it )
  259. {
  260. Q3BSP::sQ3BSPFace *pQ3BSPFace = *it;
  261. ai_assert( NULL != pQ3BSPFace );
  262. if ( NULL == pQ3BSPFace )
  263. continue;
  264. if ( pQ3BSPFace->iNumOfFaceVerts == 0 )
  265. continue;
  266. if ( pQ3BSPFace->iType == 1 || pQ3BSPFace->iType == 3 )
  267. {
  268. createTriangleTopology( pModel, pQ3BSPFace, pMesh, faceIdx, vertIdx );
  269. }
  270. }
  271. aiNode *pNode = new aiNode;
  272. pNode->mNumMeshes = 1;
  273. pNode->mMeshes = new unsigned int[ 1 ];
  274. return pNode;
  275. }
  276. // ------------------------------------------------------------------------------------------------
  277. void Q3BSPFileImporter::createTriangleTopology( const Q3BSP::Q3BSPModel *pModel,
  278. Q3BSP::sQ3BSPFace *pQ3BSPFace,
  279. aiMesh* pMesh,
  280. unsigned int &rFaceIdx,
  281. unsigned int &rVertIdx )
  282. {
  283. ai_assert( rFaceIdx < pMesh->mNumFaces );
  284. aiFace *pFace = &pMesh->mFaces[ rFaceIdx ];
  285. ai_assert( NULL != pFace );
  286. rFaceIdx++;
  287. pFace->mNumIndices = pQ3BSPFace->iNumOfFaceVerts;
  288. pFace->mIndices = new unsigned int[ pFace->mNumIndices ];
  289. aiVector3D normal;
  290. normal.Set( pQ3BSPFace->vNormal.x, pQ3BSPFace->vNormal.y, pQ3BSPFace->vNormal.z );
  291. for ( size_t i = 0; i < pFace->mNumIndices; ++i )
  292. {
  293. const size_t index = pQ3BSPFace->iVertexIndex + pModel->m_Indices[ pQ3BSPFace->iFaceVertexIndex + i ];
  294. ai_assert( index < pModel->m_Vertices.size() );
  295. sQ3BSPVertex *pVertex = pModel->m_Vertices[ index ];
  296. ai_assert( NULL != pVertex );
  297. if ( NULL == pVertex )
  298. continue;
  299. pMesh->mVertices[ rVertIdx ].Set( pVertex->vPosition.x, pVertex->vPosition.y, pVertex->vPosition.z );
  300. pMesh->mNormals[ rVertIdx ].Set( normal.x, normal.y, normal.z );
  301. pMesh->mNumUVComponents[ 0 ] = 2;
  302. pMesh->mTextureCoords[ 0 ][ rVertIdx ].Set( pVertex->vTexCoord.x, pVertex->vTexCoord.y, 0.0f );
  303. pMesh->mNumUVComponents[ 1 ] = 2;
  304. pMesh->mTextureCoords[ 1 ][ rVertIdx ].Set( pVertex->vLightmap.x, pVertex->vLightmap.y, 0.0f );
  305. pFace->mIndices[ i ] = rVertIdx;
  306. rVertIdx++;
  307. }
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. void Q3BSPFileImporter::createMaterials( const Q3BSP::Q3BSPModel *pModel, aiScene* pScene )
  311. {
  312. if ( m_MaterialLookupMap.empty() )
  313. return;
  314. pScene->mMaterials = new aiMaterial*[ m_MaterialLookupMap.size() ];
  315. for ( FaceMapIt it = m_MaterialLookupMap.begin(); it != m_MaterialLookupMap.end();
  316. ++it )
  317. {
  318. const std::string matName = (*it).first;
  319. if ( matName.empty() )
  320. continue;
  321. aiString aiMatName;
  322. aiMatName.Set( matName );
  323. Assimp::MaterialHelper *pMatHelper = new Assimp::MaterialHelper;
  324. pMatHelper->AddProperty( &aiMatName, AI_MATKEY_NAME );
  325. int textureId, lightmapId;
  326. extractIds( matName, textureId, lightmapId );
  327. // Adding the texture
  328. if ( -1 != textureId )
  329. {
  330. sQ3BSPTexture *pTexture = pModel->m_Textures[ textureId ];
  331. if ( NULL != pTexture )
  332. {
  333. aiString textureName( pTexture->strName );
  334. pMatHelper->AddProperty( &textureName, AI_MATKEY_TEXTURE_DIFFUSE( 0 ) );
  335. }
  336. /* if ( 0 != pCurrentMaterial->textureSpecular.length )
  337. mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));*/
  338. }
  339. pScene->mMaterials[ pScene->mNumMaterials ] = pMatHelper;
  340. pScene->mNumMaterials++;
  341. }
  342. }
  343. // ------------------------------------------------------------------------------------------------
  344. size_t Q3BSPFileImporter::countData( const std::vector<sQ3BSPFace*> &rArray ) const
  345. {
  346. size_t numVerts = 0;
  347. for ( std::vector<sQ3BSPFace*>::const_iterator it = rArray.begin(); it != rArray.end();
  348. ++it )
  349. {
  350. sQ3BSPFace *pQ3BSPFace = *it;
  351. if ( pQ3BSPFace->iType == Polygon || pQ3BSPFace->iType == Mesh )
  352. {
  353. Q3BSP::sQ3BSPFace *pQ3BSPFace = *it;
  354. ai_assert( NULL != pQ3BSPFace );
  355. numVerts += pQ3BSPFace->iNumOfFaceVerts;
  356. }
  357. }
  358. return numVerts;
  359. }
  360. // ------------------------------------------------------------------------------------------------
  361. size_t Q3BSPFileImporter::countFaces( const std::vector<Q3BSP::sQ3BSPFace*> &rArray ) const
  362. {
  363. size_t numFaces = 0;
  364. for ( std::vector<sQ3BSPFace*>::const_iterator it = rArray.begin(); it != rArray.end();
  365. ++it )
  366. {
  367. Q3BSP::sQ3BSPFace *pQ3BSPFace = *it;
  368. if ( pQ3BSPFace->iNumOfFaceVerts > 0)
  369. {
  370. numFaces++;
  371. }
  372. }
  373. return numFaces;
  374. }
  375. // ------------------------------------------------------------------------------------------------
  376. void Q3BSPFileImporter::createMaterialMap( const Q3BSP::Q3BSPModel *pModel)
  377. {
  378. std::string key( "" );
  379. std::vector<sQ3BSPFace*> *pCurFaceArray = NULL;
  380. for ( size_t idx=0; idx < pModel->m_Faces.size(); idx++ )
  381. {
  382. Q3BSP::sQ3BSPFace *pQ3BSPFace = pModel->m_Faces[ idx ];
  383. int texId = pQ3BSPFace->iTextureID;
  384. int lightMapId = pQ3BSPFace->iLightmapID;
  385. createKey( texId, lightMapId, key );
  386. FaceMapIt it = m_MaterialLookupMap.find( key );
  387. if ( m_MaterialLookupMap.end() == it )
  388. {
  389. std::vector<Q3BSP::sQ3BSPFace*> *pArray = new std::vector<Q3BSP::sQ3BSPFace*>;
  390. pArray->push_back( pQ3BSPFace );
  391. m_MaterialLookupMap[ key ] = pArray;
  392. }
  393. else
  394. {
  395. std::vector<Q3BSP::sQ3BSPFace*> *pArray = (*it).second;
  396. ai_assert( NULL != pArray );
  397. if ( NULL != pArray )
  398. {
  399. pArray->push_back( pQ3BSPFace );
  400. }
  401. }
  402. }
  403. }
  404. // ------------------------------------------------------------------------------------------------
  405. } // Namespace Assimp
  406. #endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER