ObjFileParser.cpp 21 KB

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