ObjFileParser.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include "ObjFileParser.h"
  2. #include "ObjFileMtlImporter.h"
  3. #include "ObjTools.h"
  4. #include "ObjFileData.h"
  5. #include "DefaultIOSystem.h"
  6. #include "../include/IOStream.h"
  7. #include "../include/aiTypes.h"
  8. #include "../include/aiAssert.h"
  9. #include "fast_atof.h"
  10. #include <iostream>
  11. #include <vector>
  12. #include <cassert>
  13. namespace Assimp
  14. {
  15. // -------------------------------------------------------------------
  16. ObjFileParser::ObjFileParser(std::vector<char> &Data,
  17. const std::string &strAbsPath,
  18. const std::string &strModelName) :
  19. m_strAbsPath(strAbsPath),
  20. m_DataIt(Data.begin()),
  21. m_DataItEnd(Data.end()),
  22. m_pModel(NULL),
  23. m_uiLine(0)
  24. {
  25. // Create the model instance to store all the data
  26. m_pModel = new ObjFile::Model();
  27. m_pModel->m_ModelName = strModelName;
  28. // Start parsing the file
  29. parseFile();
  30. }
  31. // -------------------------------------------------------------------
  32. ObjFileParser::~ObjFileParser()
  33. {
  34. // empty
  35. }
  36. // -------------------------------------------------------------------
  37. ObjFile::Model *ObjFileParser::GetModel() const
  38. {
  39. return m_pModel;
  40. }
  41. // -------------------------------------------------------------------
  42. void ObjFileParser::parseFile()
  43. {
  44. if (m_DataIt == m_DataItEnd)
  45. return;
  46. while (m_DataIt != m_DataItEnd)
  47. {
  48. switch (*m_DataIt)
  49. {
  50. case 'v': // Parse a vertex texture coordinate
  51. {
  52. ++m_DataIt;
  53. if (*m_DataIt == ' ')
  54. {
  55. // Read in vertex definition
  56. getVector3(m_pModel->m_Vertices);
  57. }
  58. else if (*m_DataIt == 't')
  59. {
  60. // Read in texture coordinate (2D)
  61. getVector2(m_pModel->m_TextureCoord);
  62. }
  63. else if (*m_DataIt == 'n')
  64. {
  65. // Read in normal vector definition
  66. getVector3(m_pModel->m_Normals);
  67. }
  68. }
  69. break;
  70. case 'f': // Parse a face
  71. {
  72. getFace();
  73. }
  74. break;
  75. case '#': // Parse a comment
  76. {
  77. getComment();
  78. }
  79. break;
  80. case 'u': // Parse a material desc. setter
  81. {
  82. getMaterialDesc();
  83. }
  84. break;
  85. case 'm': // Parse a material library
  86. {
  87. getMaterialLib();
  88. }
  89. break;
  90. case 'g': // Parse group name
  91. {
  92. getGroupName();
  93. }
  94. break;
  95. case 's': // Parse group number
  96. {
  97. getGroupNumber();
  98. }
  99. break;
  100. case 'o': // Parse object name
  101. {
  102. getObjectName();
  103. }
  104. break;
  105. default:
  106. {
  107. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  108. }
  109. break;
  110. }
  111. }
  112. }
  113. // -------------------------------------------------------------------
  114. // Copy the next word in a temporary buffer
  115. void ObjFileParser::copyNextWord(char *pBuffer, size_t length)
  116. {
  117. size_t index = 0;
  118. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  119. while (!isSpace(*m_DataIt) && m_DataIt != m_DataItEnd)
  120. {
  121. pBuffer[index] = *m_DataIt;
  122. index++;
  123. if (index == length-1)
  124. break;
  125. ++m_DataIt;
  126. }
  127. pBuffer[index] = '\0';
  128. }
  129. // -------------------------------------------------------------------
  130. // Copy the next line into a temporary buffer
  131. void ObjFileParser::copyNextLine(char *pBuffer, size_t length)
  132. {
  133. size_t index = 0;
  134. while (m_DataIt != m_DataItEnd)
  135. {
  136. if (*m_DataIt == '\n' || *m_DataIt == '\r')
  137. break;
  138. assert (index+1 <= length);
  139. pBuffer[ index ] = *m_DataIt;
  140. ++index;
  141. ++m_DataIt;
  142. }
  143. pBuffer[ index ] = '\0';
  144. }
  145. // -------------------------------------------------------------------
  146. // Get values for a new 3D vector instance
  147. void ObjFileParser::getVector3(std::vector<aiVector3D*> &point3d_array)
  148. {
  149. float x, y, z;
  150. copyNextWord(m_buffer, BUFFERSIZE);
  151. x = (float) fast_atof(m_buffer);
  152. copyNextWord(m_buffer, BUFFERSIZE);
  153. y = (float) fast_atof(m_buffer);
  154. copyNextWord(m_buffer, BUFFERSIZE);
  155. z = (float) fast_atof(m_buffer);
  156. point3d_array.push_back(new aiVector3D(x,y,z));
  157. //skipLine();
  158. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  159. }
  160. // -------------------------------------------------------------------
  161. // Get values for a new 2D vector instance
  162. void ObjFileParser::getVector2( std::vector<aiVector2D*> &point2d_array )
  163. {
  164. float x, y;
  165. copyNextWord(m_buffer, BUFFERSIZE);
  166. x = (float) fast_atof(m_buffer);
  167. copyNextWord(m_buffer, BUFFERSIZE);
  168. y = (float) fast_atof(m_buffer);
  169. point2d_array.push_back(new aiVector2D(x, y));
  170. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  171. }
  172. // -------------------------------------------------------------------
  173. // Get values for a new face instance
  174. void ObjFileParser::getFace()
  175. {
  176. copyNextLine(m_buffer, BUFFERSIZE);
  177. if (m_DataIt == m_DataItEnd)
  178. return;
  179. char *pPtr = m_buffer;
  180. char *pEnd = &pPtr[BUFFERSIZE];
  181. pPtr = getNextToken<char*>(pPtr, pEnd);
  182. if (pPtr == '\0')
  183. return;
  184. std::vector<unsigned int> *pIndices = new std::vector<unsigned int>;
  185. std::vector<unsigned int> *pTexID = new std::vector<unsigned int>;
  186. std::vector<unsigned int> *pNormalID = new std::vector<unsigned int>;
  187. bool vt = (!m_pModel->m_TextureCoord.empty());
  188. bool vn = (!m_pModel->m_Normals.empty());
  189. int iStep = 0, iPos = 0;
  190. while (pPtr != pEnd)
  191. {
  192. iStep = 1;
  193. if (*pPtr == '\0')
  194. break;
  195. if (*pPtr=='\r')
  196. break;
  197. if (*pPtr=='/' )
  198. {
  199. if (iPos == 0)
  200. {
  201. //if there are no texturecoordinates in the obj file but normals
  202. if (!vt && vn)
  203. iPos = 1;
  204. }
  205. iPos++;
  206. }
  207. else if (isSpace(*pPtr))
  208. {
  209. iPos = 0;
  210. }
  211. else
  212. {
  213. //OBJ USES 1 Base ARRAYS!!!!
  214. const int iVal = atoi(pPtr);
  215. int tmp = iVal;
  216. while ((tmp = tmp / 10)!=0)
  217. ++iStep;
  218. if (0 != iVal)
  219. {
  220. // Store parsed index
  221. if (0 == iPos)
  222. {
  223. pIndices->push_back(iVal-1);
  224. }
  225. else if (1 == iPos)
  226. {
  227. pTexID->push_back(iVal-1);
  228. }
  229. else if (2 == iPos)
  230. {
  231. pNormalID->push_back(iVal-1);
  232. }
  233. else
  234. {
  235. reportErrorTokenInFace();
  236. }
  237. }
  238. }
  239. for (int i=0; i<iStep; i++)
  240. ++pPtr;
  241. }
  242. ObjFile::Face *face = new ObjFile::Face(pIndices, pNormalID, pTexID);
  243. // Set active material, if one set
  244. if (NULL != m_pModel->m_pCurrentMaterial)
  245. face->m_pMaterial = m_pModel->m_pCurrentMaterial;
  246. else
  247. face->m_pMaterial = m_pModel->m_pDefaultMaterial;
  248. // Create a default object, if nothing there
  249. if (NULL == m_pModel->m_pCurrent)
  250. createObject("defaultobject");
  251. // Store the new instance
  252. m_pModel->m_pCurrent->m_Faces.push_back(face);
  253. // Assign face to mesh
  254. m_pModel->m_pCurrentMesh->m_Faces.push_back( face );
  255. // Skip the rest of the line
  256. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  257. }
  258. // -------------------------------------------------------------------
  259. // Get values for a new material description
  260. void ObjFileParser::getMaterialDesc()
  261. {
  262. // Get next data for material data
  263. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  264. if (m_DataIt == m_DataItEnd)
  265. return;
  266. char *pStart = &(*m_DataIt);
  267. while ( !isSpace(*m_DataIt) && m_DataIt != m_DataItEnd )
  268. ++m_DataIt;
  269. // Get name
  270. std::string strName(pStart, &(*m_DataIt));
  271. if ( strName.empty() )
  272. return;
  273. // Search for material
  274. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
  275. if ( it == m_pModel->m_MaterialMap.end() )
  276. {
  277. // Not found, use default material
  278. m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
  279. m_pModel->m_MaterialMap[ strName ] = m_pModel->m_pCurrentMaterial;
  280. }
  281. else
  282. {
  283. // Found, using detected material
  284. m_pModel->m_pCurrentMaterial = (*it).second;
  285. // Create a new mesh for a new material
  286. m_pModel->m_pCurrentMesh = new ObjFile::Mesh();
  287. m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
  288. }
  289. // Skip rest of line
  290. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  291. }
  292. // -------------------------------------------------------------------
  293. // Get a comment, values will be skipped
  294. void ObjFileParser::getComment()
  295. {
  296. while (true)
  297. {
  298. if ('\n' == (*m_DataIt) || m_DataIt == m_DataItEnd)
  299. {
  300. ++m_DataIt;
  301. break;
  302. }
  303. else
  304. {
  305. ++m_DataIt;
  306. }
  307. }
  308. }
  309. // -------------------------------------------------------------------
  310. // Get material library from file.
  311. void ObjFileParser::getMaterialLib()
  312. {
  313. // Translate tuple
  314. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  315. if (m_DataIt == m_DataItEnd)
  316. return;
  317. char *pStart = &(*m_DataIt);
  318. while (!isSpace(*m_DataIt))
  319. m_DataIt++;
  320. // Check for existence
  321. DefaultIOSystem IOSystem;
  322. std::string strMatName(pStart, &(*m_DataIt));
  323. std::string absName = m_strAbsPath + IOSystem.getOsSeparator() + strMatName;
  324. if ( !IOSystem.Exists(absName) )
  325. {
  326. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  327. return;
  328. }
  329. // Extract the extention
  330. std::string strExt("");
  331. extractExtension(strMatName, strExt);
  332. std::string mat = "mtl";
  333. // Load the material library
  334. DefaultIOSystem FileSystem;
  335. IOStream *pFile = FileSystem.Open(absName);
  336. if (0L != pFile)
  337. {
  338. size_t size = pFile->FileSize();
  339. std::vector<char> buffer;
  340. buffer.resize( size );
  341. size_t read_size = pFile->Read( &buffer[ 0 ], sizeof( char ), size );
  342. FileSystem.Close( pFile );
  343. // Importing the material library
  344. ObjFileMtlImporter mtlImporter( buffer, absName, m_pModel );
  345. m_pModel->m_MaterialLib.push_back( strMatName );
  346. }
  347. // Skip rest of line
  348. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  349. }
  350. // -------------------------------------------------------------------
  351. // Set a new material definition as the current material.
  352. void ObjFileParser::getNewMaterial()
  353. {
  354. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  355. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  356. char *pStart = &(*m_DataIt);
  357. std::string strMat(pStart, *m_DataIt);
  358. while (isSpace(*m_DataIt))
  359. m_DataIt++;
  360. std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find(strMat);
  361. if (it == m_pModel->m_MaterialMap.end())
  362. {
  363. // Show a warning, if material was not found
  364. std::string strWarn ("Unsupported material requested: ");
  365. strWarn += strMat;
  366. std::cerr << "Warning : " << strWarn << std::endl;
  367. m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
  368. }
  369. else
  370. {
  371. // Set new material
  372. m_pModel->m_pCurrentMaterial = (*it).second;
  373. }
  374. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  375. }
  376. // -------------------------------------------------------------------
  377. // Getter for a group name.
  378. void ObjFileParser::getGroupName()
  379. {
  380. // Get next word from data buffer
  381. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  382. m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
  383. // Store groupname in group library
  384. char *pStart = &(*m_DataIt);
  385. while (!isSpace(*m_DataIt))
  386. m_DataIt++;
  387. std::string strGroupName(pStart, &(*m_DataIt));
  388. // Change active group, if necessary
  389. if (m_pModel->m_strActiveGroup != strGroupName)
  390. {
  391. // Search for already existing entry
  392. ObjFile::Model::ConstGroupMapIt it = m_pModel->m_Groups.find(&strGroupName);
  393. // New group name, creating a new entry
  394. ObjFile::Object *pObject = m_pModel->m_pCurrent;
  395. if (it == m_pModel->m_Groups.end())
  396. {
  397. std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
  398. m_pModel->m_Groups[ &strGroupName ] = pFaceIDArray;
  399. m_pModel->m_pGroupFaceIDs = (pFaceIDArray);
  400. }
  401. else
  402. {
  403. m_pModel->m_pGroupFaceIDs = (*it).second;
  404. }
  405. m_pModel->m_strActiveGroup = strGroupName;
  406. }
  407. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  408. }
  409. // -------------------------------------------------------------------
  410. // Not supported
  411. void ObjFileParser::getGroupNumber()
  412. {
  413. // Not used
  414. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  415. }
  416. // -------------------------------------------------------------------
  417. // Stores values for a new object instance, name will be used to
  418. // identify it.
  419. void ObjFileParser::getObjectName()
  420. {
  421. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  422. if (m_DataIt == m_DataItEnd)
  423. return;
  424. char *pStart = &(*m_DataIt);
  425. while (!isSpace(*m_DataIt))
  426. m_DataIt++;
  427. std::string strObjectName(pStart, &(*m_DataIt));
  428. if (!strObjectName.empty())
  429. {
  430. // Reset current object
  431. m_pModel->m_pCurrent = NULL;
  432. // Search for actual object
  433. for (std::vector<ObjFile::Object*>::const_iterator it = m_pModel->m_Objects.begin();
  434. it != m_pModel->m_Objects.end();
  435. ++it)
  436. {
  437. if ((*it)->m_strObjName == strObjectName)
  438. {
  439. m_pModel->m_pCurrent = *it;
  440. break;
  441. }
  442. }
  443. // Allocate a new object, if current one wasn´t found before
  444. if (m_pModel->m_pCurrent == NULL)
  445. {
  446. createObject(strObjectName);
  447. /*m_pModel->m_pCurrent = new ObjFile::Object();
  448. m_pModel->m_pCurrent->m_strObjName = strObjectName;
  449. m_pModel->m_Objects.push_back(m_pModel->m_pCurrent);*/
  450. }
  451. }
  452. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  453. }
  454. // -------------------------------------------------------------------
  455. // Creates a new object instance
  456. void ObjFileParser::createObject(const std::string &strObjectName)
  457. {
  458. ai_assert (NULL != m_pModel);
  459. ai_assert (!strObjectName.empty());
  460. m_pModel->m_pCurrent = new ObjFile::Object();
  461. m_pModel->m_pCurrent->m_strObjName = strObjectName;
  462. m_pModel->m_Objects.push_back(m_pModel->m_pCurrent);
  463. }
  464. // -------------------------------------------------------------------
  465. // Shows an error in parsing process.
  466. void ObjFileParser::reportErrorTokenInFace()
  467. {
  468. std::string strErr("");
  469. m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
  470. std::cerr << "Not supported token in face desc. detected : " << strErr << std::endl;
  471. }
  472. // -------------------------------------------------------------------
  473. // Extracts the extention from a filename
  474. void ObjFileParser::extractExtension(const std::string strFile,
  475. std::string &strExt)
  476. {
  477. strExt = "";
  478. if (strFile.empty())
  479. return;
  480. std::string::size_type pos = strFile.find_last_of(".");
  481. if (pos == std::string::npos)
  482. return;
  483. strExt = strFile.substr(pos, strFile.size() - pos);
  484. }
  485. // -------------------------------------------------------------------
  486. } // Namespace Assimp