ObjFileParser.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 &modelName, 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 = modelName;
  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( "defaultobject" );
  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. }
  409. char *pStart = &(*m_DataIt);
  410. while( m_DataIt != m_DataItEnd && !IsLineEnd( *m_DataIt ) ) {
  411. ++m_DataIt;
  412. }
  413. // Get name
  414. std::string strName(pStart, &(*m_DataIt));
  415. if ( strName.empty())
  416. return;
  417. // Search for material
  418. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
  419. if ( it == m_pModel->m_MaterialMap.end() ) {
  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. } else {
  424. // Found, using detected material
  425. m_pModel->m_pCurrentMaterial = (*it).second;
  426. if ( needsNewMesh( strName ))
  427. {
  428. createMesh( strName );
  429. }
  430. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strName );
  431. }
  432. // Skip rest of line
  433. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  434. }
  435. // -------------------------------------------------------------------
  436. // Get a comment, values will be skipped
  437. void ObjFileParser::getComment()
  438. {
  439. while (m_DataIt != m_DataItEnd)
  440. {
  441. if ( '\n' == (*m_DataIt))
  442. {
  443. ++m_DataIt;
  444. break;
  445. }
  446. else
  447. {
  448. ++m_DataIt;
  449. }
  450. }
  451. }
  452. // -------------------------------------------------------------------
  453. // Get material library from file.
  454. void ObjFileParser::getMaterialLib()
  455. {
  456. // Translate tuple
  457. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  458. if( m_DataIt == m_DataItEnd ) {
  459. return;
  460. }
  461. char *pStart = &(*m_DataIt);
  462. while( m_DataIt != m_DataItEnd && !IsLineEnd( *m_DataIt ) ) {
  463. ++m_DataIt;
  464. }
  465. // Check for existence
  466. const std::string strMatName(pStart, &(*m_DataIt));
  467. std::string absName;
  468. if ( m_pIO->StackSize() > 0 ) {
  469. const std::string &path = m_pIO->CurrentDirectory();
  470. absName = path + strMatName;
  471. } else {
  472. absName = strMatName;
  473. }
  474. //IOStream *pFile = m_pIO->Open( strMatName );
  475. IOStream *pFile = m_pIO->Open( absName );
  476. if (!pFile )
  477. {
  478. DefaultLogger::get()->error("OBJ: Unable to locate material file " + strMatName );
  479. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  480. return;
  481. }
  482. // Import material library data from file
  483. std::vector<char> buffer;
  484. BaseImporter::TextFileToBuffer( pFile, buffer );
  485. m_pIO->Close( pFile );
  486. // Importing the material library
  487. ObjFileMtlImporter mtlImporter( buffer, strMatName, m_pModel );
  488. }
  489. // -------------------------------------------------------------------
  490. // Set a new material definition as the current material.
  491. void ObjFileParser::getNewMaterial()
  492. {
  493. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  494. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  495. if( m_DataIt == m_DataItEnd ) {
  496. return;
  497. }
  498. char *pStart = &(*m_DataIt);
  499. std::string strMat( pStart, *m_DataIt );
  500. while( m_DataIt != m_DataItEnd && IsSpaceOrNewLine( *m_DataIt ) ) {
  501. ++m_DataIt;
  502. }
  503. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strMat );
  504. if ( it == m_pModel->m_MaterialMap.end() )
  505. {
  506. // Show a warning, if material was not found
  507. DefaultLogger::get()->warn("OBJ: Unsupported material requested: " + strMat);
  508. m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
  509. }
  510. else
  511. {
  512. // Set new material
  513. if ( needsNewMesh( strMat ) )
  514. {
  515. createMesh( strMat );
  516. }
  517. m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strMat );
  518. }
  519. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  520. }
  521. // -------------------------------------------------------------------
  522. int ObjFileParser::getMaterialIndex( const std::string &strMaterialName )
  523. {
  524. int mat_index = -1;
  525. if( strMaterialName.empty() ) {
  526. return mat_index;
  527. }
  528. for (size_t index = 0; index < m_pModel->m_MaterialLib.size(); ++index)
  529. {
  530. if ( strMaterialName == m_pModel->m_MaterialLib[ index ])
  531. {
  532. mat_index = (int)index;
  533. break;
  534. }
  535. }
  536. return mat_index;
  537. }
  538. // -------------------------------------------------------------------
  539. // Getter for a group name.
  540. void ObjFileParser::getGroupName()
  541. {
  542. std::string strGroupName;
  543. m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, strGroupName);
  544. if( isEndOfBuffer( m_DataIt, m_DataItEnd ) ) {
  545. return;
  546. }
  547. // Change active group, if necessary
  548. if ( m_pModel->m_strActiveGroup != strGroupName )
  549. {
  550. // Search for already existing entry
  551. ObjFile::Model::ConstGroupMapIt it = m_pModel->m_Groups.find(strGroupName);
  552. // We are mapping groups into the object structure
  553. createObject( strGroupName );
  554. // New group name, creating a new entry
  555. if (it == m_pModel->m_Groups.end())
  556. {
  557. std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
  558. m_pModel->m_Groups[ strGroupName ] = pFaceIDArray;
  559. m_pModel->m_pGroupFaceIDs = (pFaceIDArray);
  560. }
  561. else
  562. {
  563. m_pModel->m_pGroupFaceIDs = (*it).second;
  564. }
  565. m_pModel->m_strActiveGroup = strGroupName;
  566. }
  567. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  568. }
  569. // -------------------------------------------------------------------
  570. // Not supported
  571. void ObjFileParser::getGroupNumber()
  572. {
  573. // Not used
  574. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  575. }
  576. // -------------------------------------------------------------------
  577. // Not supported
  578. void ObjFileParser::getGroupNumberAndResolution()
  579. {
  580. // Not used
  581. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  582. }
  583. // -------------------------------------------------------------------
  584. // Stores values for a new object instance, name will be used to
  585. // identify it.
  586. void ObjFileParser::getObjectName()
  587. {
  588. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  589. if( m_DataIt == m_DataItEnd ) {
  590. return;
  591. }
  592. char *pStart = &(*m_DataIt);
  593. while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
  594. ++m_DataIt;
  595. }
  596. std::string strObjectName(pStart, &(*m_DataIt));
  597. if (!strObjectName.empty())
  598. {
  599. // Reset current object
  600. m_pModel->m_pCurrent = NULL;
  601. // Search for actual object
  602. for (std::vector<ObjFile::Object*>::const_iterator it = m_pModel->m_Objects.begin();
  603. it != m_pModel->m_Objects.end();
  604. ++it)
  605. {
  606. if ((*it)->m_strObjName == strObjectName)
  607. {
  608. m_pModel->m_pCurrent = *it;
  609. break;
  610. }
  611. }
  612. // Allocate a new object, if current one was not found before
  613. if( NULL == m_pModel->m_pCurrent ) {
  614. createObject( strObjectName );
  615. }
  616. }
  617. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  618. }
  619. // -------------------------------------------------------------------
  620. // Creates a new object instance
  621. void ObjFileParser::createObject(const std::string &objName)
  622. {
  623. ai_assert( NULL != m_pModel );
  624. m_pModel->m_pCurrent = new ObjFile::Object;
  625. m_pModel->m_pCurrent->m_strObjName = objName;
  626. m_pModel->m_Objects.push_back( m_pModel->m_pCurrent );
  627. createMesh( objName );
  628. if( m_pModel->m_pCurrentMaterial )
  629. {
  630. m_pModel->m_pCurrentMesh->m_uiMaterialIndex =
  631. getMaterialIndex( m_pModel->m_pCurrentMaterial->MaterialName.data );
  632. m_pModel->m_pCurrentMesh->m_pMaterial = m_pModel->m_pCurrentMaterial;
  633. }
  634. }
  635. // -------------------------------------------------------------------
  636. // Creates a new mesh
  637. void ObjFileParser::createMesh( const std::string &meshName )
  638. {
  639. ai_assert( NULL != m_pModel );
  640. m_pModel->m_pCurrentMesh = new ObjFile::Mesh( meshName );
  641. m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
  642. unsigned int meshId = m_pModel->m_Meshes.size()-1;
  643. if ( NULL != m_pModel->m_pCurrent )
  644. {
  645. m_pModel->m_pCurrent->m_Meshes.push_back( meshId );
  646. }
  647. else
  648. {
  649. DefaultLogger::get()->error("OBJ: No object detected to attach a new mesh instance.");
  650. }
  651. }
  652. // -------------------------------------------------------------------
  653. // Returns true, if a new mesh must be created.
  654. bool ObjFileParser::needsNewMesh( const std::string &rMaterialName )
  655. {
  656. if(m_pModel->m_pCurrentMesh == 0)
  657. {
  658. // No mesh data yet
  659. return true;
  660. }
  661. bool newMat = false;
  662. int matIdx = getMaterialIndex( rMaterialName );
  663. int curMatIdx = m_pModel->m_pCurrentMesh->m_uiMaterialIndex;
  664. if ( curMatIdx != int(ObjFile::Mesh::NoMaterial) || curMatIdx != matIdx )
  665. {
  666. // New material -> only one material per mesh, so we need to create a new
  667. // material
  668. newMat = true;
  669. }
  670. return newMat;
  671. }
  672. // -------------------------------------------------------------------
  673. // Shows an error in parsing process.
  674. void ObjFileParser::reportErrorTokenInFace()
  675. {
  676. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  677. DefaultLogger::get()->error("OBJ: Not supported token in face description detected");
  678. }
  679. // -------------------------------------------------------------------
  680. } // Namespace Assimp
  681. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER