ObjFileParser.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 "ObjFileParser.h"
  37. #include "ObjFileMtlImporter.h"
  38. #include "ObjTools.h"
  39. #include "ObjFileData.h"
  40. #include "ParsingUtils.h"
  41. #include "../include/assimp/types.h"
  42. #include "DefaultIOSystem.h"
  43. namespace Assimp
  44. {
  45. // -------------------------------------------------------------------
  46. const std::string ObjFileParser::DEFAULT_MATERIAL = AI_DEFAULT_MATERIAL_NAME;
  47. // -------------------------------------------------------------------
  48. // Constructor with loaded data and directories.
  49. ObjFileParser::ObjFileParser(std::vector<char> &Data,const std::string &strModelName, IOSystem *io ) :
  50. m_DataIt(Data.begin()),
  51. m_DataItEnd(Data.end()),
  52. m_pModel(NULL),
  53. m_uiLine(0),
  54. m_pIO( io )
  55. {
  56. std::fill_n(m_buffer,BUFFERSIZE,0);
  57. // Create the model instance to store all the data
  58. m_pModel = new ObjFile::Model();
  59. m_pModel->m_ModelName = strModelName;
  60. m_pModel->m_pDefaultMaterial = new ObjFile::Material();
  61. m_pModel->m_pDefaultMaterial->MaterialName.Set( DEFAULT_MATERIAL );
  62. m_pModel->m_MaterialLib.push_back( DEFAULT_MATERIAL );
  63. m_pModel->m_MaterialMap[ DEFAULT_MATERIAL ] = m_pModel->m_pDefaultMaterial;
  64. // Start parsing the file
  65. parseFile();
  66. }
  67. // -------------------------------------------------------------------
  68. // Destructor
  69. ObjFileParser::~ObjFileParser()
  70. {
  71. /*delete m_pModel->m_pDefaultMaterial;
  72. m_pModel->m_pDefaultMaterial = NULL;*/
  73. delete m_pModel;
  74. m_pModel = NULL;
  75. }
  76. // -------------------------------------------------------------------
  77. // Returns a pointer to the model instance.
  78. ObjFile::Model *ObjFileParser::GetModel() const
  79. {
  80. return m_pModel;
  81. }
  82. // -------------------------------------------------------------------
  83. // File parsing method.
  84. void ObjFileParser::parseFile()
  85. {
  86. if (m_DataIt == m_DataItEnd)
  87. return;
  88. while (m_DataIt != m_DataItEnd)
  89. {
  90. switch (*m_DataIt)
  91. {
  92. case 'v': // Parse a vertex texture coordinate
  93. {
  94. ++m_DataIt;
  95. if (*m_DataIt == ' ')
  96. {
  97. // Read in vertex definition
  98. getVector3(m_pModel->m_Vertices);
  99. }
  100. else if (*m_DataIt == 't')
  101. {
  102. // Read in texture coordinate (2D)
  103. ++m_DataIt;
  104. getVector2(m_pModel->m_TextureCoord);
  105. }
  106. else if (*m_DataIt == 'n')
  107. {
  108. // Read in normal vector definition
  109. ++m_DataIt;
  110. getVector3( m_pModel->m_Normals );
  111. }
  112. }
  113. break;
  114. case 'p': // Parse a face, line or point statement
  115. case 'l':
  116. case 'f':
  117. {
  118. getFace(*m_DataIt == 'f' ? aiPrimitiveType_POLYGON : (*m_DataIt == 'l'
  119. ? aiPrimitiveType_LINE : aiPrimitiveType_POINT));
  120. }
  121. break;
  122. case '#': // Parse a comment
  123. {
  124. getComment();
  125. }
  126. break;
  127. case 'u': // Parse a material desc. setter
  128. {
  129. getMaterialDesc();
  130. }
  131. break;
  132. case 'm': // Parse a material library
  133. {
  134. getMaterialLib();
  135. }
  136. break;
  137. case 'g': // Parse group name
  138. {
  139. getGroupName();
  140. }
  141. break;
  142. case 's': // Parse group number
  143. {
  144. getGroupNumber();
  145. }
  146. break;
  147. case 'o': // Parse object name
  148. {
  149. getObjectName();
  150. }
  151. break;
  152. default:
  153. {
  154. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. // -------------------------------------------------------------------
  161. // Copy the next word in a temporary buffer
  162. void ObjFileParser::copyNextWord(char *pBuffer, size_t length)
  163. {
  164. size_t index = 0;
  165. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  166. while ( m_DataIt != m_DataItEnd && !isSeparator(*m_DataIt) )
  167. {
  168. pBuffer[index] = *m_DataIt;
  169. index++;
  170. if (index == length-1)
  171. break;
  172. ++m_DataIt;
  173. }
  174. pBuffer[index] = '\0';
  175. }
  176. // -------------------------------------------------------------------
  177. // Copy the next line into a temporary buffer
  178. void ObjFileParser::copyNextLine(char *pBuffer, size_t length)
  179. {
  180. size_t index = 0;
  181. while (m_DataIt != m_DataItEnd)
  182. {
  183. if (*m_DataIt == '\n' || *m_DataIt == '\r' || index == length-1)
  184. break;
  185. pBuffer[ index ] = *m_DataIt;
  186. ++index;
  187. ++m_DataIt;
  188. }
  189. pBuffer[ index ] = '\0';
  190. }
  191. // -------------------------------------------------------------------
  192. // Get values for a new 3D vector instance
  193. void ObjFileParser::getVector3(std::vector<aiVector3D> &point3d_array)
  194. {
  195. float x, y, z;
  196. copyNextWord(m_buffer, BUFFERSIZE);
  197. x = (float) fast_atof(m_buffer);
  198. copyNextWord(m_buffer, BUFFERSIZE);
  199. y = (float) fast_atof(m_buffer);
  200. copyNextWord(m_buffer, BUFFERSIZE);
  201. z = (float) fast_atof(m_buffer);
  202. point3d_array.push_back( aiVector3D( x, y, z ) );
  203. //skipLine();
  204. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  205. }
  206. // -------------------------------------------------------------------
  207. // Get values for a new 2D vector instance
  208. void ObjFileParser::getVector2( std::vector<aiVector2D> &point2d_array )
  209. {
  210. float x, y;
  211. copyNextWord(m_buffer, BUFFERSIZE);
  212. x = (float) fast_atof(m_buffer);
  213. copyNextWord(m_buffer, BUFFERSIZE);
  214. y = (float) fast_atof(m_buffer);
  215. point2d_array.push_back(aiVector2D(x, y));
  216. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  217. }
  218. // -------------------------------------------------------------------
  219. // Get values for a new face instance
  220. void ObjFileParser::getFace(aiPrimitiveType type)
  221. {
  222. copyNextLine(m_buffer, BUFFERSIZE);
  223. if (m_DataIt == m_DataItEnd)
  224. return;
  225. char *pPtr = m_buffer;
  226. char *pEnd = &pPtr[BUFFERSIZE];
  227. pPtr = getNextToken<char*>(pPtr, pEnd);
  228. if (pPtr == pEnd || *pPtr == '\0')
  229. return;
  230. std::vector<unsigned int> *pIndices = new std::vector<unsigned int>;
  231. std::vector<unsigned int> *pTexID = new std::vector<unsigned int>;
  232. std::vector<unsigned int> *pNormalID = new std::vector<unsigned int>;
  233. bool hasNormal = false;
  234. const bool vt = (!m_pModel->m_TextureCoord.empty());
  235. const bool vn = (!m_pModel->m_Normals.empty());
  236. int iStep = 0, iPos = 0;
  237. while (pPtr != pEnd)
  238. {
  239. iStep = 1;
  240. if (IsLineEnd(*pPtr))
  241. break;
  242. if (*pPtr=='/' )
  243. {
  244. if (type == aiPrimitiveType_POINT) {
  245. DefaultLogger::get()->error("Obj: Separator unexpected in point statement");
  246. }
  247. if (iPos == 0)
  248. {
  249. //if there are no texture coordinates in the file, but normals
  250. if (!vt && vn) {
  251. iPos = 1;
  252. iStep++;
  253. }
  254. }
  255. iPos++;
  256. }
  257. else if ( isSeparator(*pPtr) )
  258. {
  259. iPos = 0;
  260. }
  261. else
  262. {
  263. //OBJ USES 1 Base ARRAYS!!!!
  264. const int iVal = atoi( pPtr );
  265. int tmp = iVal;
  266. while ( ( tmp = tmp / 10 )!=0 )
  267. ++iStep;
  268. if ( iVal > 0 )
  269. {
  270. // Store parsed index
  271. if ( 0 == iPos )
  272. {
  273. pIndices->push_back( iVal-1 );
  274. }
  275. else if ( 1 == iPos )
  276. {
  277. pTexID->push_back( iVal-1 );
  278. }
  279. else if ( 2 == iPos )
  280. {
  281. pNormalID->push_back( iVal-1 );
  282. hasNormal = true;
  283. }
  284. else
  285. {
  286. reportErrorTokenInFace();
  287. }
  288. }
  289. }
  290. pPtr += iStep;
  291. }
  292. if ( pIndices->empty() )
  293. {
  294. DefaultLogger::get()->error("Obj: Ignoring empty face");
  295. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  296. return;
  297. }
  298. ObjFile::Face *face = new ObjFile::Face( pIndices, pNormalID, pTexID, type );
  299. // Set active material, if one set
  300. if (NULL != m_pModel->m_pCurrentMaterial)
  301. face->m_pMaterial = m_pModel->m_pCurrentMaterial;
  302. else
  303. face->m_pMaterial = m_pModel->m_pDefaultMaterial;
  304. // Create a default object, if nothing is there
  305. if ( NULL == m_pModel->m_pCurrent )
  306. createObject( "defaultobject" );
  307. // Assign face to mesh
  308. if ( NULL == m_pModel->m_pCurrentMesh )
  309. {
  310. createMesh();
  311. }
  312. // Store the face
  313. m_pModel->m_pCurrentMesh->m_Faces.push_back( face );
  314. m_pModel->m_pCurrentMesh->m_uiNumIndices += (unsigned int)face->m_pVertices->size();
  315. m_pModel->m_pCurrentMesh->m_uiUVCoordinates[ 0 ] += (unsigned int)face->m_pTexturCoords[0].size();
  316. if( !m_pModel->m_pCurrentMesh->m_hasNormals && hasNormal )
  317. {
  318. m_pModel->m_pCurrentMesh->m_hasNormals = true;
  319. }
  320. // Skip the rest of the line
  321. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  322. }
  323. // -------------------------------------------------------------------
  324. // Get values for a new material description
  325. void ObjFileParser::getMaterialDesc()
  326. {
  327. // Each material request a new object.
  328. // Sometimes the object is already created (see 'o' tag by example), but it is not initialized !
  329. // So, we create a new object only if the current on is already initialized !
  330. if (m_pModel->m_pCurrent != NULL &&
  331. ( m_pModel->m_pCurrent->m_Meshes.size() > 1 ||
  332. (m_pModel->m_pCurrent->m_Meshes.size() == 1 && m_pModel->m_Meshes[m_pModel->m_pCurrent->m_Meshes[0]]->m_Faces.size() != 0) )
  333. )
  334. m_pModel->m_pCurrent = NULL;
  335. // Get next data for material data
  336. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  337. if (m_DataIt == m_DataItEnd)
  338. return;
  339. char *pStart = &(*m_DataIt);
  340. while ( m_DataIt != m_DataItEnd && !isSeparator(*m_DataIt) )
  341. ++m_DataIt;
  342. // Get name
  343. std::string strName(pStart, &(*m_DataIt));
  344. if ( strName.empty())
  345. return;
  346. // Search for material
  347. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
  348. if ( it == m_pModel->m_MaterialMap.end() )
  349. {
  350. // Not found, use default material
  351. m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
  352. DefaultLogger::get()->error("OBJ: failed to locate material " + strName + ", skipping");
  353. }
  354. else
  355. {
  356. // Found, using detected material
  357. m_pModel->m_pCurrentMaterial = (*it).second;
  358. if ( needsNewMesh( strName ))
  359. {
  360. createMesh();
  361. }
  362. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strName );
  363. }
  364. // Skip rest of line
  365. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  366. }
  367. // -------------------------------------------------------------------
  368. // Get a comment, values will be skipped
  369. void ObjFileParser::getComment()
  370. {
  371. while (m_DataIt != m_DataItEnd)
  372. {
  373. if ( '\n' == (*m_DataIt))
  374. {
  375. ++m_DataIt;
  376. break;
  377. }
  378. else
  379. {
  380. ++m_DataIt;
  381. }
  382. }
  383. }
  384. // -------------------------------------------------------------------
  385. // Get material library from file.
  386. void ObjFileParser::getMaterialLib()
  387. {
  388. // Translate tuple
  389. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  390. if (m_DataIt == m_DataItEnd)
  391. return;
  392. char *pStart = &(*m_DataIt);
  393. while (m_DataIt != m_DataItEnd && !isNewLine(*m_DataIt))
  394. m_DataIt++;
  395. // Check for existence
  396. const std::string strMatName(pStart, &(*m_DataIt));
  397. IOStream *pFile = m_pIO->Open(strMatName);
  398. if (!pFile )
  399. {
  400. DefaultLogger::get()->error("OBJ: Unable to locate material file " + strMatName);
  401. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  402. return;
  403. }
  404. // Import material library data from file
  405. std::vector<char> buffer;
  406. BaseImporter::TextFileToBuffer(pFile,buffer);
  407. m_pIO->Close( pFile );
  408. // Importing the material library
  409. ObjFileMtlImporter mtlImporter( buffer, strMatName, m_pModel );
  410. }
  411. // -------------------------------------------------------------------
  412. // Set a new material definition as the current material.
  413. void ObjFileParser::getNewMaterial()
  414. {
  415. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  416. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  417. if ( m_DataIt == m_DataItEnd )
  418. return;
  419. char *pStart = &(*m_DataIt);
  420. std::string strMat( pStart, *m_DataIt );
  421. while ( m_DataIt != m_DataItEnd && isSeparator( *m_DataIt ) )
  422. m_DataIt++;
  423. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strMat );
  424. if ( it == m_pModel->m_MaterialMap.end() )
  425. {
  426. // Show a warning, if material was not found
  427. DefaultLogger::get()->warn("OBJ: Unsupported material requested: " + strMat);
  428. m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
  429. }
  430. else
  431. {
  432. // Set new material
  433. if ( needsNewMesh( strMat ) )
  434. {
  435. createMesh();
  436. }
  437. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strMat );
  438. }
  439. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  440. }
  441. // -------------------------------------------------------------------
  442. int ObjFileParser::getMaterialIndex( const std::string &strMaterialName )
  443. {
  444. int mat_index = -1;
  445. if ( strMaterialName.empty() )
  446. return mat_index;
  447. for (size_t index = 0; index < m_pModel->m_MaterialLib.size(); ++index)
  448. {
  449. if ( strMaterialName == m_pModel->m_MaterialLib[ index ])
  450. {
  451. mat_index = (int)index;
  452. break;
  453. }
  454. }
  455. return mat_index;
  456. }
  457. // -------------------------------------------------------------------
  458. // Getter for a group name.
  459. void ObjFileParser::getGroupName()
  460. {
  461. // Get next word from data buffer
  462. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  463. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  464. if ( isEndOfBuffer( m_DataIt, m_DataItEnd ) )
  465. return;
  466. // Store the group name in the group library
  467. char *pStart = &(*m_DataIt);
  468. while ( m_DataIt != m_DataItEnd && !isSeparator(*m_DataIt) )
  469. m_DataIt++;
  470. std::string strGroupName( pStart, &(*m_DataIt) );
  471. // Change active group, if necessary
  472. if ( m_pModel->m_strActiveGroup != strGroupName )
  473. {
  474. // Search for already existing entry
  475. ObjFile::Model::ConstGroupMapIt it = m_pModel->m_Groups.find(strGroupName);
  476. // We are mapping groups into the object structure
  477. createObject( strGroupName );
  478. // New group name, creating a new entry
  479. if (it == m_pModel->m_Groups.end())
  480. {
  481. std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
  482. m_pModel->m_Groups[ strGroupName ] = pFaceIDArray;
  483. m_pModel->m_pGroupFaceIDs = (pFaceIDArray);
  484. }
  485. else
  486. {
  487. m_pModel->m_pGroupFaceIDs = (*it).second;
  488. }
  489. m_pModel->m_strActiveGroup = strGroupName;
  490. }
  491. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  492. }
  493. // -------------------------------------------------------------------
  494. // Not supported
  495. void ObjFileParser::getGroupNumber()
  496. {
  497. // Not used
  498. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  499. }
  500. // -------------------------------------------------------------------
  501. // Stores values for a new object instance, name will be used to
  502. // identify it.
  503. void ObjFileParser::getObjectName()
  504. {
  505. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  506. if (m_DataIt == m_DataItEnd)
  507. return;
  508. char *pStart = &(*m_DataIt);
  509. while ( m_DataIt != m_DataItEnd && !isSeparator( *m_DataIt ) )
  510. ++m_DataIt;
  511. std::string strObjectName(pStart, &(*m_DataIt));
  512. if (!strObjectName.empty())
  513. {
  514. // Reset current object
  515. m_pModel->m_pCurrent = NULL;
  516. // Search for actual object
  517. for (std::vector<ObjFile::Object*>::const_iterator it = m_pModel->m_Objects.begin();
  518. it != m_pModel->m_Objects.end();
  519. ++it)
  520. {
  521. if ((*it)->m_strObjName == strObjectName)
  522. {
  523. m_pModel->m_pCurrent = *it;
  524. break;
  525. }
  526. }
  527. // Allocate a new object, if current one was not found before
  528. if ( NULL == m_pModel->m_pCurrent )
  529. createObject(strObjectName);
  530. }
  531. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  532. }
  533. // -------------------------------------------------------------------
  534. // Creates a new object instance
  535. void ObjFileParser::createObject(const std::string &strObjectName)
  536. {
  537. ai_assert( NULL != m_pModel );
  538. //ai_assert( !strObjectName.empty() );
  539. m_pModel->m_pCurrent = new ObjFile::Object;
  540. m_pModel->m_pCurrent->m_strObjName = strObjectName;
  541. m_pModel->m_Objects.push_back( m_pModel->m_pCurrent );
  542. createMesh();
  543. if( m_pModel->m_pCurrentMaterial )
  544. {
  545. m_pModel->m_pCurrentMesh->m_uiMaterialIndex =
  546. getMaterialIndex( m_pModel->m_pCurrentMaterial->MaterialName.data );
  547. m_pModel->m_pCurrentMesh->m_pMaterial = m_pModel->m_pCurrentMaterial;
  548. }
  549. }
  550. // -------------------------------------------------------------------
  551. // Creates a new mesh
  552. void ObjFileParser::createMesh()
  553. {
  554. ai_assert( NULL != m_pModel );
  555. m_pModel->m_pCurrentMesh = new ObjFile::Mesh;
  556. m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
  557. unsigned int meshId = m_pModel->m_Meshes.size()-1;
  558. if ( NULL != m_pModel->m_pCurrent )
  559. {
  560. m_pModel->m_pCurrent->m_Meshes.push_back( meshId );
  561. }
  562. else
  563. {
  564. DefaultLogger::get()->error("OBJ: No object detected to attach a new mesh instance.");
  565. }
  566. }
  567. // -------------------------------------------------------------------
  568. // Returns true, if a new mesh must be created.
  569. bool ObjFileParser::needsNewMesh( const std::string &rMaterialName )
  570. {
  571. if(m_pModel->m_pCurrentMesh == 0)
  572. {
  573. // No mesh data yet
  574. return true;
  575. }
  576. bool newMat = false;
  577. int matIdx = getMaterialIndex( rMaterialName );
  578. int curMatIdx = m_pModel->m_pCurrentMesh->m_uiMaterialIndex;
  579. if ( curMatIdx != int(ObjFile::Mesh::NoMaterial) || curMatIdx != matIdx )
  580. {
  581. // New material -> only one material per mesh, so we need to create a new
  582. // material
  583. newMat = true;
  584. }
  585. return newMat;
  586. }
  587. // -------------------------------------------------------------------
  588. // Shows an error in parsing process.
  589. void ObjFileParser::reportErrorTokenInFace()
  590. {
  591. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  592. DefaultLogger::get()->error("OBJ: Not supported token in face description detected");
  593. }
  594. // -------------------------------------------------------------------
  595. } // Namespace Assimp
  596. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER