Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

759 строки
20 KiB

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