2
0

ObjFileParser.cpp 14 KB

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