ObjFileImporter.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2015, 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. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  35. #include "DefaultIOSystem.h"
  36. #include "ObjFileImporter.h"
  37. #include "ObjFileParser.h"
  38. #include "ObjFileData.h"
  39. #include <boost/scoped_ptr.hpp>
  40. #include "../include/assimp/Importer.hpp"
  41. #include "../include/assimp/scene.h"
  42. #include "../include/assimp/ai_assert.h"
  43. #include "../include/assimp/DefaultLogger.hpp"
  44. static const aiImporterDesc desc = {
  45. "Wavefront Object Importer",
  46. "",
  47. "",
  48. "surfaces not supported",
  49. aiImporterFlags_SupportTextFlavour,
  50. 0,
  51. 0,
  52. 0,
  53. 0,
  54. "obj"
  55. };
  56. static const unsigned int ObjMinSize = 16;
  57. namespace Assimp {
  58. using namespace std;
  59. // ------------------------------------------------------------------------------------------------
  60. // Default constructor
  61. ObjFileImporter::ObjFileImporter() :
  62. m_Buffer(),
  63. m_pRootObject( NULL ),
  64. m_strAbsPath( "" )
  65. {
  66. DefaultIOSystem io;
  67. m_strAbsPath = io.getOsSeparator();
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. // Destructor.
  71. ObjFileImporter::~ObjFileImporter()
  72. {
  73. delete m_pRootObject;
  74. m_pRootObject = NULL;
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Returns true, if file is an obj file.
  78. bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler , bool checkSig ) const
  79. {
  80. if(!checkSig) //Check File Extension
  81. {
  82. return SimpleExtensionCheck(pFile,"obj");
  83. }
  84. else //Check file Header
  85. {
  86. static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " };
  87. return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9 );
  88. }
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. const aiImporterDesc* ObjFileImporter::GetInfo () const
  92. {
  93. return &desc;
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // Obj-file import implementation
  97. void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  98. {
  99. // Read file into memory
  100. const std::string mode = "rb";
  101. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, mode));
  102. if( !file.get() ) {
  103. throw DeadlyImportError( "Failed to open file " + pFile + "." );
  104. }
  105. // Get the file-size and validate it, throwing an exception when fails
  106. size_t fileSize = file->FileSize();
  107. if( fileSize < ObjMinSize ) {
  108. throw DeadlyImportError( "OBJ-file is too small.");
  109. }
  110. // Allocate buffer and read file into it
  111. TextFileToBuffer(file.get(),m_Buffer);
  112. // Get the model name
  113. std::string strModelName;
  114. std::string::size_type pos = pFile.find_last_of( "\\/" );
  115. if ( pos != std::string::npos )
  116. {
  117. strModelName = pFile.substr(pos+1, pFile.size() - pos - 1);
  118. }
  119. else
  120. {
  121. strModelName = pFile;
  122. }
  123. // process all '\'
  124. std::vector<char> ::iterator iter = m_Buffer.begin();
  125. while (iter != m_Buffer.end())
  126. {
  127. if (*iter == '\\')
  128. {
  129. // remove '\'
  130. iter = m_Buffer.erase(iter);
  131. // remove next character
  132. while (*iter == '\r' || *iter == '\n')
  133. iter = m_Buffer.erase(iter);
  134. }
  135. else
  136. ++iter;
  137. }
  138. // parse the file into a temporary representation
  139. ObjFileParser parser(m_Buffer, strModelName, pIOHandler);
  140. // And create the proper return structures out of it
  141. CreateDataFromImport(parser.GetModel(), pScene);
  142. // Clean up allocated storage for the next import
  143. m_Buffer.clear();
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. // Create the data from parsed obj-file
  147. void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene) {
  148. if( 0L == pModel ) {
  149. return;
  150. }
  151. // Create the root node of the scene
  152. pScene->mRootNode = new aiNode;
  153. if ( !pModel->m_ModelName.empty() )
  154. {
  155. // Set the name of the scene
  156. pScene->mRootNode->mName.Set(pModel->m_ModelName);
  157. }
  158. else
  159. {
  160. // This is a fatal error, so break down the application
  161. ai_assert(false);
  162. }
  163. // Create nodes for the whole scene
  164. std::vector<aiMesh*> MeshArray;
  165. for (size_t index = 0; index < pModel->m_Objects.size(); index++)
  166. {
  167. createNodes(pModel, pModel->m_Objects[ index ], pScene->mRootNode, pScene, MeshArray);
  168. }
  169. // Create mesh pointer buffer for this scene
  170. if (pScene->mNumMeshes > 0)
  171. {
  172. pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
  173. for (size_t index =0; index < MeshArray.size(); index++)
  174. {
  175. pScene->mMeshes[ index ] = MeshArray[ index ];
  176. }
  177. }
  178. // Create all materials
  179. createMaterials( pModel, pScene );
  180. }
  181. // ------------------------------------------------------------------------------------------------
  182. // Creates all nodes of the model
  183. aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pObject,
  184. aiNode *pParent, aiScene* pScene,
  185. std::vector<aiMesh*> &MeshArray )
  186. {
  187. ai_assert( NULL != pModel );
  188. if( NULL == pObject ) {
  189. return NULL;
  190. }
  191. // Store older mesh size to be able to computes mesh offsets for new mesh instances
  192. const size_t oldMeshSize = MeshArray.size();
  193. aiNode *pNode = new aiNode;
  194. pNode->mName = pObject->m_strObjName;
  195. // If we have a parent node, store it
  196. if( pParent != NULL ) {
  197. appendChildToParentNode( pParent, pNode );
  198. }
  199. for ( size_t i=0; i< pObject->m_Meshes.size(); i++ )
  200. {
  201. unsigned int meshId = pObject->m_Meshes[ i ];
  202. aiMesh *pMesh = createTopology( pModel, pObject, meshId );
  203. if( pMesh && pMesh->mNumFaces > 0 ) {
  204. MeshArray.push_back( pMesh );
  205. }
  206. }
  207. // Create all nodes from the sub-objects stored in the current object
  208. if ( !pObject->m_SubObjects.empty() )
  209. {
  210. size_t numChilds = pObject->m_SubObjects.size();
  211. pNode->mNumChildren = static_cast<unsigned int>( numChilds );
  212. pNode->mChildren = new aiNode*[ numChilds ];
  213. pNode->mNumMeshes = 1;
  214. pNode->mMeshes = new unsigned int[ 1 ];
  215. }
  216. // Set mesh instances into scene- and node-instances
  217. const size_t meshSizeDiff = MeshArray.size()- oldMeshSize;
  218. if ( meshSizeDiff > 0 )
  219. {
  220. pNode->mMeshes = new unsigned int[ meshSizeDiff ];
  221. pNode->mNumMeshes = static_cast<unsigned int>( meshSizeDiff );
  222. size_t index = 0;
  223. for (size_t i = oldMeshSize; i < MeshArray.size(); i++)
  224. {
  225. pNode->mMeshes[ index ] = pScene->mNumMeshes;
  226. pScene->mNumMeshes++;
  227. index++;
  228. }
  229. }
  230. return pNode;
  231. }
  232. // ------------------------------------------------------------------------------------------------
  233. // Create topology data
  234. aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const ObjFile::Object* pData,
  235. unsigned int meshIndex )
  236. {
  237. // Checking preconditions
  238. ai_assert( NULL != pModel );
  239. if( NULL == pData ) {
  240. return NULL;
  241. }
  242. // Create faces
  243. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ meshIndex ];
  244. if( !pObjMesh ) {
  245. return NULL;
  246. }
  247. ai_assert( NULL != pObjMesh );
  248. aiMesh* pMesh = new aiMesh;
  249. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  250. {
  251. ObjFile::Face *const inp = pObjMesh->m_Faces[ index ];
  252. ai_assert( NULL != inp );
  253. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  254. pMesh->mNumFaces += inp->m_pVertices->size() - 1;
  255. pMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  256. } else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  257. pMesh->mNumFaces += inp->m_pVertices->size();
  258. pMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  259. } else {
  260. ++pMesh->mNumFaces;
  261. if (inp->m_pVertices->size() > 3) {
  262. pMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  263. } else {
  264. pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  265. }
  266. }
  267. }
  268. unsigned int uiIdxCount( 0u );
  269. if ( pMesh->mNumFaces > 0 )
  270. {
  271. pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
  272. if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial )
  273. {
  274. pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
  275. }
  276. unsigned int outIndex( 0 );
  277. // Copy all data from all stored meshes
  278. for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
  279. {
  280. ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
  281. if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
  282. for(size_t i = 0; i < inp->m_pVertices->size() - 1; ++i) {
  283. aiFace& f = pMesh->mFaces[ outIndex++ ];
  284. uiIdxCount += f.mNumIndices = 2;
  285. f.mIndices = new unsigned int[2];
  286. }
  287. continue;
  288. }
  289. else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
  290. for(size_t i = 0; i < inp->m_pVertices->size(); ++i) {
  291. aiFace& f = pMesh->mFaces[ outIndex++ ];
  292. uiIdxCount += f.mNumIndices = 1;
  293. f.mIndices = new unsigned int[1];
  294. }
  295. continue;
  296. }
  297. aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
  298. const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
  299. uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
  300. if (pFace->mNumIndices > 0) {
  301. pFace->mIndices = new unsigned int[ uiNumIndices ];
  302. }
  303. }
  304. }
  305. // Create mesh vertices
  306. createVertexArray(pModel, pData, meshIndex, pMesh, uiIdxCount);
  307. return pMesh;
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. // Creates a vertex array
  311. void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
  312. const ObjFile::Object* pCurrentObject,
  313. unsigned int uiMeshIndex,
  314. aiMesh* pMesh,
  315. unsigned int numIndices)
  316. {
  317. // Checking preconditions
  318. ai_assert( NULL != pCurrentObject );
  319. // Break, if no faces are stored in object
  320. if ( pCurrentObject->m_Meshes.empty() )
  321. return;
  322. // Get current mesh
  323. ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
  324. if ( NULL == pObjMesh || pObjMesh->m_uiNumIndices < 1)
  325. return;
  326. // Copy vertices of this mesh instance
  327. pMesh->mNumVertices = numIndices;
  328. pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
  329. // Allocate buffer for normal vectors
  330. if ( !pModel->m_Normals.empty() && pObjMesh->m_hasNormals )
  331. pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
  332. // Allocate buffer for texture coordinates
  333. if ( !pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0] )
  334. {
  335. pMesh->mNumUVComponents[ 0 ] = 2;
  336. pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
  337. }
  338. // Copy vertices, normals and textures into aiMesh instance
  339. unsigned int newIndex = 0, outIndex = 0;
  340. for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ )
  341. {
  342. // Get source face
  343. ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ];
  344. // Copy all index arrays
  345. for ( size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ )
  346. {
  347. const unsigned int vertex = pSourceFace->m_pVertices->at( vertexIndex );
  348. if ( vertex >= pModel->m_Vertices.size() )
  349. throw DeadlyImportError( "OBJ: vertex index out of range" );
  350. pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
  351. // Copy all normals
  352. if ( !pModel->m_Normals.empty() && vertexIndex < pSourceFace->m_pNormals->size())
  353. {
  354. const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
  355. if ( normal >= pModel->m_Normals.size() )
  356. throw DeadlyImportError("OBJ: vertex normal index out of range");
  357. pMesh->mNormals[ newIndex ] = pModel->m_Normals[ normal ];
  358. }
  359. // Copy all texture coordinates
  360. if ( !pModel->m_TextureCoord.empty() && vertexIndex < pSourceFace->m_pTexturCoords->size())
  361. {
  362. const unsigned int tex = pSourceFace->m_pTexturCoords->at( vertexIndex );
  363. ai_assert( tex < pModel->m_TextureCoord.size() );
  364. if ( tex >= pModel->m_TextureCoord.size() )
  365. throw DeadlyImportError("OBJ: texture coordinate index out of range");
  366. const aiVector3D &coord3d = pModel->m_TextureCoord[ tex ];
  367. pMesh->mTextureCoords[ 0 ][ newIndex ] = aiVector3D( coord3d.x, coord3d.y, coord3d.z );
  368. }
  369. if ( pMesh->mNumVertices <= newIndex ) {
  370. throw DeadlyImportError("OBJ: bad vertex index");
  371. }
  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