ObjFileParser.cpp 24 KB

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