ObjFileParser.cpp 13 KB

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