ObjFileImporter.cpp 19 KB

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