ObjFileParser.cpp 25 KB

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