ObjFileImporter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. static const unsigned int ObjMinSize = 16;
  53. namespace Assimp {
  54. using namespace std;
  55. // ------------------------------------------------------------------------------------------------
  56. // Default constructor
  57. ObjFileImporter::ObjFileImporter() :
  58. m_Buffer(),
  59. m_pRootObject( NULL ),
  60. m_strAbsPath( "" )
  61. {
  62. DefaultIOSystem io;
  63. m_strAbsPath = io.getOsSeparator();
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. // Destructor.
  67. ObjFileImporter::~ObjFileImporter()
  68. {
  69. delete m_pRootObject;
  70. m_pRootObject = NULL;
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Returns true, if file is an obj file.
  74. bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler , bool checkSig ) const
  75. {
  76. if(!checkSig) //Check File Extension
  77. {
  78. return SimpleExtensionCheck(pFile,"obj");
  79. }
  80. else //Check file Header
  81. {
  82. static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " };
  83. return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9 );
  84. }
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. const aiImporterDesc* ObjFileImporter::GetInfo () const
  88. {
  89. return &desc;
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. // Obj-file import implementation
  93. void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  94. {
  95. DefaultIOSystem io;
  96. // Read file into memory
  97. const std::string mode = "rb";
  98. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, mode));
  99. if( !file.get() ) {
  100. throw DeadlyImportError( "Failed to open file " + pFile + "." );
  101. }
  102. // Get the file-size and validate it, throwing an exception when fails
  103. size_t fileSize = file->FileSize();
  104. if( fileSize < ObjMinSize ) {
  105. throw DeadlyImportError( "OBJ-file is too small.");
  106. }
  107. // Allocate buffer and read file into it
  108. TextFileToBuffer(file.get(),m_Buffer);
  109. // Get the model name
  110. std::string strModelName;
  111. std::string::size_type pos = pFile.find_last_of( "\\/" );
  112. if ( pos != std::string::npos )
  113. {
  114. strModelName = pFile.substr(pos+1, pFile.size() - pos - 1);
  115. }
  116. else
  117. {
  118. strModelName = pFile;
  119. }
  120. // parse the file into a temporary representation
  121. ObjFileParser parser(m_Buffer, strModelName, pIOHandler);
  122. // And create the proper return structures out of it
  123. CreateDataFromImport(parser.GetModel(), pScene);
  124. // Clean up allocated storage for the next import
  125. m_Buffer.clear();
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. // Create the data from parsed obj-file
  129. void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene) {
  130. if( 0L == pModel ) {
  131. return;
  132. }
  133. // Create the root node of the scene
  134. pScene->mRootNode = new aiNode;
  135. if ( !pModel->m_ModelName.empty() )
  136. {
  137. // Set the name of the scene
  138. pScene->mRootNode->mName.Set(pModel->m_ModelName);
  139. }
  140. else
  141. {
  142. // This is a fatal error, so break down the application
  143. ai_assert(false);
  144. }
  145. // Create nodes for the whole scene
  146. std::vector<aiMesh*> MeshArray;
  147. for (size_t index = 0; index < pModel->m_Objects.size(); index++)
  148. {
  149. createNodes(pModel, pModel->m_Objects[ index ], pScene->mRootNode, pScene, MeshArray);
  150. }
  151. // Create mesh pointer buffer for this scene
  152. if (pScene->mNumMeshes > 0)
  153. {
  154. pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
  155. for (size_t index =0; index < MeshArray.size(); index++)
  156. {
  157. pScene->mMeshes [ index ] = MeshArray[ index ];
  158. }
  159. }
  160. // Create all materials
  161. createMaterials( pModel, pScene );
  162. }
  163. // ------------------------------------------------------------------------------------------------
  164. // Creates all nodes of the model
  165. aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pObject,
  166. aiNode *pParent, aiScene* pScene,
  167. std::vector<aiMesh*> &MeshArray )
  168. {
  169. ai_assert( NULL != pModel );
  170. if( NULL == pObject ) {
  171. return NULL;
  172. }
  173. // Store older mesh size to be able to computes mesh offsets for new mesh instances
  174. const size_t oldMeshSize = MeshArray.size();
  175. aiNode *pNode = new aiNode;
  176. pNode->mName = pObject->m_strObjName;
  177. // If we have a parent node, store it
  178. if( pParent != NULL ) {
  179. appendChildToParentNode( pParent, pNode );
  180. }
  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. }
  232. // Create faces
  233. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
  234. ai_assert( NULL != pObjMesh );
  235. pMesh->mNumFaces = 0;
  236. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  237. {
  238. ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
  239. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  240. pMesh->mNumFaces += inp->m_pVertices->size() - 1;
  241. pMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  242. }
  243. else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  244. pMesh->mNumFaces += inp->m_pVertices->size();
  245. pMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  246. } else {
  247. ++pMesh->mNumFaces;
  248. if (inp->m_pVertices->size() > 3) {
  249. pMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  250. }
  251. else {
  252. pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  253. }
  254. }
  255. }
  256. unsigned int uiIdxCount = 0u;
  257. if ( pMesh->mNumFaces > 0 )
  258. {
  259. pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
  260. if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial )
  261. {
  262. pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
  263. }
  264. unsigned int outIndex = 0;
  265. // Copy all data from all stored meshes
  266. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  267. {
  268. ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
  269. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  270. for(size_t i = 0; i < inp->m_pVertices->size() - 1; ++i) {
  271. aiFace& f = pMesh->mFaces[ outIndex++ ];
  272. uiIdxCount += f.mNumIndices = 2;
  273. f.mIndices = new unsigned int[2];
  274. }
  275. continue;
  276. }
  277. else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  278. for(size_t i = 0; i < inp->m_pVertices->size(); ++i) {
  279. aiFace& f = pMesh->mFaces[ outIndex++ ];
  280. uiIdxCount += f.mNumIndices = 1;
  281. f.mIndices = new unsigned int[1];
  282. }
  283. continue;
  284. }
  285. aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
  286. const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
  287. uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
  288. if (pFace->mNumIndices > 0) {
  289. pFace->mIndices = new unsigned int[ uiNumIndices ];
  290. }
  291. }
  292. }
  293. // Create mesh vertices
  294. createVertexArray(pModel, pData, uiMeshIndex, pMesh, uiIdxCount);
  295. }
  296. // ------------------------------------------------------------------------------------------------
  297. // Creates a vertex array
  298. void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
  299. const ObjFile::Object* pCurrentObject,
  300. unsigned int uiMeshIndex,
  301. aiMesh* pMesh,
  302. unsigned int uiIdxCount)
  303. {
  304. // Checking preconditions
  305. ai_assert( NULL != pCurrentObject );
  306. // Break, if no faces are stored in object
  307. if ( pCurrentObject->m_Meshes.empty() )
  308. return;
  309. // Get current mesh
  310. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
  311. if ( NULL == pObjMesh || pObjMesh->m_uiNumIndices < 1)
  312. return;
  313. // Copy vertices of this mesh instance
  314. pMesh->mNumVertices = uiIdxCount;
  315. pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
  316. // Allocate buffer for normal vectors
  317. if ( !pModel->m_Normals.empty() && pObjMesh->m_hasNormals )
  318. pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
  319. // Allocate buffer for texture coordinates
  320. if ( !pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0] )
  321. {
  322. pMesh->mNumUVComponents[ 0 ] = 2;
  323. pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
  324. }
  325. // Copy vertices, normals and textures into aiMesh instance
  326. unsigned int newIndex = 0, outIndex = 0;
  327. for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ )
  328. {
  329. // Get source face
  330. ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ];
  331. // Copy all index arrays
  332. for ( size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ )
  333. {
  334. const unsigned int vertex = pSourceFace->m_pVertices->at( vertexIndex );
  335. if ( vertex >= pModel->m_Vertices.size() )
  336. throw DeadlyImportError( "OBJ: vertex index out of range" );
  337. pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
  338. // Copy all normals
  339. if ( !pModel->m_Normals.empty() && vertexIndex < pSourceFace->m_pNormals->size())
  340. {
  341. const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
  342. if ( normal >= pModel->m_Normals.size() )
  343. throw DeadlyImportError("OBJ: vertex normal index out of range");
  344. pMesh->mNormals[ newIndex ] = pModel->m_Normals[ normal ];
  345. }
  346. // Copy all texture coordinates
  347. if ( !pModel->m_TextureCoord.empty() && vertexIndex < pSourceFace->m_pTexturCoords->size())
  348. {
  349. const unsigned int tex = pSourceFace->m_pTexturCoords->at( vertexIndex );
  350. ai_assert( tex < pModel->m_TextureCoord.size() );
  351. if ( tex >= pModel->m_TextureCoord.size() )
  352. throw DeadlyImportError("OBJ: texture coordinate index out of range");
  353. const aiVector3D &coord3d = pModel->m_TextureCoord[ tex ];
  354. pMesh->mTextureCoords[ 0 ][ newIndex ] = aiVector3D( coord3d.x, coord3d.y, coord3d.z );
  355. }
  356. ai_assert( pMesh->mNumVertices > newIndex );
  357. // Get destination face
  358. aiFace *pDestFace = &pMesh->mFaces[ outIndex ];
  359. const bool last = ( vertexIndex == pSourceFace->m_pVertices->size() - 1 );
  360. if (pSourceFace->m_PrimitiveType != aiPrimitiveType_LINE || !last)
  361. {
  362. pDestFace->mIndices[ outVertexIndex ] = newIndex;
  363. outVertexIndex++;
  364. }
  365. if (pSourceFace->m_PrimitiveType == aiPrimitiveType_POINT)
  366. {
  367. outIndex++;
  368. outVertexIndex = 0;
  369. }
  370. else if (pSourceFace->m_PrimitiveType == aiPrimitiveType_LINE)
  371. {
  372. outVertexIndex = 0;
  373. if(!last)
  374. outIndex++;
  375. if (vertexIndex) {
  376. if(!last) {
  377. pMesh->mVertices[ newIndex+1 ] = pMesh->mVertices[ newIndex ];
  378. if ( !pSourceFace->m_pNormals->empty() && !pModel->m_Normals.empty()) {
  379. pMesh->mNormals[ newIndex+1 ] = pMesh->mNormals[newIndex ];
  380. }
  381. if ( !pModel->m_TextureCoord.empty() ) {
  382. for ( size_t i=0; i < pMesh->GetNumUVChannels(); i++ ) {
  383. pMesh->mTextureCoords[ i ][ newIndex+1 ] = pMesh->mTextureCoords[ i ][ newIndex ];
  384. }
  385. }
  386. ++newIndex;
  387. }
  388. pDestFace[-1].mIndices[1] = newIndex;
  389. }
  390. }
  391. else if (last) {
  392. outIndex++;
  393. }
  394. ++newIndex;
  395. }
  396. }
  397. }
  398. // ------------------------------------------------------------------------------------------------
  399. // Counts all stored meshes
  400. void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes)
  401. {
  402. iNumMeshes = 0;
  403. if ( rObjects.empty() )
  404. return;
  405. iNumMeshes += static_cast<unsigned int>( rObjects.size() );
  406. for (std::vector<ObjFile::Object*>::const_iterator it = rObjects.begin();
  407. it != rObjects.end();
  408. ++it)
  409. {
  410. if (!(*it)->m_SubObjects.empty())
  411. {
  412. countObjects((*it)->m_SubObjects, iNumMeshes);
  413. }
  414. }
  415. }
  416. // ------------------------------------------------------------------------------------------------
  417. // Add clamp mode property to material if necessary
  418. void ObjFileImporter::addTextureMappingModeProperty(aiMaterial* mat, aiTextureType type, int clampMode)
  419. {
  420. ai_assert( NULL != mat);
  421. mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_U(type, 0));
  422. mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_V(type, 0));
  423. }
  424. // ------------------------------------------------------------------------------------------------
  425. // Creates the material
  426. void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene )
  427. {
  428. ai_assert( NULL != pScene );
  429. if ( NULL == pScene )
  430. return;
  431. const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size();
  432. pScene->mNumMaterials = 0;
  433. if ( pModel->m_MaterialLib.empty() ) {
  434. DefaultLogger::get()->debug("OBJ: no materials specified");
  435. return;
  436. }
  437. pScene->mMaterials = new aiMaterial*[ numMaterials ];
  438. for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
  439. {
  440. // Store material name
  441. std::map<std::string, ObjFile::Material*>::const_iterator it;
  442. it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] );
  443. // No material found, use the default material
  444. if ( pModel->m_MaterialMap.end() == it )
  445. continue;
  446. aiMaterial* mat = new aiMaterial;
  447. ObjFile::Material *pCurrentMaterial = (*it).second;
  448. mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME );
  449. // convert illumination model
  450. int sm = 0;
  451. switch (pCurrentMaterial->illumination_model)
  452. {
  453. case 0:
  454. sm = aiShadingMode_NoShading;
  455. break;
  456. case 1:
  457. sm = aiShadingMode_Gouraud;
  458. break;
  459. case 2:
  460. sm = aiShadingMode_Phong;
  461. break;
  462. default:
  463. sm = aiShadingMode_Gouraud;
  464. DefaultLogger::get()->error("OBJ: unexpected illumination model (0-2 recognized)");
  465. }
  466. mat->AddProperty<int>( &sm, 1, AI_MATKEY_SHADING_MODEL);
  467. // multiplying the specular exponent with 2 seems to yield better results
  468. pCurrentMaterial->shineness *= 4.f;
  469. // Adding material colors
  470. mat->AddProperty( &pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT );
  471. mat->AddProperty( &pCurrentMaterial->diffuse, 1, AI_MATKEY_COLOR_DIFFUSE );
  472. mat->AddProperty( &pCurrentMaterial->specular, 1, AI_MATKEY_COLOR_SPECULAR );
  473. mat->AddProperty( &pCurrentMaterial->emissive, 1, AI_MATKEY_COLOR_EMISSIVE );
  474. mat->AddProperty( &pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS );
  475. mat->AddProperty( &pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY );
  476. // Adding refraction index
  477. mat->AddProperty( &pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI );
  478. // Adding textures
  479. if ( 0 != pCurrentMaterial->texture.length )
  480. {
  481. mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
  482. if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType])
  483. {
  484. addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE);
  485. }
  486. }
  487. if ( 0 != pCurrentMaterial->textureAmbient.length )
  488. {
  489. mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
  490. if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType])
  491. {
  492. addTextureMappingModeProperty(mat, aiTextureType_AMBIENT);
  493. }
  494. }
  495. if ( 0 != pCurrentMaterial->textureEmissive.length )
  496. mat->AddProperty( &pCurrentMaterial->textureEmissive, AI_MATKEY_TEXTURE_EMISSIVE(0));
  497. if ( 0 != pCurrentMaterial->textureSpecular.length )
  498. {
  499. mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
  500. if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType])
  501. {
  502. addTextureMappingModeProperty(mat, aiTextureType_SPECULAR);
  503. }
  504. }
  505. if ( 0 != pCurrentMaterial->textureBump.length )
  506. {
  507. mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
  508. if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType])
  509. {
  510. addTextureMappingModeProperty(mat, aiTextureType_HEIGHT);
  511. }
  512. }
  513. if ( 0 != pCurrentMaterial->textureNormal.length )
  514. {
  515. mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
  516. if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType])
  517. {
  518. addTextureMappingModeProperty(mat, aiTextureType_NORMALS);
  519. }
  520. }
  521. if ( 0 != pCurrentMaterial->textureDisp.length )
  522. {
  523. mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
  524. if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType])
  525. {
  526. addTextureMappingModeProperty(mat, aiTextureType_DISPLACEMENT);
  527. }
  528. }
  529. if ( 0 != pCurrentMaterial->textureOpacity.length )
  530. {
  531. mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
  532. if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType])
  533. {
  534. addTextureMappingModeProperty(mat, aiTextureType_OPACITY);
  535. }
  536. }
  537. if ( 0 != pCurrentMaterial->textureSpecularity.length )
  538. {
  539. mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
  540. if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType])
  541. {
  542. addTextureMappingModeProperty(mat, aiTextureType_SHININESS);
  543. }
  544. }
  545. // Store material property info in material array in scene
  546. pScene->mMaterials[ pScene->mNumMaterials ] = mat;
  547. pScene->mNumMaterials++;
  548. }
  549. // Test number of created materials.
  550. ai_assert( pScene->mNumMaterials == numMaterials );
  551. }
  552. // ------------------------------------------------------------------------------------------------
  553. // Appends this node to the parent node
  554. void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
  555. {
  556. // Checking preconditions
  557. ai_assert( NULL != pParent );
  558. ai_assert( NULL != pChild );
  559. // Assign parent to child
  560. pChild->mParent = pParent;
  561. // If already children was assigned to the parent node, store them in a
  562. std::vector<aiNode*> temp;
  563. if (pParent->mChildren != NULL)
  564. {
  565. ai_assert( 0 != pParent->mNumChildren );
  566. for (size_t index = 0; index < pParent->mNumChildren; index++)
  567. {
  568. temp.push_back(pParent->mChildren [ index ] );
  569. }
  570. delete [] pParent->mChildren;
  571. }
  572. // Copy node instances into parent node
  573. pParent->mNumChildren++;
  574. pParent->mChildren = new aiNode*[ pParent->mNumChildren ];
  575. for (size_t index = 0; index < pParent->mNumChildren-1; index++)
  576. {
  577. pParent->mChildren[ index ] = temp [ index ];
  578. }
  579. pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
  580. }
  581. // ------------------------------------------------------------------------------------------------
  582. } // Namespace Assimp
  583. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER