ObjFileParser.cpp 24 KB

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