ObjFileImporter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. // process all '\'
  121. std::vector<char> ::iterator iter = m_Buffer.begin();
  122. while (iter != m_Buffer.end())
  123. {
  124. if (*iter == '\\')
  125. {
  126. // remove '\'
  127. iter = m_Buffer.erase(iter);
  128. // remove next character
  129. while (*iter == '\r' || *iter == '\n')
  130. iter = m_Buffer.erase(iter);
  131. }
  132. else
  133. ++iter;
  134. }
  135. // parse the file into a temporary representation
  136. ObjFileParser parser(m_Buffer, strModelName, pIOHandler);
  137. // And create the proper return structures out of it
  138. CreateDataFromImport(parser.GetModel(), pScene);
  139. // Clean up allocated storage for the next import
  140. m_Buffer.clear();
  141. }
  142. // ------------------------------------------------------------------------------------------------
  143. // Create the data from parsed obj-file
  144. void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene) {
  145. if( 0L == pModel ) {
  146. return;
  147. }
  148. // Create the root node of the scene
  149. pScene->mRootNode = new aiNode;
  150. if ( !pModel->m_ModelName.empty() )
  151. {
  152. // Set the name of the scene
  153. pScene->mRootNode->mName.Set(pModel->m_ModelName);
  154. }
  155. else
  156. {
  157. // This is a fatal error, so break down the application
  158. ai_assert(false);
  159. }
  160. // Create nodes for the whole scene
  161. std::vector<aiMesh*> MeshArray;
  162. for (size_t index = 0; index < pModel->m_Objects.size(); index++)
  163. {
  164. createNodes(pModel, pModel->m_Objects[ index ], pScene->mRootNode, pScene, MeshArray);
  165. }
  166. // Create mesh pointer buffer for this scene
  167. if (pScene->mNumMeshes > 0)
  168. {
  169. pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
  170. for (size_t index =0; index < MeshArray.size(); index++)
  171. {
  172. pScene->mMeshes [ index ] = MeshArray[ index ];
  173. }
  174. }
  175. // Create all materials
  176. createMaterials( pModel, pScene );
  177. }
  178. // ------------------------------------------------------------------------------------------------
  179. // Creates all nodes of the model
  180. aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pObject,
  181. aiNode *pParent, aiScene* pScene,
  182. std::vector<aiMesh*> &MeshArray )
  183. {
  184. ai_assert( NULL != pModel );
  185. if( NULL == pObject ) {
  186. return NULL;
  187. }
  188. // Store older mesh size to be able to computes mesh offsets for new mesh instances
  189. const size_t oldMeshSize = MeshArray.size();
  190. aiNode *pNode = new aiNode;
  191. pNode->mName = pObject->m_strObjName;
  192. // If we have a parent node, store it
  193. if( pParent != NULL ) {
  194. appendChildToParentNode( pParent, pNode );
  195. }
  196. for ( unsigned int i=0; i< pObject->m_Meshes.size(); i++ )
  197. {
  198. unsigned int meshId = pObject->m_Meshes[ i ];
  199. aiMesh *pMesh = new aiMesh;
  200. createTopology( pModel, pObject, meshId, pMesh );
  201. if ( pMesh->mNumVertices > 0 )
  202. {
  203. MeshArray.push_back( pMesh );
  204. }
  205. else
  206. {
  207. delete pMesh;
  208. }
  209. }
  210. // Create all nodes from the sub-objects stored in the current object
  211. if ( !pObject->m_SubObjects.empty() )
  212. {
  213. size_t numChilds = pObject->m_SubObjects.size();
  214. pNode->mNumChildren = static_cast<unsigned int>( numChilds );
  215. pNode->mChildren = new aiNode*[ numChilds ];
  216. pNode->mNumMeshes = 1;
  217. pNode->mMeshes = new unsigned int[ 1 ];
  218. }
  219. // Set mesh instances into scene- and node-instances
  220. const size_t meshSizeDiff = MeshArray.size()- oldMeshSize;
  221. if ( meshSizeDiff > 0 )
  222. {
  223. pNode->mMeshes = new unsigned int[ meshSizeDiff ];
  224. pNode->mNumMeshes = static_cast<unsigned int>( meshSizeDiff );
  225. size_t index = 0;
  226. for (size_t i = oldMeshSize; i < MeshArray.size(); i++)
  227. {
  228. pNode->mMeshes[ index ] = pScene->mNumMeshes;
  229. pScene->mNumMeshes++;
  230. index++;
  231. }
  232. }
  233. return pNode;
  234. }
  235. // ------------------------------------------------------------------------------------------------
  236. // Create topology data
  237. void ObjFileImporter::createTopology(const ObjFile::Model* pModel,
  238. const ObjFile::Object* pData,
  239. unsigned int uiMeshIndex,
  240. aiMesh* pMesh )
  241. {
  242. // Checking preconditions
  243. ai_assert( NULL != pModel );
  244. if( NULL == pData ) {
  245. return;
  246. }
  247. // Create faces
  248. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
  249. ai_assert( NULL != pObjMesh );
  250. pMesh->mNumFaces = 0;
  251. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  252. {
  253. ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
  254. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  255. pMesh->mNumFaces += inp->m_pVertices->size() - 1;
  256. pMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  257. }
  258. else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  259. pMesh->mNumFaces += inp->m_pVertices->size();
  260. pMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  261. } else {
  262. ++pMesh->mNumFaces;
  263. if (inp->m_pVertices->size() > 3) {
  264. pMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  265. }
  266. else {
  267. pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  268. }
  269. }
  270. }
  271. unsigned int uiIdxCount = 0u;
  272. if ( pMesh->mNumFaces > 0 )
  273. {
  274. pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
  275. if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial )
  276. {
  277. pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
  278. }
  279. unsigned int outIndex = 0;
  280. // Copy all data from all stored meshes
  281. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  282. {
  283. ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
  284. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  285. for(size_t i = 0; i < inp->m_pVertices->size() - 1; ++i) {
  286. aiFace& f = pMesh->mFaces[ outIndex++ ];
  287. uiIdxCount += f.mNumIndices = 2;
  288. f.mIndices = new unsigned int[2];
  289. }
  290. continue;
  291. }
  292. else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  293. for(size_t i = 0; i < inp->m_pVertices->size(); ++i) {
  294. aiFace& f = pMesh->mFaces[ outIndex++ ];
  295. uiIdxCount += f.mNumIndices = 1;
  296. f.mIndices = new unsigned int[1];
  297. }
  298. continue;
  299. }
  300. aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
  301. const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
  302. uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
  303. if (pFace->mNumIndices > 0) {
  304. pFace->mIndices = new unsigned int[ uiNumIndices ];
  305. }
  306. }
  307. }
  308. // Create mesh vertices
  309. createVertexArray(pModel, pData, uiMeshIndex, pMesh, uiIdxCount);
  310. }
  311. // ------------------------------------------------------------------------------------------------
  312. // Creates a vertex array
  313. void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
  314. const ObjFile::Object* pCurrentObject,
  315. unsigned int uiMeshIndex,
  316. aiMesh* pMesh,
  317. unsigned int uiIdxCount)
  318. {
  319. // Checking preconditions
  320. ai_assert( NULL != pCurrentObject );
  321. // Break, if no faces are stored in object
  322. if ( pCurrentObject->m_Meshes.empty() )
  323. return;
  324. // Get current mesh
  325. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
  326. if ( NULL == pObjMesh || pObjMesh->m_uiNumIndices < 1)
  327. return;
  328. // Copy vertices of this mesh instance
  329. pMesh->mNumVertices = uiIdxCount;
  330. pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
  331. // Allocate buffer for normal vectors
  332. if ( !pModel->m_Normals.empty() && pObjMesh->m_hasNormals )
  333. pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
  334. // Allocate buffer for texture coordinates
  335. if ( !pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0] )
  336. {
  337. pMesh->mNumUVComponents[ 0 ] = 2;
  338. pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
  339. }
  340. // Copy vertices, normals and textures into aiMesh instance
  341. unsigned int newIndex = 0, outIndex = 0;
  342. for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ )
  343. {
  344. // Get source face
  345. ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ];
  346. // Copy all index arrays
  347. for ( size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ )
  348. {
  349. const unsigned int vertex = pSourceFace->m_pVertices->at( vertexIndex );
  350. if ( vertex >= pModel->m_Vertices.size() )
  351. throw DeadlyImportError( "OBJ: vertex index out of range" );
  352. pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
  353. // Copy all normals
  354. if ( !pModel->m_Normals.empty() && vertexIndex < pSourceFace->m_pNormals->size())
  355. {
  356. const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
  357. if ( normal >= pModel->m_Normals.size() )
  358. throw DeadlyImportError("OBJ: vertex normal index out of range");
  359. pMesh->mNormals[ newIndex ] = pModel->m_Normals[ normal ];
  360. }
  361. // Copy all texture coordinates
  362. if ( !pModel->m_TextureCoord.empty() && vertexIndex < pSourceFace->m_pTexturCoords->size())
  363. {
  364. const unsigned int tex = pSourceFace->m_pTexturCoords->at( vertexIndex );
  365. ai_assert( tex < pModel->m_TextureCoord.size() );
  366. if ( tex >= pModel->m_TextureCoord.size() )
  367. throw DeadlyImportError("OBJ: texture coordinate index out of range");
  368. const aiVector3D &coord3d = pModel->m_TextureCoord[ tex ];
  369. pMesh->mTextureCoords[ 0 ][ newIndex ] = aiVector3D( coord3d.x, coord3d.y, coord3d.z );
  370. }
  371. ai_assert( pMesh->mNumVertices > newIndex );
  372. // Get destination face
  373. aiFace *pDestFace = &pMesh->mFaces[ outIndex ];
  374. const bool last = ( vertexIndex == pSourceFace->m_pVertices->size() - 1 );
  375. if (pSourceFace->m_PrimitiveType != aiPrimitiveType_LINE || !last)
  376. {
  377. pDestFace->mIndices[ outVertexIndex ] = newIndex;
  378. outVertexIndex++;
  379. }
  380. if (pSourceFace->m_PrimitiveType == aiPrimitiveType_POINT)
  381. {
  382. outIndex++;
  383. outVertexIndex = 0;
  384. }
  385. else if (pSourceFace->m_PrimitiveType == aiPrimitiveType_LINE)
  386. {
  387. outVertexIndex = 0;
  388. if(!last)
  389. outIndex++;
  390. if (vertexIndex) {
  391. if(!last) {
  392. pMesh->mVertices[ newIndex+1 ] = pMesh->mVertices[ newIndex ];
  393. if ( !pSourceFace->m_pNormals->empty() && !pModel->m_Normals.empty()) {
  394. pMesh->mNormals[ newIndex+1 ] = pMesh->mNormals[newIndex ];
  395. }
  396. if ( !pModel->m_TextureCoord.empty() ) {
  397. for ( size_t i=0; i < pMesh->GetNumUVChannels(); i++ ) {
  398. pMesh->mTextureCoords[ i ][ newIndex+1 ] = pMesh->mTextureCoords[ i ][ newIndex ];
  399. }
  400. }
  401. ++newIndex;
  402. }
  403. pDestFace[-1].mIndices[1] = newIndex;
  404. }
  405. }
  406. else if (last) {
  407. outIndex++;
  408. }
  409. ++newIndex;
  410. }
  411. }
  412. }
  413. // ------------------------------------------------------------------------------------------------
  414. // Counts all stored meshes
  415. void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes)
  416. {
  417. iNumMeshes = 0;
  418. if ( rObjects.empty() )
  419. return;
  420. iNumMeshes += static_cast<unsigned int>( rObjects.size() );
  421. for (std::vector<ObjFile::Object*>::const_iterator it = rObjects.begin();
  422. it != rObjects.end();
  423. ++it)
  424. {
  425. if (!(*it)->m_SubObjects.empty())
  426. {
  427. countObjects((*it)->m_SubObjects, iNumMeshes);
  428. }
  429. }
  430. }
  431. // ------------------------------------------------------------------------------------------------
  432. // Add clamp mode property to material if necessary
  433. void ObjFileImporter::addTextureMappingModeProperty(aiMaterial* mat, aiTextureType type, int clampMode)
  434. {
  435. ai_assert( NULL != mat);
  436. mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_U(type, 0));
  437. mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_V(type, 0));
  438. }
  439. // ------------------------------------------------------------------------------------------------
  440. // Creates the material
  441. void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene )
  442. {
  443. ai_assert( NULL != pScene );
  444. if ( NULL == pScene )
  445. return;
  446. const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size();
  447. pScene->mNumMaterials = 0;
  448. if ( pModel->m_MaterialLib.empty() ) {
  449. DefaultLogger::get()->debug("OBJ: no materials specified");
  450. return;
  451. }
  452. pScene->mMaterials = new aiMaterial*[ numMaterials ];
  453. for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
  454. {
  455. // Store material name
  456. std::map<std::string, ObjFile::Material*>::const_iterator it;
  457. it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] );
  458. // No material found, use the default material
  459. if ( pModel->m_MaterialMap.end() == it )
  460. continue;
  461. aiMaterial* mat = new aiMaterial;
  462. ObjFile::Material *pCurrentMaterial = (*it).second;
  463. mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME );
  464. // convert illumination model
  465. int sm = 0;
  466. switch (pCurrentMaterial->illumination_model)
  467. {
  468. case 0:
  469. sm = aiShadingMode_NoShading;
  470. break;
  471. case 1:
  472. sm = aiShadingMode_Gouraud;
  473. break;
  474. case 2:
  475. sm = aiShadingMode_Phong;
  476. break;
  477. default:
  478. sm = aiShadingMode_Gouraud;
  479. DefaultLogger::get()->error("OBJ: unexpected illumination model (0-2 recognized)");
  480. }
  481. mat->AddProperty<int>( &sm, 1, AI_MATKEY_SHADING_MODEL);
  482. // multiplying the specular exponent with 2 seems to yield better results
  483. pCurrentMaterial->shineness *= 4.f;
  484. // Adding material colors
  485. mat->AddProperty( &pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT );
  486. mat->AddProperty( &pCurrentMaterial->diffuse, 1, AI_MATKEY_COLOR_DIFFUSE );
  487. mat->AddProperty( &pCurrentMaterial->specular, 1, AI_MATKEY_COLOR_SPECULAR );
  488. mat->AddProperty( &pCurrentMaterial->emissive, 1, AI_MATKEY_COLOR_EMISSIVE );
  489. mat->AddProperty( &pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS );
  490. mat->AddProperty( &pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY );
  491. // Adding refraction index
  492. mat->AddProperty( &pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI );
  493. // Adding textures
  494. if ( 0 != pCurrentMaterial->texture.length )
  495. {
  496. mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
  497. if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType])
  498. {
  499. addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE);
  500. }
  501. }
  502. if ( 0 != pCurrentMaterial->textureAmbient.length )
  503. {
  504. mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
  505. if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType])
  506. {
  507. addTextureMappingModeProperty(mat, aiTextureType_AMBIENT);
  508. }
  509. }
  510. if ( 0 != pCurrentMaterial->textureEmissive.length )
  511. mat->AddProperty( &pCurrentMaterial->textureEmissive, AI_MATKEY_TEXTURE_EMISSIVE(0));
  512. if ( 0 != pCurrentMaterial->textureSpecular.length )
  513. {
  514. mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
  515. if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType])
  516. {
  517. addTextureMappingModeProperty(mat, aiTextureType_SPECULAR);
  518. }
  519. }
  520. if ( 0 != pCurrentMaterial->textureBump.length )
  521. {
  522. mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
  523. if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType])
  524. {
  525. addTextureMappingModeProperty(mat, aiTextureType_HEIGHT);
  526. }
  527. }
  528. if ( 0 != pCurrentMaterial->textureNormal.length )
  529. {
  530. mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
  531. if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType])
  532. {
  533. addTextureMappingModeProperty(mat, aiTextureType_NORMALS);
  534. }
  535. }
  536. if ( 0 != pCurrentMaterial->textureDisp.length )
  537. {
  538. mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
  539. if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType])
  540. {
  541. addTextureMappingModeProperty(mat, aiTextureType_DISPLACEMENT);
  542. }
  543. }
  544. if ( 0 != pCurrentMaterial->textureOpacity.length )
  545. {
  546. mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
  547. if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType])
  548. {
  549. addTextureMappingModeProperty(mat, aiTextureType_OPACITY);
  550. }
  551. }
  552. if ( 0 != pCurrentMaterial->textureSpecularity.length )
  553. {
  554. mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
  555. if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType])
  556. {
  557. addTextureMappingModeProperty(mat, aiTextureType_SHININESS);
  558. }
  559. }
  560. // Store material property info in material array in scene
  561. pScene->mMaterials[ pScene->mNumMaterials ] = mat;
  562. pScene->mNumMaterials++;
  563. }
  564. // Test number of created materials.
  565. ai_assert( pScene->mNumMaterials == numMaterials );
  566. }
  567. // ------------------------------------------------------------------------------------------------
  568. // Appends this node to the parent node
  569. void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
  570. {
  571. // Checking preconditions
  572. ai_assert( NULL != pParent );
  573. ai_assert( NULL != pChild );
  574. // Assign parent to child
  575. pChild->mParent = pParent;
  576. // If already children was assigned to the parent node, store them in a
  577. std::vector<aiNode*> temp;
  578. if (pParent->mChildren != NULL)
  579. {
  580. ai_assert( 0 != pParent->mNumChildren );
  581. for (size_t index = 0; index < pParent->mNumChildren; index++)
  582. {
  583. temp.push_back(pParent->mChildren [ index ] );
  584. }
  585. delete [] pParent->mChildren;
  586. }
  587. // Copy node instances into parent node
  588. pParent->mNumChildren++;
  589. pParent->mChildren = new aiNode*[ pParent->mNumChildren ];
  590. for (size_t index = 0; index < pParent->mNumChildren-1; index++)
  591. {
  592. pParent->mChildren[ index ] = temp [ index ];
  593. }
  594. pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
  595. }
  596. // ------------------------------------------------------------------------------------------------
  597. } // Namespace Assimp
  598. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER