ObjFileImporter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #include "AssimpPCH.h"
  35. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  36. #include "DefaultIOSystem.h"
  37. #include "ObjFileImporter.h"
  38. #include "ObjFileParser.h"
  39. #include "ObjFileData.h"
  40. static const aiImporterDesc desc = {
  41. "Wavefront Object Importer",
  42. "",
  43. "",
  44. "surfaces not supported",
  45. aiImporterFlags_SupportTextFlavour,
  46. 0,
  47. 0,
  48. 0,
  49. 0,
  50. "obj"
  51. };
  52. namespace Assimp {
  53. using namespace std;
  54. // ------------------------------------------------------------------------------------------------
  55. // Default constructor
  56. ObjFileImporter::ObjFileImporter() :
  57. m_Buffer(),
  58. m_pRootObject( NULL ),
  59. m_strAbsPath( "" )
  60. {
  61. DefaultIOSystem io;
  62. m_strAbsPath = io.getOsSeparator();
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Destructor.
  66. ObjFileImporter::~ObjFileImporter()
  67. {
  68. // Release root object instance
  69. if (NULL != m_pRootObject)
  70. {
  71. delete m_pRootObject;
  72. m_pRootObject = NULL;
  73. }
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. // Returns true, if file is an obj file.
  77. bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler , bool checkSig ) const
  78. {
  79. if(!checkSig) //Check File Extension
  80. {
  81. return SimpleExtensionCheck(pFile,"obj");
  82. }
  83. else //Check file Header
  84. {
  85. static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " };
  86. return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9 );
  87. }
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. const aiImporterDesc* ObjFileImporter::GetInfo () const
  91. {
  92. return &desc;
  93. }
  94. // ------------------------------------------------------------------------------------------------
  95. // Obj-file import implementation
  96. void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  97. {
  98. DefaultIOSystem io;
  99. // Read file into memory
  100. const std::string mode = "rb";
  101. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, mode));
  102. if (NULL == file.get())
  103. throw DeadlyImportError( "Failed to open file " + pFile + ".");
  104. // Get the file-size and validate it, throwing an exception when fails
  105. size_t fileSize = file->FileSize();
  106. if( fileSize < 16)
  107. throw DeadlyImportError( "OBJ-file is too small.");
  108. // Allocate buffer and read file into it
  109. TextFileToBuffer(file.get(),m_Buffer);
  110. // Get the model name
  111. std::string strModelName;
  112. std::string::size_type pos = pFile.find_last_of( "\\/" );
  113. if ( pos != std::string::npos )
  114. {
  115. strModelName = pFile.substr(pos+1, pFile.size() - pos - 1);
  116. }
  117. else
  118. {
  119. strModelName = pFile;
  120. }
  121. // parse the file into a temporary representation
  122. ObjFileParser parser(m_Buffer, strModelName, pIOHandler);
  123. // And create the proper return structures out of it
  124. CreateDataFromImport(parser.GetModel(), pScene);
  125. // Clean up allocated storage for the next import
  126. m_Buffer.clear();
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. // Create the data from parsed obj-file
  130. void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene)
  131. {
  132. if (0L == pModel)
  133. return;
  134. // Create the root node of the scene
  135. pScene->mRootNode = new aiNode;
  136. if ( !pModel->m_ModelName.empty() )
  137. {
  138. // Set the name of the scene
  139. pScene->mRootNode->mName.Set(pModel->m_ModelName);
  140. }
  141. else
  142. {
  143. // This is an error, so break down the application
  144. ai_assert(false);
  145. }
  146. // Create nodes for the whole scene
  147. std::vector<aiMesh*> MeshArray;
  148. for (size_t index = 0; index < pModel->m_Objects.size(); index++)
  149. {
  150. createNodes(pModel, pModel->m_Objects[ index ], index, pScene->mRootNode, pScene, MeshArray);
  151. }
  152. // Create mesh pointer buffer for this scene
  153. if (pScene->mNumMeshes > 0)
  154. {
  155. pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
  156. for (size_t index =0; index < MeshArray.size(); index++)
  157. {
  158. pScene->mMeshes [ index ] = MeshArray[ index ];
  159. }
  160. }
  161. // Create all materials
  162. createMaterials( pModel, pScene );
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. // Creates all nodes of the model
  166. aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pObject,
  167. unsigned int /*uiMeshIndex*/,
  168. aiNode *pParent, aiScene* pScene,
  169. std::vector<aiMesh*> &MeshArray )
  170. {
  171. ai_assert( NULL != pModel );
  172. if ( NULL == pObject )
  173. return NULL;
  174. // Store older mesh size to be able to computes mesh offsets for new mesh instances
  175. const size_t oldMeshSize = MeshArray.size();
  176. aiNode *pNode = new aiNode;
  177. pNode->mName = pObject->m_strObjName;
  178. // If we have a parent node, store it
  179. if (pParent != NULL)
  180. appendChildToParentNode(pParent, pNode);
  181. for ( unsigned int i=0; i< pObject->m_Meshes.size(); i++ )
  182. {
  183. unsigned int meshId = pObject->m_Meshes[ i ];
  184. aiMesh *pMesh = new aiMesh;
  185. createTopology( pModel, pObject, meshId, pMesh );
  186. if ( pMesh->mNumVertices > 0 )
  187. {
  188. MeshArray.push_back( pMesh );
  189. }
  190. else
  191. {
  192. delete pMesh;
  193. }
  194. }
  195. // Create all nodes from the sub-objects stored in the current object
  196. if ( !pObject->m_SubObjects.empty() )
  197. {
  198. size_t numChilds = pObject->m_SubObjects.size();
  199. pNode->mNumChildren = static_cast<unsigned int>( numChilds );
  200. pNode->mChildren = new aiNode*[ numChilds ];
  201. pNode->mNumMeshes = 1;
  202. pNode->mMeshes = new unsigned int[ 1 ];
  203. }
  204. // Set mesh instances into scene- and node-instances
  205. const size_t meshSizeDiff = MeshArray.size()- oldMeshSize;
  206. if ( meshSizeDiff > 0 )
  207. {
  208. pNode->mMeshes = new unsigned int[ meshSizeDiff ];
  209. pNode->mNumMeshes = static_cast<unsigned int>( meshSizeDiff );
  210. size_t index = 0;
  211. for (size_t i = oldMeshSize; i < MeshArray.size(); i++)
  212. {
  213. pNode->mMeshes[ index ] = pScene->mNumMeshes;
  214. pScene->mNumMeshes++;
  215. index++;
  216. }
  217. }
  218. return pNode;
  219. }
  220. // ------------------------------------------------------------------------------------------------
  221. // Create topology data
  222. void ObjFileImporter::createTopology(const ObjFile::Model* pModel,
  223. const ObjFile::Object* pData,
  224. unsigned int uiMeshIndex,
  225. aiMesh* pMesh )
  226. {
  227. // Checking preconditions
  228. ai_assert( NULL != pModel );
  229. if (NULL == pData)
  230. return;
  231. // Create faces
  232. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
  233. ai_assert( NULL != pObjMesh );
  234. pMesh->mNumFaces = 0;
  235. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  236. {
  237. ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
  238. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  239. pMesh->mNumFaces += inp->m_pVertices->size() - 1;
  240. }
  241. else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  242. pMesh->mNumFaces += inp->m_pVertices->size();
  243. }
  244. else {
  245. ++pMesh->mNumFaces;
  246. }
  247. }
  248. unsigned int uiIdxCount = 0u;
  249. if ( pMesh->mNumFaces > 0 )
  250. {
  251. pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
  252. if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial )
  253. {
  254. pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
  255. }
  256. unsigned int outIndex = 0;
  257. // Copy all data from all stored meshes
  258. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  259. {
  260. ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
  261. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  262. for(size_t i = 0; i < inp->m_pVertices->size() - 1; ++i) {
  263. aiFace& f = pMesh->mFaces[ outIndex++ ];
  264. uiIdxCount += f.mNumIndices = 2;
  265. f.mIndices = new unsigned int[2];
  266. }
  267. continue;
  268. }
  269. else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  270. for(size_t i = 0; i < inp->m_pVertices->size(); ++i) {
  271. aiFace& f = pMesh->mFaces[ outIndex++ ];
  272. uiIdxCount += f.mNumIndices = 1;
  273. f.mIndices = new unsigned int[1];
  274. }
  275. continue;
  276. }
  277. aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
  278. const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
  279. uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
  280. if (pFace->mNumIndices > 0) {
  281. pFace->mIndices = new unsigned int[ uiNumIndices ];
  282. }
  283. }
  284. }
  285. // Create mesh vertices
  286. createVertexArray(pModel, pData, uiMeshIndex, pMesh, uiIdxCount);
  287. }
  288. // ------------------------------------------------------------------------------------------------
  289. // Creates a vertex array
  290. void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
  291. const ObjFile::Object* pCurrentObject,
  292. unsigned int uiMeshIndex,
  293. aiMesh* pMesh,
  294. unsigned int uiIdxCount)
  295. {
  296. // Checking preconditions
  297. ai_assert( NULL != pCurrentObject );
  298. // Break, if no faces are stored in object
  299. if ( pCurrentObject->m_Meshes.empty() )
  300. return;
  301. // Get current mesh
  302. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
  303. if ( NULL == pObjMesh || pObjMesh->m_uiNumIndices < 1)
  304. return;
  305. // Copy vertices of this mesh instance
  306. pMesh->mNumVertices = uiIdxCount;
  307. pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
  308. // Allocate buffer for normal vectors
  309. if ( !pModel->m_Normals.empty() && pObjMesh->m_hasNormals )
  310. pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
  311. // Allocate buffer for texture coordinates
  312. if ( !pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0] )
  313. {
  314. pMesh->mNumUVComponents[ 0 ] = 2;
  315. pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
  316. }
  317. // Copy vertices, normals and textures into aiMesh instance
  318. unsigned int newIndex = 0, outIndex = 0;
  319. for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ )
  320. {
  321. // Get source face
  322. ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ];
  323. // Copy all index arrays
  324. for ( size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ )
  325. {
  326. const unsigned int vertex = pSourceFace->m_pVertices->at( vertexIndex );
  327. if ( vertex >= pModel->m_Vertices.size() )
  328. throw DeadlyImportError( "OBJ: vertex index out of range" );
  329. pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
  330. // Copy all normals
  331. if ( !pSourceFace->m_pNormals->empty() && !pModel->m_Normals.empty())
  332. {
  333. const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
  334. if ( normal >= pModel->m_Normals.size() )
  335. throw DeadlyImportError("OBJ: vertex normal index out of range");
  336. pMesh->mNormals[ newIndex ] = pModel->m_Normals[ normal ];
  337. }
  338. // Copy all texture coordinates
  339. if ( !pModel->m_TextureCoord.empty() )
  340. {
  341. if ( !pSourceFace->m_pTexturCoords->empty() )
  342. {
  343. const unsigned int tex = pSourceFace->m_pTexturCoords->at( vertexIndex );
  344. ai_assert( tex < pModel->m_TextureCoord.size() );
  345. for ( size_t i=0; i < pMesh->GetNumUVChannels(); i++ )
  346. {
  347. if ( tex >= pModel->m_TextureCoord.size() )
  348. throw DeadlyImportError("OBJ: texture coord index out of range");
  349. aiVector2D coord2d = pModel->m_TextureCoord[ tex ];
  350. pMesh->mTextureCoords[ i ][ newIndex ] = aiVector3D( coord2d.x, coord2d.y, 0.0 );
  351. }
  352. }
  353. }
  354. ai_assert( pMesh->mNumVertices > newIndex );
  355. // Get destination face
  356. aiFace *pDestFace = &pMesh->mFaces[ outIndex ];
  357. const bool last = ( vertexIndex == pSourceFace->m_pVertices->size() - 1 );
  358. if (pSourceFace->m_PrimitiveType != aiPrimitiveType_LINE || !last)
  359. {
  360. pDestFace->mIndices[ outVertexIndex ] = newIndex;
  361. outVertexIndex++;
  362. }
  363. if (pSourceFace->m_PrimitiveType == aiPrimitiveType_POINT)
  364. {
  365. outIndex++;
  366. outVertexIndex = 0;
  367. }
  368. else if (pSourceFace->m_PrimitiveType == aiPrimitiveType_LINE)
  369. {
  370. outVertexIndex = 0;
  371. if(!last)
  372. outIndex++;
  373. if (vertexIndex) {
  374. if(!last) {
  375. pMesh->mVertices[ newIndex+1 ] = pMesh->mVertices[ newIndex ];
  376. if ( !pSourceFace->m_pNormals->empty() && !pModel->m_Normals.empty()) {
  377. pMesh->mNormals[ newIndex+1 ] = pMesh->mNormals[newIndex ];
  378. }
  379. if ( !pModel->m_TextureCoord.empty() ) {
  380. for ( size_t i=0; i < pMesh->GetNumUVChannels(); i++ ) {
  381. pMesh->mTextureCoords[ i ][ newIndex+1 ] = pMesh->mTextureCoords[ i ][ newIndex ];
  382. }
  383. }
  384. ++newIndex;
  385. }
  386. pDestFace[-1].mIndices[1] = newIndex;
  387. }
  388. }
  389. else if (last) {
  390. outIndex++;
  391. }
  392. ++newIndex;
  393. }
  394. }
  395. }
  396. // ------------------------------------------------------------------------------------------------
  397. // Counts all stored meshes
  398. void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes)
  399. {
  400. iNumMeshes = 0;
  401. if ( rObjects.empty() )
  402. return;
  403. iNumMeshes += static_cast<unsigned int>( rObjects.size() );
  404. for (std::vector<ObjFile::Object*>::const_iterator it = rObjects.begin();
  405. it != rObjects.end();
  406. ++it)
  407. {
  408. if (!(*it)->m_SubObjects.empty())
  409. {
  410. countObjects((*it)->m_SubObjects, iNumMeshes);
  411. }
  412. }
  413. }
  414. // ------------------------------------------------------------------------------------------------
  415. // Creates the material
  416. void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene )
  417. {
  418. ai_assert( NULL != pScene );
  419. if ( NULL == pScene )
  420. return;
  421. const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size();
  422. pScene->mNumMaterials = 0;
  423. if ( pModel->m_MaterialLib.empty() ) {
  424. DefaultLogger::get()->debug("OBJ: no materials specified");
  425. return;
  426. }
  427. pScene->mMaterials = new aiMaterial*[ numMaterials ];
  428. for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
  429. {
  430. // Store material name
  431. std::map<std::string, ObjFile::Material*>::const_iterator it;
  432. it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] );
  433. // No material found, use the default material
  434. if ( pModel->m_MaterialMap.end() == it )
  435. continue;
  436. aiMaterial* mat = new aiMaterial;
  437. ObjFile::Material *pCurrentMaterial = (*it).second;
  438. mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME );
  439. // convert illumination model
  440. int sm = 0;
  441. switch (pCurrentMaterial->illumination_model)
  442. {
  443. case 0:
  444. sm = aiShadingMode_NoShading;
  445. break;
  446. case 1:
  447. sm = aiShadingMode_Gouraud;
  448. break;
  449. case 2:
  450. sm = aiShadingMode_Phong;
  451. break;
  452. default:
  453. sm = aiShadingMode_Gouraud;
  454. DefaultLogger::get()->error("OBJ: unexpected illumination model (0-2 recognized)");
  455. }
  456. mat->AddProperty<int>( &sm, 1, AI_MATKEY_SHADING_MODEL);
  457. // multiplying the specular exponent with 2 seems to yield better results
  458. pCurrentMaterial->shineness *= 4.f;
  459. // Adding material colors
  460. mat->AddProperty( &pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT );
  461. mat->AddProperty( &pCurrentMaterial->diffuse, 1, AI_MATKEY_COLOR_DIFFUSE );
  462. mat->AddProperty( &pCurrentMaterial->specular, 1, AI_MATKEY_COLOR_SPECULAR );
  463. mat->AddProperty( &pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS );
  464. mat->AddProperty( &pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY );
  465. // Adding refraction index
  466. mat->AddProperty( &pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI );
  467. // Adding textures
  468. if ( 0 != pCurrentMaterial->texture.length )
  469. mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
  470. if ( 0 != pCurrentMaterial->textureAmbient.length )
  471. mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
  472. if ( 0 != pCurrentMaterial->textureSpecular.length )
  473. mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
  474. if ( 0 != pCurrentMaterial->textureBump.length )
  475. mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
  476. if ( 0 != pCurrentMaterial->textureDisp.length )
  477. mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
  478. if ( 0 != pCurrentMaterial->textureOpacity.length )
  479. mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
  480. if ( 0 != pCurrentMaterial->textureSpecularity.length )
  481. mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
  482. // Store material property info in material array in scene
  483. pScene->mMaterials[ pScene->mNumMaterials ] = mat;
  484. pScene->mNumMaterials++;
  485. }
  486. // Test number of created materials.
  487. ai_assert( pScene->mNumMaterials == numMaterials );
  488. }
  489. // ------------------------------------------------------------------------------------------------
  490. // Appends this node to the parent node
  491. void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
  492. {
  493. // Checking preconditions
  494. ai_assert( NULL != pParent );
  495. ai_assert( NULL != pChild );
  496. // Assign parent to child
  497. pChild->mParent = pParent;
  498. size_t sNumChildren = 0;
  499. (void)sNumChildren; // remove warning on release build
  500. // If already children was assigned to the parent node, store them in a
  501. std::vector<aiNode*> temp;
  502. if (pParent->mChildren != NULL)
  503. {
  504. sNumChildren = pParent->mNumChildren;
  505. ai_assert( 0 != sNumChildren );
  506. for (size_t index = 0; index < pParent->mNumChildren; index++)
  507. {
  508. temp.push_back(pParent->mChildren [ index ] );
  509. }
  510. delete [] pParent->mChildren;
  511. }
  512. // Copy node instances into parent node
  513. pParent->mNumChildren++;
  514. pParent->mChildren = new aiNode*[ pParent->mNumChildren ];
  515. for (size_t index = 0; index < pParent->mNumChildren-1; index++)
  516. {
  517. pParent->mChildren[ index ] = temp [ index ];
  518. }
  519. pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
  520. }
  521. // ------------------------------------------------------------------------------------------------
  522. } // Namespace Assimp
  523. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER