ObjFileParser.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 == ' ' || *m_DataIt == '\t')
  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. ai_assert(index < length);
  175. pBuffer[index] = '\0';
  176. }
  177. // -------------------------------------------------------------------
  178. // Copy the next line into a temporary buffer
  179. void ObjFileParser::copyNextLine(char *pBuffer, size_t length)
  180. {
  181. size_t index = 0u;
  182. // some OBJ files have line continuations using \ (such as in C++ et al)
  183. bool continuation = false;
  184. for (;m_DataIt != m_DataItEnd && index < length-1; ++m_DataIt)
  185. {
  186. const char c = *m_DataIt;
  187. if (c == '\\') {
  188. continuation = true;
  189. continue;
  190. }
  191. if (c == '\n' || c == '\r') {
  192. if(continuation) {
  193. pBuffer[ index++ ] = ' ';
  194. continue;
  195. }
  196. break;
  197. }
  198. continuation = false;
  199. pBuffer[ index++ ] = c;
  200. }
  201. ai_assert(index < length);
  202. pBuffer[ index ] = '\0';
  203. }
  204. // -------------------------------------------------------------------
  205. // Get values for a new 3D vector instance
  206. void ObjFileParser::getVector3(std::vector<aiVector3D> &point3d_array)
  207. {
  208. float x, y, z;
  209. copyNextWord(m_buffer, BUFFERSIZE);
  210. x = (float) fast_atof(m_buffer);
  211. copyNextWord(m_buffer, BUFFERSIZE);
  212. y = (float) fast_atof(m_buffer);
  213. copyNextWord(m_buffer, BUFFERSIZE);
  214. z = (float) fast_atof(m_buffer);
  215. point3d_array.push_back( aiVector3D( x, y, z ) );
  216. //skipLine();
  217. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  218. }
  219. // -------------------------------------------------------------------
  220. // Get values for a new 2D vector instance
  221. void ObjFileParser::getVector2( std::vector<aiVector2D> &point2d_array )
  222. {
  223. float x, y;
  224. copyNextWord(m_buffer, BUFFERSIZE);
  225. x = (float) fast_atof(m_buffer);
  226. copyNextWord(m_buffer, BUFFERSIZE);
  227. y = (float) fast_atof(m_buffer);
  228. point2d_array.push_back(aiVector2D(x, y));
  229. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  230. }
  231. // -------------------------------------------------------------------
  232. // Get values for a new face instance
  233. void ObjFileParser::getFace(aiPrimitiveType type)
  234. {
  235. copyNextLine(m_buffer, BUFFERSIZE);
  236. if (m_DataIt == m_DataItEnd)
  237. return;
  238. char *pPtr = m_buffer;
  239. char *pEnd = &pPtr[BUFFERSIZE];
  240. pPtr = getNextToken<char*>(pPtr, pEnd);
  241. if (pPtr == pEnd || *pPtr == '\0')
  242. return;
  243. std::vector<unsigned int> *pIndices = new std::vector<unsigned int>;
  244. std::vector<unsigned int> *pTexID = new std::vector<unsigned int>;
  245. std::vector<unsigned int> *pNormalID = new std::vector<unsigned int>;
  246. bool hasNormal = false;
  247. const bool vt = (!m_pModel->m_TextureCoord.empty());
  248. const bool vn = (!m_pModel->m_Normals.empty());
  249. int iStep = 0, iPos = 0;
  250. while (pPtr != pEnd)
  251. {
  252. iStep = 1;
  253. if (IsLineEnd(*pPtr))
  254. break;
  255. if (*pPtr=='/' )
  256. {
  257. if (type == aiPrimitiveType_POINT) {
  258. DefaultLogger::get()->error("Obj: Separator unexpected in point statement");
  259. }
  260. if (iPos == 0)
  261. {
  262. //if there are no texture coordinates in the file, but normals
  263. if (!vt && vn) {
  264. iPos = 1;
  265. iStep++;
  266. }
  267. }
  268. iPos++;
  269. }
  270. else if ( isSeparator(*pPtr) )
  271. {
  272. iPos = 0;
  273. }
  274. else
  275. {
  276. //OBJ USES 1 Base ARRAYS!!!!
  277. const int iVal = atoi( pPtr );
  278. int tmp = iVal;
  279. while ( ( tmp = tmp / 10 )!=0 )
  280. ++iStep;
  281. if ( iVal > 0 )
  282. {
  283. // Store parsed index
  284. if ( 0 == iPos )
  285. {
  286. pIndices->push_back( iVal-1 );
  287. }
  288. else if ( 1 == iPos )
  289. {
  290. pTexID->push_back( iVal-1 );
  291. }
  292. else if ( 2 == iPos )
  293. {
  294. pNormalID->push_back( iVal-1 );
  295. hasNormal = true;
  296. }
  297. else
  298. {
  299. reportErrorTokenInFace();
  300. }
  301. }
  302. }
  303. pPtr += iStep;
  304. }
  305. if ( pIndices->empty() )
  306. {
  307. DefaultLogger::get()->error("Obj: Ignoring empty face");
  308. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  309. return;
  310. }
  311. ObjFile::Face *face = new ObjFile::Face( pIndices, pNormalID, pTexID, type );
  312. // Set active material, if one set
  313. if (NULL != m_pModel->m_pCurrentMaterial)
  314. face->m_pMaterial = m_pModel->m_pCurrentMaterial;
  315. else
  316. face->m_pMaterial = m_pModel->m_pDefaultMaterial;
  317. // Create a default object, if nothing is there
  318. if ( NULL == m_pModel->m_pCurrent )
  319. createObject( "defaultobject" );
  320. // Assign face to mesh
  321. if ( NULL == m_pModel->m_pCurrentMesh )
  322. {
  323. createMesh();
  324. }
  325. // Store the face
  326. m_pModel->m_pCurrentMesh->m_Faces.push_back( face );
  327. m_pModel->m_pCurrentMesh->m_uiNumIndices += (unsigned int)face->m_pVertices->size();
  328. m_pModel->m_pCurrentMesh->m_uiUVCoordinates[ 0 ] += (unsigned int)face->m_pTexturCoords[0].size();
  329. if( !m_pModel->m_pCurrentMesh->m_hasNormals && hasNormal )
  330. {
  331. m_pModel->m_pCurrentMesh->m_hasNormals = true;
  332. }
  333. // Skip the rest of the line
  334. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  335. }
  336. // -------------------------------------------------------------------
  337. // Get values for a new material description
  338. void ObjFileParser::getMaterialDesc()
  339. {
  340. // Each material request a new object.
  341. // Sometimes the object is already created (see 'o' tag by example), but it is not initialized !
  342. // So, we create a new object only if the current on is already initialized !
  343. if (m_pModel->m_pCurrent != NULL &&
  344. ( m_pModel->m_pCurrent->m_Meshes.size() > 1 ||
  345. (m_pModel->m_pCurrent->m_Meshes.size() == 1 && m_pModel->m_Meshes[m_pModel->m_pCurrent->m_Meshes[0]]->m_Faces.size() != 0) )
  346. )
  347. m_pModel->m_pCurrent = NULL;
  348. // Get next data for material data
  349. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  350. if (m_DataIt == m_DataItEnd)
  351. return;
  352. char *pStart = &(*m_DataIt);
  353. while ( m_DataIt != m_DataItEnd && !isSeparator(*m_DataIt) )
  354. ++m_DataIt;
  355. // Get name
  356. std::string strName(pStart, &(*m_DataIt));
  357. if ( strName.empty())
  358. return;
  359. // Search for material
  360. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
  361. if ( it == m_pModel->m_MaterialMap.end() )
  362. {
  363. // Not found, use default material
  364. m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
  365. DefaultLogger::get()->error("OBJ: failed to locate material " + strName + ", skipping");
  366. }
  367. else
  368. {
  369. // Found, using detected material
  370. m_pModel->m_pCurrentMaterial = (*it).second;
  371. if ( needsNewMesh( strName ))
  372. {
  373. createMesh();
  374. }
  375. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strName );
  376. }
  377. // Skip rest of line
  378. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  379. }
  380. // -------------------------------------------------------------------
  381. // Get a comment, values will be skipped
  382. void ObjFileParser::getComment()
  383. {
  384. while (m_DataIt != m_DataItEnd)
  385. {
  386. if ( '\n' == (*m_DataIt))
  387. {
  388. ++m_DataIt;
  389. break;
  390. }
  391. else
  392. {
  393. ++m_DataIt;
  394. }
  395. }
  396. }
  397. // -------------------------------------------------------------------
  398. // Get material library from file.
  399. void ObjFileParser::getMaterialLib()
  400. {
  401. // Translate tuple
  402. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  403. if (m_DataIt == m_DataItEnd)
  404. return;
  405. char *pStart = &(*m_DataIt);
  406. while (m_DataIt != m_DataItEnd && !isNewLine(*m_DataIt))
  407. m_DataIt++;
  408. // Check for existence
  409. const std::string strMatName(pStart, &(*m_DataIt));
  410. IOStream *pFile = m_pIO->Open(strMatName);
  411. if (!pFile )
  412. {
  413. DefaultLogger::get()->error("OBJ: Unable to locate material file " + strMatName);
  414. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  415. return;
  416. }
  417. // Import material library data from file
  418. std::vector<char> buffer;
  419. BaseImporter::TextFileToBuffer(pFile,buffer);
  420. m_pIO->Close( pFile );
  421. // Importing the material library
  422. ObjFileMtlImporter mtlImporter( buffer, strMatName, m_pModel );
  423. }
  424. // -------------------------------------------------------------------
  425. // Set a new material definition as the current material.
  426. void ObjFileParser::getNewMaterial()
  427. {
  428. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  429. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  430. if ( m_DataIt == m_DataItEnd )
  431. return;
  432. char *pStart = &(*m_DataIt);
  433. std::string strMat( pStart, *m_DataIt );
  434. while ( m_DataIt != m_DataItEnd && isSeparator( *m_DataIt ) )
  435. m_DataIt++;
  436. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strMat );
  437. if ( it == m_pModel->m_MaterialMap.end() )
  438. {
  439. // Show a warning, if material was not found
  440. DefaultLogger::get()->warn("OBJ: Unsupported material requested: " + strMat);
  441. m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
  442. }
  443. else
  444. {
  445. // Set new material
  446. if ( needsNewMesh( strMat ) )
  447. {
  448. createMesh();
  449. }
  450. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strMat );
  451. }
  452. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  453. }
  454. // -------------------------------------------------------------------
  455. int ObjFileParser::getMaterialIndex( const std::string &strMaterialName )
  456. {
  457. int mat_index = -1;
  458. if ( strMaterialName.empty() )
  459. return mat_index;
  460. for (size_t index = 0; index < m_pModel->m_MaterialLib.size(); ++index)
  461. {
  462. if ( strMaterialName == m_pModel->m_MaterialLib[ index ])
  463. {
  464. mat_index = (int)index;
  465. break;
  466. }
  467. }
  468. return mat_index;
  469. }
  470. // -------------------------------------------------------------------
  471. // Getter for a group name.
  472. void ObjFileParser::getGroupName()
  473. {
  474. std::string strGroupName;
  475. m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, strGroupName);
  476. if ( isEndOfBuffer( m_DataIt, m_DataItEnd ) )
  477. return;
  478. // Change active group, if necessary
  479. if ( m_pModel->m_strActiveGroup != strGroupName )
  480. {
  481. // Search for already existing entry
  482. ObjFile::Model::ConstGroupMapIt it = m_pModel->m_Groups.find(strGroupName);
  483. // We are mapping groups into the object structure
  484. createObject( strGroupName );
  485. // New group name, creating a new entry
  486. if (it == m_pModel->m_Groups.end())
  487. {
  488. std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
  489. m_pModel->m_Groups[ strGroupName ] = pFaceIDArray;
  490. m_pModel->m_pGroupFaceIDs = (pFaceIDArray);
  491. }
  492. else
  493. {
  494. m_pModel->m_pGroupFaceIDs = (*it).second;
  495. }
  496. m_pModel->m_strActiveGroup = strGroupName;
  497. }
  498. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  499. }
  500. // -------------------------------------------------------------------
  501. // Not supported
  502. void ObjFileParser::getGroupNumber()
  503. {
  504. // Not used
  505. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  506. }
  507. // -------------------------------------------------------------------
  508. // Stores values for a new object instance, name will be used to
  509. // identify it.
  510. void ObjFileParser::getObjectName()
  511. {
  512. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  513. if (m_DataIt == m_DataItEnd)
  514. return;
  515. char *pStart = &(*m_DataIt);
  516. while ( m_DataIt != m_DataItEnd && !isSeparator( *m_DataIt ) )
  517. ++m_DataIt;
  518. std::string strObjectName(pStart, &(*m_DataIt));
  519. if (!strObjectName.empty())
  520. {
  521. // Reset current object
  522. m_pModel->m_pCurrent = NULL;
  523. // Search for actual object
  524. for (std::vector<ObjFile::Object*>::const_iterator it = m_pModel->m_Objects.begin();
  525. it != m_pModel->m_Objects.end();
  526. ++it)
  527. {
  528. if ((*it)->m_strObjName == strObjectName)
  529. {
  530. m_pModel->m_pCurrent = *it;
  531. break;
  532. }
  533. }
  534. // Allocate a new object, if current one was not found before
  535. if ( NULL == m_pModel->m_pCurrent )
  536. createObject(strObjectName);
  537. }
  538. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  539. }
  540. // -------------------------------------------------------------------
  541. // Creates a new object instance
  542. void ObjFileParser::createObject(const std::string &strObjectName)
  543. {
  544. ai_assert( NULL != m_pModel );
  545. //ai_assert( !strObjectName.empty() );
  546. m_pModel->m_pCurrent = new ObjFile::Object;
  547. m_pModel->m_pCurrent->m_strObjName = strObjectName;
  548. m_pModel->m_Objects.push_back( m_pModel->m_pCurrent );
  549. createMesh();
  550. if( m_pModel->m_pCurrentMaterial )
  551. {
  552. m_pModel->m_pCurrentMesh->m_uiMaterialIndex =
  553. getMaterialIndex( m_pModel->m_pCurrentMaterial->MaterialName.data );
  554. m_pModel->m_pCurrentMesh->m_pMaterial = m_pModel->m_pCurrentMaterial;
  555. }
  556. }
  557. // -------------------------------------------------------------------
  558. // Creates a new mesh
  559. void ObjFileParser::createMesh()
  560. {
  561. ai_assert( NULL != m_pModel );
  562. m_pModel->m_pCurrentMesh = new ObjFile::Mesh;
  563. m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
  564. unsigned int meshId = m_pModel->m_Meshes.size()-1;
  565. if ( NULL != m_pModel->m_pCurrent )
  566. {
  567. m_pModel->m_pCurrent->m_Meshes.push_back( meshId );
  568. }
  569. else
  570. {
  571. DefaultLogger::get()->error("OBJ: No object detected to attach a new mesh instance.");
  572. }
  573. }
  574. // -------------------------------------------------------------------
  575. // Returns true, if a new mesh must be created.
  576. bool ObjFileParser::needsNewMesh( const std::string &rMaterialName )
  577. {
  578. if(m_pModel->m_pCurrentMesh == 0)
  579. {
  580. // No mesh data yet
  581. return true;
  582. }
  583. bool newMat = false;
  584. int matIdx = getMaterialIndex( rMaterialName );
  585. int curMatIdx = m_pModel->m_pCurrentMesh->m_uiMaterialIndex;
  586. if ( curMatIdx != int(ObjFile::Mesh::NoMaterial) || curMatIdx != matIdx )
  587. {
  588. // New material -> only one material per mesh, so we need to create a new
  589. // material
  590. newMat = true;
  591. }
  592. return newMat;
  593. }
  594. // -------------------------------------------------------------------
  595. // Shows an error in parsing process.
  596. void ObjFileParser::reportErrorTokenInFace()
  597. {
  598. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  599. DefaultLogger::get()->error("OBJ: Not supported token in face description detected");
  600. }
  601. // -------------------------------------------------------------------
  602. } // Namespace Assimp
  603. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER