LWOLoader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. /** @file Implementation of the LWO importer class */
  35. // internal headers
  36. #include "LWOLoader.h"
  37. #include "MaterialSystem.h"
  38. #include "StringComparison.h"
  39. #include "ByteSwap.h"
  40. // public assimp headers
  41. #include "../include/IOStream.h"
  42. #include "../include/IOSystem.h"
  43. #include "../include/aiScene.h"
  44. #include "../include/aiAssert.h"
  45. #include "../include/DefaultLogger.h"
  46. #include "../include/assimp.hpp"
  47. // boost headers
  48. #include <boost/scoped_ptr.hpp>
  49. using namespace Assimp;
  50. // ------------------------------------------------------------------------------------------------
  51. // Constructor to be privately used by Importer
  52. LWOImporter::LWOImporter()
  53. {
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. // Destructor, private as well
  57. LWOImporter::~LWOImporter()
  58. {
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // Returns whether the class can handle the format of the given file.
  62. bool LWOImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  63. {
  64. // simple check of file extension is enough for the moment
  65. std::string::size_type pos = pFile.find_last_of('.');
  66. // no file extension - can't read
  67. if( pos == std::string::npos)return false;
  68. std::string extension = pFile.substr( pos);
  69. if (extension.length() < 4)return false;
  70. if (extension[0] != '.')return false;
  71. if (extension[1] != 'l' && extension[1] != 'L')return false;
  72. if (extension[2] != 'w' && extension[2] != 'W')return false;
  73. if (extension[3] != 'o' && extension[3] != 'O')return false;
  74. return true;
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Setup configuration properties
  78. void LWOImporter::SetupProperties(const Importer* pImp)
  79. {
  80. this->configGradientResX = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_LWO_GRADIENT_RESX,512);
  81. this->configGradientResY = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_LWO_GRADIENT_RESY,512);
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. // Imports the given file into the given scene structure.
  85. void LWOImporter::InternReadFile( const std::string& pFile,
  86. aiScene* pScene,
  87. IOSystem* pIOHandler)
  88. {
  89. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  90. // Check whether we can read from the file
  91. if( file.get() == NULL)
  92. throw new ImportErrorException( "Failed to open LWO file " + pFile + ".");
  93. if((this->fileSize = (unsigned int)file->FileSize()) < 12)
  94. throw new ImportErrorException("LWO: The file is too small to contain the IFF header");
  95. // allocate storage and copy the contents of the file to a memory buffer
  96. std::vector< uint8_t > mBuffer(fileSize);
  97. file->Read( &mBuffer[0], 1, fileSize);
  98. this->pScene = pScene;
  99. // determine the type of the file
  100. uint32_t fileType;
  101. const char* sz = IFF::ReadHeader(&mBuffer[0],fileType);
  102. if (sz)throw new ImportErrorException(sz);
  103. mFileBuffer = &mBuffer[0] + 12;
  104. fileSize -= 12;
  105. // create temporary storage on the stack but store pointers to it in the class
  106. // instance. Therefore everything will be destructed properly if an exception
  107. // is thrown and we needn't take care of that.
  108. LayerList _mLayers;
  109. mLayers = &_mLayers;
  110. TagList _mTags;
  111. mTags = &_mTags;
  112. TagMappingTable _mMapping;
  113. mMapping = &_mMapping;
  114. SurfaceList _mSurfaces;
  115. mSurfaces = &_mSurfaces;
  116. // allocate a default layer
  117. mLayers->push_back(Layer());
  118. mCurLayer = &mLayers->back();
  119. mCurLayer->mName = "<LWODefault>";
  120. // old lightwave file format (prior to v6)
  121. if (AI_LWO_FOURCC_LWOB == fileType)
  122. {
  123. mIsLWO2 = false;
  124. this->LoadLWOBFile();
  125. }
  126. // new lightwave format
  127. else if (AI_LWO_FOURCC_LWO2 == fileType)
  128. {
  129. throw new ImportErrorException("LWO2 is under development and currently disabled.");
  130. mIsLWO2 = true;
  131. this->LoadLWO2File();
  132. }
  133. // we don't know this format
  134. else
  135. {
  136. char szBuff[5];
  137. szBuff[0] = (char)(fileType >> 24u);
  138. szBuff[1] = (char)(fileType >> 16u);
  139. szBuff[2] = (char)(fileType >> 8u);
  140. szBuff[3] = (char)(fileType);
  141. throw new ImportErrorException(std::string("Unknown LWO sub format: ") + szBuff);
  142. }
  143. ResolveTags();
  144. // now process all layers and build meshes and nodes
  145. std::vector<aiMesh*> apcMeshes;
  146. std::vector<aiNode*> apcNodes;
  147. apcNodes.reserve(mLayers->size());
  148. apcMeshes.reserve(mLayers->size()*std::min(((unsigned int)mSurfaces->size()/2u), 1u));
  149. // the RemoveRedundantMaterials step will clean this up later
  150. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = (unsigned int)mSurfaces->size()];
  151. for (unsigned int mat = 0; mat < pScene->mNumMaterials;++mat)
  152. {
  153. MaterialHelper* pcMat = new MaterialHelper();
  154. pScene->mMaterials[mat] = pcMat;
  155. ConvertMaterial((*mSurfaces)[mat],pcMat);
  156. }
  157. unsigned int iDefaultSurface = 0xffffffff; // index of the default surface
  158. for (LayerList::const_iterator lit = mLayers->begin(), lend = mLayers->end();
  159. lit != lend;++lit)
  160. {
  161. const LWO::Layer& layer = *lit;
  162. // I don't know whether there could be dummy layers, but it would be possible
  163. const unsigned int meshStart = (unsigned int)apcMeshes.size();
  164. if (!layer.mFaces.empty() && !layer.mTempPoints.empty())
  165. {
  166. // now sort all faces by the surfaces assigned to them
  167. typedef std::vector<unsigned int> SortedRep;
  168. std::vector<SortedRep> pSorted(mSurfaces->size()+1);
  169. unsigned int i = 0;
  170. for (FaceList::const_iterator it = layer.mFaces.begin(), end = layer.mFaces.end();
  171. it != end;++it,++i)
  172. {
  173. unsigned int idx = (*it).surfaceIndex;
  174. if (idx >= mTags->size())
  175. {
  176. DefaultLogger::get()->warn("LWO: Invalid face surface index");
  177. idx = (unsigned int)mTags->size()-1;
  178. }
  179. if(0xffffffff == (idx = _mMapping[idx]))
  180. {
  181. if (0xffffffff == iDefaultSurface)
  182. {
  183. iDefaultSurface = (unsigned int)mSurfaces->size();
  184. mSurfaces->push_back(LWO::Surface());
  185. LWO::Surface& surf = mSurfaces->back();
  186. surf.mColor.r = surf.mColor.g = surf.mColor.b = 0.6f;
  187. }
  188. idx = iDefaultSurface;
  189. }
  190. pSorted[idx].push_back(i);
  191. }
  192. if (0xffffffff == iDefaultSurface)pSorted.erase(pSorted.end()-1);
  193. // now generate output meshes
  194. for (unsigned int p = 0; p < mSurfaces->size();++p)
  195. if (!pSorted[p].empty())pScene->mNumMeshes++;
  196. if (!pScene->mNumMeshes)
  197. throw new ImportErrorException("LWO: There are no meshes");
  198. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  199. for (unsigned int p = 0,i = 0;i < mSurfaces->size();++i)
  200. {
  201. SortedRep& sorted = pSorted[i];
  202. if (sorted.empty())continue;
  203. // generate the mesh
  204. aiMesh* mesh = new aiMesh();
  205. apcMeshes.push_back(mesh);
  206. mesh->mNumFaces = (unsigned int)sorted.size();
  207. for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
  208. it != end;++it)
  209. {
  210. mesh->mNumVertices += layer.mFaces[*it].mNumIndices;
  211. }
  212. aiVector3D* pv = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  213. aiFace* pf = mesh->mFaces = new aiFace[mesh->mNumFaces];
  214. mesh->mMaterialIndex = i;
  215. // now convert all faces
  216. unsigned int vert = 0;
  217. for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
  218. it != end;++it)
  219. {
  220. const LWO::Face& face = layer.mFaces[*it];
  221. // copy all vertices
  222. for (unsigned int q = 0; q < face.mNumIndices;++q)
  223. {
  224. *pv++ = layer.mTempPoints[face.mIndices[q]];
  225. face.mIndices[q] = vert++;
  226. }
  227. pf->mIndices = face.mIndices;
  228. pf->mNumIndices = face.mNumIndices;
  229. const_cast<unsigned int*>(face.mIndices) = NULL; // make sure it won't be deleted
  230. pf++;
  231. }
  232. ++p;
  233. }
  234. }
  235. // generate nodes to render the mesh. Store the parent index
  236. // in the mParent member of the nodes
  237. aiNode* pcNode = new aiNode();
  238. apcNodes.push_back(pcNode);
  239. pcNode->mName.Set(layer.mName);
  240. pcNode->mParent = reinterpret_cast<aiNode*>(layer.mParent);
  241. pcNode->mNumMeshes = (unsigned int)apcMeshes.size() - meshStart;
  242. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  243. for (unsigned int p = 0; p < pcNode->mNumMeshes;++p)
  244. pcNode->mMeshes[p] = p + meshStart;
  245. }
  246. // generate the final node graph
  247. GenerateNodeGraph(apcNodes);
  248. // copy the meshes to the output structure
  249. if (apcMeshes.size()) // shouldn't occur, just to be sure we don't crash
  250. {
  251. pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = (unsigned int)apcMeshes.size() ];
  252. ::memcpy(pScene->mMeshes,&apcMeshes[0],pScene->mNumMeshes*sizeof(void*));
  253. }
  254. }
  255. // ------------------------------------------------------------------------------------------------
  256. void LWOImporter::GenerateNodeGraph(std::vector<aiNode*>& apcNodes)
  257. {
  258. // now generate the final nodegraph
  259. uint16_t curIndex = 0;
  260. while (curIndex < (uint16_t)apcNodes.size())
  261. {
  262. aiNode* node;
  263. uint16_t iCurParent = curIndex-1;
  264. node = curIndex ? apcNodes[iCurParent] : new aiNode("<dummy_root>");
  265. unsigned int numChilds = 0;
  266. for (unsigned int i = 0; i < apcNodes.size();++i)
  267. {
  268. if (i == iCurParent)continue;
  269. if ( reinterpret_cast<uint16_t>(apcNodes[i]->mParent) == iCurParent)++numChilds;
  270. }
  271. if (numChilds)
  272. {
  273. if (!pScene->mRootNode)
  274. {
  275. pScene->mRootNode = node;
  276. }
  277. node->mChildren = new aiNode* [ node->mNumChildren = numChilds ];
  278. for (unsigned int i = 0, p = 0; i < apcNodes.size();++i)
  279. {
  280. if (i == iCurParent)continue;
  281. uint16_t parent = reinterpret_cast<uint16_t>(apcNodes[i]->mParent);
  282. if (parent == iCurParent)
  283. {
  284. node->mChildren[p++] = apcNodes[i];
  285. apcNodes[i]->mParent = node;
  286. apcNodes[i] = NULL;
  287. }
  288. }
  289. }
  290. else if (!curIndex)delete node;
  291. ++curIndex;
  292. }
  293. // remove a single root node
  294. // TODO: implement directly in the above loop, no need to deallocate here
  295. if (1 == pScene->mRootNode->mNumChildren)
  296. {
  297. aiNode* pc = pScene->mRootNode->mChildren[0];
  298. pc->mParent = pScene->mRootNode->mChildren[0] = NULL;
  299. delete pScene->mRootNode;
  300. pScene->mRootNode = pc;
  301. }
  302. // add unreferenced nodes to a dummy root
  303. unsigned int m = 0;
  304. for (std::vector<aiNode*>::iterator it = apcNodes.begin(), end = apcNodes.end();
  305. it != end;++it)
  306. {
  307. aiNode* p = *it;
  308. if (p)++m;
  309. }
  310. if (m)
  311. {
  312. aiNode* pc = new aiNode();
  313. pc->mName.Set("<dummy_root>");
  314. aiNode** cc = pc->mChildren = new aiNode*[ pc->mNumChildren = m+1 ];
  315. for (std::vector<aiNode*>::iterator it = apcNodes.begin(), end = apcNodes.end();
  316. it != end;++it)
  317. {
  318. aiNode* p = *it;
  319. if (p)*cc++ = p;
  320. }
  321. if (pScene->mRootNode)
  322. {
  323. *cc = pScene->mRootNode;
  324. pScene->mRootNode->mParent = pc;
  325. }
  326. else --pc->mNumChildren;
  327. pScene->mRootNode = pc;
  328. }
  329. if (!pScene->mRootNode)throw new ImportErrorException("LWO: Unable to build a valid node graph");
  330. }
  331. // ------------------------------------------------------------------------------------------------
  332. void LWOImporter::CountVertsAndFaces(unsigned int& verts, unsigned int& faces,
  333. LE_NCONST uint16_t*& cursor, const uint16_t* const end, unsigned int max)
  334. {
  335. while (cursor < end && max--)
  336. {
  337. uint16_t numIndices = *cursor++;
  338. verts += numIndices;faces++;
  339. cursor += numIndices;
  340. int16_t surface = *cursor++;
  341. if (surface < 0)
  342. {
  343. // there are detail polygons
  344. numIndices = *cursor++;
  345. CountVertsAndFaces(verts,faces,cursor,end,numIndices);
  346. }
  347. }
  348. }
  349. // ------------------------------------------------------------------------------------------------
  350. void LWOImporter::CopyFaceIndices(FaceList::iterator& it,
  351. LE_NCONST uint16_t*& cursor,
  352. const uint16_t* const end,
  353. unsigned int max)
  354. {
  355. while (cursor < end && max--)
  356. {
  357. LWO::Face& face = *it;++it;
  358. if(face.mNumIndices = *cursor++)
  359. {
  360. if (cursor + face.mNumIndices >= end)break;
  361. face.mIndices = new unsigned int[face.mNumIndices];
  362. for (unsigned int i = 0; i < face.mNumIndices;++i)
  363. {
  364. unsigned int & mi = face.mIndices[i] = *cursor++;
  365. if (mi > mCurLayer->mTempPoints.size())
  366. {
  367. DefaultLogger::get()->warn("LWO: face index is out of range");
  368. mi = (unsigned int)mCurLayer->mTempPoints.size()-1;
  369. }
  370. }
  371. }
  372. else DefaultLogger::get()->warn("LWO: Face has 0 indices");
  373. int16_t surface = *cursor++;
  374. if (surface < 0)
  375. {
  376. surface = -surface;
  377. // there are detail polygons
  378. uint16_t numPolygons = *cursor++;
  379. if (cursor < end)CopyFaceIndices(it,cursor,end,numPolygons);
  380. }
  381. face.surfaceIndex = surface-1;
  382. }
  383. }
  384. // ------------------------------------------------------------------------------------------------
  385. void LWOImporter::ResolveTags()
  386. {
  387. mMapping->resize(mTags->size(),0xffffffff);
  388. for (unsigned int a = 0; a < mTags->size();++a)
  389. {
  390. for (unsigned int i = 0; i < mSurfaces->size();++i)
  391. {
  392. const std::string& c = (*mTags)[a];
  393. const std::string& d = (*mSurfaces)[i].mName;
  394. if (!ASSIMP_stricmp(c,d))
  395. {
  396. (*mMapping)[a] = i;
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. // ------------------------------------------------------------------------------------------------
  403. void LWOImporter::ParseString(std::string& out,unsigned int max)
  404. {
  405. unsigned int iCursor = 0;
  406. const char* in = (const char*)mFileBuffer,*sz = in;
  407. while (*in)
  408. {
  409. if (++iCursor > max)
  410. {
  411. DefaultLogger::get()->warn("LWOB: Invalid file, string is is too long");
  412. break;
  413. }
  414. ++in;
  415. }
  416. unsigned int len = (unsigned int) (in-sz);
  417. out = std::string(sz,len);
  418. }
  419. // ------------------------------------------------------------------------------------------------
  420. void LWOImporter::AdjustTexturePath(std::string& out)
  421. {
  422. if (::strstr(out.c_str(), "(sequence)"))
  423. {
  424. // remove the (sequence) and append 000
  425. DefaultLogger::get()->info("LWO: Sequence of animated texture found. It will be ignored");
  426. out = out.substr(0,out.length()-10) + "000";
  427. }
  428. }
  429. // ------------------------------------------------------------------------------------------------
  430. void LWOImporter::LoadLWOTags(unsigned int size)
  431. {
  432. const char* szCur = (const char*)mFileBuffer, *szLast = szCur;
  433. const char* const szEnd = szLast+size;
  434. while (szCur < szEnd)
  435. {
  436. if (!(*szCur))
  437. {
  438. const unsigned int len = (unsigned int)(szCur-szLast);
  439. mTags->push_back(std::string(szLast,len));
  440. szCur += len & 1;
  441. szLast = szCur;
  442. }
  443. szCur++;
  444. }
  445. }
  446. // ------------------------------------------------------------------------------------------------
  447. void LWOImporter::LoadLWOPoints(unsigned int length)
  448. {
  449. mCurLayer->mTempPoints.resize( length / 12 );
  450. // perform endianess conversions
  451. #ifndef AI_BUILD_BIG_ENDIAN
  452. for (unsigned int i = 0; i < length>>2;++i)
  453. ByteSwap::Swap4( mFileBuffer + (i << 2));
  454. #endif
  455. ::memcpy(&mCurLayer->mTempPoints[0],mFileBuffer,length);
  456. }
  457. // ------------------------------------------------------------------------------------------------
  458. void LWOImporter::LoadLWOPolygons(unsigned int length)
  459. {
  460. // first find out how many faces and vertices we'll finally need
  461. LE_NCONST uint16_t* const end = (LE_NCONST uint16_t*)(mFileBuffer+length);
  462. LE_NCONST uint16_t* cursor = (LE_NCONST uint16_t*)mFileBuffer;
  463. // perform endianess conversions
  464. #ifndef AI_BUILD_BIG_ENDIAN
  465. while (cursor < end)ByteSwap::Swap2(cursor++);
  466. cursor = (LE_NCONST uint16_t*)mFileBuffer;
  467. #endif
  468. unsigned int iNumFaces = 0,iNumVertices = 0;
  469. CountVertsAndFaces(iNumVertices,iNumFaces,cursor,end);
  470. // allocate the output array and copy face indices
  471. if (iNumFaces)
  472. {
  473. cursor = (LE_NCONST uint16_t*)mFileBuffer;
  474. // this->mTempPoints->resize(iNumVertices);
  475. mCurLayer->mFaces.resize(iNumFaces);
  476. FaceList::iterator it = mCurLayer->mFaces.begin();
  477. CopyFaceIndices(it,cursor,end);
  478. }
  479. }
  480. // ------------------------------------------------------------------------------------------------
  481. void LWOImporter::LoadLWOBFile()
  482. {
  483. LE_NCONST uint8_t* const end = mFileBuffer + fileSize;
  484. while (true)
  485. {
  486. if (mFileBuffer + sizeof(IFF::ChunkHeader) > end)break;
  487. LE_NCONST IFF::ChunkHeader* const head = (LE_NCONST IFF::ChunkHeader*)mFileBuffer;
  488. AI_LSWAP4(head->length);
  489. AI_LSWAP4(head->type);
  490. mFileBuffer += sizeof(IFF::ChunkHeader);
  491. if (mFileBuffer + head->length > end)
  492. {
  493. throw new ImportErrorException("LWOB: Invalid file, the size attribute of "
  494. "a chunk points behind the end of the file");
  495. break;
  496. }
  497. LE_NCONST uint8_t* const next = mFileBuffer+head->length;
  498. switch (head->type)
  499. {
  500. // vertex list
  501. case AI_LWO_PNTS:
  502. {
  503. if (!mCurLayer->mTempPoints.empty())
  504. DefaultLogger::get()->warn("LWO: PNTS chunk encountered twice");
  505. else LoadLWOPoints(head->length);
  506. break;
  507. }
  508. // face list
  509. case AI_LWO_POLS:
  510. {
  511. if (!mCurLayer->mFaces.empty())
  512. DefaultLogger::get()->warn("LWO: POLS chunk encountered twice");
  513. else LoadLWOPolygons(head->length);
  514. break;
  515. }
  516. // list of tags
  517. case AI_LWO_SRFS:
  518. {
  519. if (!mTags->empty())
  520. DefaultLogger::get()->warn("LWO: SRFS chunk encountered twice");
  521. else LoadLWOTags(head->length);
  522. break;
  523. }
  524. // surface chunk
  525. case AI_LWO_SURF:
  526. {
  527. if (!mSurfaces->empty())
  528. DefaultLogger::get()->warn("LWO: SURF chunk encountered twice");
  529. else LoadLWOBSurface(head->length);
  530. break;
  531. }
  532. }
  533. mFileBuffer = next;
  534. }
  535. }
  536. // ------------------------------------------------------------------------------------------------
  537. void LWOImporter::LoadLWO2File()
  538. {
  539. LE_NCONST uint8_t* const end = mFileBuffer + fileSize;
  540. while (true)
  541. {
  542. if (mFileBuffer + sizeof(IFF::ChunkHeader) > end)break;
  543. LE_NCONST IFF::ChunkHeader* const head = (LE_NCONST IFF::ChunkHeader*)mFileBuffer;
  544. AI_LSWAP4(head->length);
  545. AI_LSWAP4(head->type);
  546. mFileBuffer += sizeof(IFF::ChunkHeader);
  547. if (mFileBuffer + head->length > end)
  548. {
  549. throw new ImportErrorException("LWOB: Invalid file, the size attribute of "
  550. "a chunk points behind the end of the file");
  551. break;
  552. }
  553. LE_NCONST uint8_t* const next = mFileBuffer+head->length;
  554. unsigned int iUnnamed = 0;
  555. switch (head->type)
  556. {
  557. // new layer
  558. case AI_LWO_LAYR:
  559. {
  560. // add a new layer to the list ....
  561. mLayers->push_back ( LWO::Layer() );
  562. LWO::Layer& layer = mLayers->back();
  563. mCurLayer = &layer;
  564. AI_LWO_VALIDATE_CHUNK_LENGTH(head->length,LAYR,16);
  565. // and parse its properties
  566. mFileBuffer += 16;
  567. ParseString(layer.mName,head->length-16);
  568. // if the name is empty, generate a default name
  569. if (layer.mName.empty())
  570. {
  571. char buffer[128]; // should be sufficiently large
  572. ::sprintf(buffer,"Layer_%i", iUnnamed++);
  573. layer.mName = buffer;
  574. }
  575. if (mFileBuffer + 2 <= next)
  576. layer.mParent = *((uint16_t*)mFileBuffer);
  577. break;
  578. }
  579. // vertex list
  580. case AI_LWO_PNTS:
  581. {
  582. if (!mCurLayer->mTempPoints.empty())
  583. DefaultLogger::get()->warn("LWO: PNTS chunk encountered twice");
  584. else LoadLWOPoints(head->length);
  585. break;
  586. }
  587. // face list
  588. case AI_LWO_POLS:
  589. {
  590. if (!mCurLayer->mFaces.empty())
  591. DefaultLogger::get()->warn("LWO: POLS chunk encountered twice");
  592. else LoadLWOPolygons(head->length);
  593. break;
  594. }
  595. // list of tags
  596. case AI_LWO_SRFS:
  597. {
  598. if (!mTags->empty())
  599. DefaultLogger::get()->warn("LWO: SRFS chunk encountered twice");
  600. else LoadLWOTags(head->length);
  601. break;
  602. }
  603. // surface chunk
  604. case AI_LWO_SURF:
  605. {
  606. if (!mSurfaces->empty())
  607. DefaultLogger::get()->warn("LWO: SURF chunk encountered twice");
  608. else LoadLWOBSurface(head->length);
  609. break;
  610. }
  611. }
  612. mFileBuffer = next;
  613. }
  614. }