ObjFileParser.cpp 20 KB

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