LWOLoader.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 "ByteSwap.h"
  39. // public assimp headers
  40. #include "../include/IOStream.h"
  41. #include "../include/IOSystem.h"
  42. #include "../include/aiMesh.h"
  43. #include "../include/aiScene.h"
  44. #include "../include/aiAssert.h"
  45. #include "../include/DefaultLogger.h"
  46. // boost headers
  47. #include <boost/scoped_ptr.hpp>
  48. using namespace Assimp;
  49. // ------------------------------------------------------------------------------------------------
  50. // Constructor to be privately used by Importer
  51. LWOImporter::LWOImporter()
  52. {
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Destructor, private as well
  56. LWOImporter::~LWOImporter()
  57. {
  58. mFaces.clear();
  59. mTempPoints.clear();
  60. mSurfaces.clear();
  61. }
  62. // ------------------------------------------------------------------------------------------------
  63. // Returns whether the class can handle the format of the given file.
  64. bool LWOImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  65. {
  66. // simple check of file extension is enough for the moment
  67. std::string::size_type pos = pFile.find_last_of('.');
  68. // no file extension - can't read
  69. if( pos == std::string::npos)return false;
  70. std::string extension = pFile.substr( pos);
  71. if (extension.length() < 4)return false;
  72. if (extension[0] != '.')return false;
  73. if (extension[1] != 'l' && extension[1] != 'L')return false;
  74. if (extension[2] != 'w' && extension[2] != 'W')return false;
  75. if (extension[3] != 'o' && extension[3] != 'O')return false;
  76. return true;
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Imports the given file into the given scene structure.
  80. void LWOImporter::InternReadFile( const std::string& pFile,
  81. aiScene* pScene,
  82. IOSystem* pIOHandler)
  83. {
  84. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  85. // Check whether we can read from the file
  86. if( file.get() == NULL)
  87. throw new ImportErrorException( "Failed to open LWO file " + pFile + ".");
  88. if((this->fileSize = (unsigned int)file->FileSize()) < 12)
  89. throw new ImportErrorException("LWO: The file is too small to contain the IFF header");
  90. // allocate storage and copy the contents of the file to a memory buffer
  91. std::vector< uint8_t > mBuffer(fileSize);
  92. file->Read( &mBuffer[0], 1, fileSize);
  93. this->pScene = pScene;
  94. // determine the type of the file
  95. uint32_t fileType;
  96. const char* sz = IFF::ReadHeader(&mBuffer[0],fileType);
  97. if (sz)throw new ImportErrorException(sz);
  98. mFileBuffer = &mBuffer[0] + 12;
  99. fileSize -= 12;
  100. try
  101. {
  102. // old lightwave file format (prior to v6)
  103. if (AI_LWO_FOURCC_LWOB == fileType)
  104. this->LoadLWOBFile();
  105. // new lightwave format
  106. else if (AI_LWO_FOURCC_LWO2 == fileType)
  107. this->LoadLWO2File();
  108. // we don't know this format
  109. else
  110. {
  111. char szBuff[5];
  112. szBuff[0] = (char)(fileType >> 24u);
  113. szBuff[1] = (char)(fileType >> 16u);
  114. szBuff[2] = (char)(fileType >> 8u);
  115. szBuff[3] = (char)(fileType);
  116. throw new ImportErrorException(std::string("Unknown LWO sub format: ") + szBuff);
  117. }
  118. // generate a default surface if necessary
  119. if (mSurfaces.empty())
  120. mSurfaces.push_back(LWO::Surface());
  121. // now sort all faces by the surfaces assigned to them
  122. typedef std::vector<unsigned int> SortedRep;
  123. std::vector<SortedRep> pSorted(mSurfaces.size());
  124. unsigned int i = 0;
  125. for (FaceList::iterator it = mFaces.begin(), end = mFaces.end();
  126. it != end;++it,++i)
  127. {
  128. if ((*it).surfaceIndex >= mSurfaces.size())
  129. {
  130. DefaultLogger::get()->warn("LWO: Invalid face surface index");
  131. (*it).surfaceIndex = mSurfaces.size()-1;
  132. }
  133. pSorted[(*it).surfaceIndex].push_back(i);
  134. }
  135. // now generate output meshes
  136. for (unsigned int p = 0; p < mSurfaces.size();++p)
  137. if (!pSorted[p].empty())pScene->mNumMeshes++;
  138. if (!(pScene->mNumMaterials = pScene->mNumMeshes))
  139. throw new ImportErrorException("LWO: There are no meshes");
  140. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  141. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  142. for (unsigned int p = 0,i = 0;i < mSurfaces.size();++i)
  143. {
  144. SortedRep& sorted = pSorted[i];
  145. if (sorted.empty())continue;
  146. // generate the mesh
  147. aiMesh* mesh = pScene->mMeshes[p] = new aiMesh();
  148. mesh->mNumFaces = sorted.size();
  149. for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
  150. it != end;++it)
  151. {
  152. mesh->mNumVertices += mFaces[*it].mNumIndices;
  153. }
  154. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  155. mesh->mFaces = new aiFace[mesh->mNumFaces];
  156. mesh->mMaterialIndex = p;
  157. // now convert all faces
  158. unsigned int vert = 0;
  159. for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
  160. it != end;++it)
  161. {
  162. LWO::Face& face = mFaces[*it];
  163. // copy all vertices
  164. for (unsigned int q = 0; q < face.mNumIndices;++q)
  165. {
  166. *mesh->mVertices++ = mTempPoints[face.mIndices[q]];
  167. face.mIndices[q] = vert++;
  168. }
  169. mesh->mFaces->mIndices = face.mIndices;
  170. mesh->mFaces->mNumIndices = face.mNumIndices;
  171. face.mIndices = NULL; // make sure it won't be deleted
  172. mesh->mFaces++;
  173. }
  174. mesh->mFaces -= mesh->mNumFaces;
  175. mesh->mVertices -= mesh->mNumVertices;
  176. // generate the corresponding material
  177. MaterialHelper* pcMat = new MaterialHelper();
  178. pScene->mMaterials[p] = pcMat;
  179. // todo
  180. ++p;
  181. }
  182. }
  183. // make sure the arrays are cleaned up ...
  184. catch (ImportErrorException* ex)
  185. {
  186. this->~LWOImporter();
  187. throw ex;
  188. }
  189. this->~LWOImporter();
  190. }
  191. // ------------------------------------------------------------------------------------------------
  192. void LWOImporter::CountVertsAndFaces(unsigned int& verts, unsigned int& faces,
  193. LE_NCONST uint8_t*& cursor, const uint8_t* const end, unsigned int max)
  194. {
  195. while (cursor < end && max--)
  196. {
  197. uint16_t numIndices = *((uint16_t*)cursor);cursor+=2;
  198. verts += numIndices;faces++;
  199. cursor += numIndices*2;
  200. int16_t surface = *((uint16_t*)cursor);cursor+=2;
  201. if (surface < 0)
  202. {
  203. // there are detail polygons
  204. numIndices = *((uint16_t*)cursor);cursor+=2;
  205. CountVertsAndFaces(verts,faces,cursor,end,numIndices);
  206. }
  207. }
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. void LWOImporter::CopyFaceIndices(LWOImporter::FaceList::iterator& it,LE_NCONST uint8_t*& cursor,
  211. const uint8_t* const end, unsigned int max)
  212. {
  213. while (cursor < end && max--)
  214. {
  215. LWO::Face& face = *it;++it;
  216. face.mNumIndices = *((uint16_t*)cursor);
  217. if (cursor + face.mNumIndices*2 + 4 >= end)break;
  218. face.mIndices = new unsigned int[face.mNumIndices];
  219. for (unsigned int i = 0; i < face.mNumIndices;++i)
  220. {
  221. face.mIndices[i] = *((uint16_t*)(cursor+=2));
  222. if (face.mIndices[i] >= mTempPoints.size())
  223. {
  224. face.mIndices[i] = mTempPoints.size()-1;
  225. DefaultLogger::get()->warn("LWO: Face index is out of range");
  226. }
  227. }
  228. cursor+=2;
  229. int16_t surface = *((uint16_t*)cursor);cursor+=2;
  230. if (surface < 0)
  231. {
  232. surface *= -1;
  233. // there are detail polygons
  234. uint16_t numPolygons = *((uint16_t*)cursor);cursor+=2;
  235. if (cursor < end)CopyFaceIndices(it,cursor,end,numPolygons);
  236. }
  237. face.surfaceIndex = surface-1;
  238. }
  239. }
  240. // ------------------------------------------------------------------------------------------------
  241. void LWOImporter::LoadLWOBSurface(unsigned int size)
  242. {
  243. uint32_t iCursor = 0;
  244. mSurfaces.push_back( LWO::Surface () );
  245. LWO::Surface& surf = mSurfaces.back();
  246. LWO::Texture* pTex = NULL;
  247. // at first we'll need to read the name of the surface
  248. const uint8_t* sz = mFileBuffer;
  249. while (*mFileBuffer)
  250. {
  251. if (++iCursor > size)throw new ImportErrorException("LWOB: Invalid file, surface name is too long");
  252. ++mFileBuffer;
  253. }
  254. unsigned int len = unsigned int (mFileBuffer-sz);
  255. surf.mName = std::string((const char*)sz,len);
  256. mFileBuffer += 1-(len & 1); // skip one byte if the length of the string is odd
  257. while (true)
  258. {
  259. if ((iCursor += sizeof(IFF::ChunkHeader)) > size)break;
  260. LE_NCONST IFF::ChunkHeader* head = (LE_NCONST IFF::ChunkHeader*)mFileBuffer;
  261. AI_LSWAP4(head->length);
  262. AI_LSWAP4(head->type);
  263. if ((iCursor += head->length) > size)
  264. {
  265. throw new ImportErrorException("LWOB: Invalid file, the size attribute of "
  266. "a surface sub chunk points behind the end of the file");
  267. }
  268. mFileBuffer += sizeof(IFF::ChunkHeader);
  269. switch (head->type)
  270. {
  271. // diffuse color
  272. case AI_LWO_COLR:
  273. {
  274. if (head->length < 3)
  275. {
  276. DefaultLogger::get()->warn("LWO: COLR chunk is expected to be at least 3 bytes in size");
  277. break;
  278. }
  279. surf.mColor.r = *mFileBuffer++ / 255.0f;
  280. surf.mColor.g = *mFileBuffer++ / 255.0f;
  281. surf.mColor.b = *mFileBuffer / 255.0f;
  282. break;
  283. }
  284. // diffuse strength ... hopefully
  285. case AI_LWO_DIFF:
  286. {
  287. AI_LSWAP2(mFileBuffer);
  288. surf.mDiffuseValue = *((int16_t*)mFileBuffer) / 255.0f;
  289. break;
  290. }
  291. // specular strength ... hopefully
  292. case AI_LWO_SPEC:
  293. {
  294. AI_LSWAP2(mFileBuffer);
  295. surf.mSpecularValue = *((int16_t*)mFileBuffer) / 255.0f;
  296. break;
  297. }
  298. // transparency
  299. case AI_LWO_TRAN:
  300. {
  301. AI_LSWAP2(mFileBuffer);
  302. surf.mTransparency = *((int16_t*)mFileBuffer) / 255.0f;
  303. break;
  304. }
  305. // glossiness
  306. case AI_LWO_GLOS:
  307. {
  308. AI_LSWAP2(mFileBuffer);
  309. surf.mGlossiness = float(*((int16_t*)mFileBuffer));
  310. break;
  311. }
  312. // color texture
  313. case AI_LWO_CTEX:
  314. {
  315. pTex = &surf.mColorTexture;
  316. break;
  317. }
  318. // diffuse texture
  319. case AI_LWO_DTEX:
  320. {
  321. pTex = &surf.mDiffuseTexture;
  322. break;
  323. }
  324. // specular texture
  325. case AI_LWO_STEX:
  326. {
  327. pTex = &surf.mSpecularTexture;
  328. break;
  329. }
  330. // bump texture
  331. case AI_LWO_BTEX:
  332. {
  333. pTex = &surf.mBumpTexture;
  334. break;
  335. }
  336. // transparency texture
  337. case AI_LWO_TTEX:
  338. {
  339. pTex = &surf.mTransparencyTexture;
  340. break;
  341. }
  342. // texture path
  343. case AI_LWO_TIMG:
  344. {
  345. if (pTex)
  346. {
  347. unsigned int iCursor = 0;
  348. while (*mFileBuffer)
  349. {
  350. if (++iCursor > head->length)
  351. {
  352. DefaultLogger::get()->warn("LWOB: Invalid file, texture name (TIMG) is too long");
  353. break;
  354. }
  355. ++mFileBuffer;
  356. }
  357. unsigned int len = unsigned int (mFileBuffer-sz);
  358. pTex->mFileName = std::string((const char*)sz,len);
  359. }
  360. else DefaultLogger::get()->warn("LWOB: TIMG tag was encuntered although "
  361. "there was no xTEX tag before");
  362. break;
  363. }
  364. // texture strength
  365. case AI_LWO_TVAL:
  366. {
  367. if (pTex)pTex->mStrength = *mFileBuffer / 255.0f;
  368. else DefaultLogger::get()->warn("LWOB: TVAL tag was encuntered "
  369. "although there was no xTEX tag before");
  370. break;
  371. }
  372. }
  373. mFileBuffer += head->length;
  374. }
  375. }
  376. // ------------------------------------------------------------------------------------------------
  377. void LWOImporter::LoadLWOBFile()
  378. {
  379. uint32_t iCursor = 0;
  380. while (true)
  381. {
  382. if ((iCursor += sizeof(IFF::ChunkHeader)) > this->fileSize)break;
  383. LE_NCONST IFF::ChunkHeader* head = (LE_NCONST IFF::ChunkHeader*)mFileBuffer;
  384. AI_LSWAP4(head->length);
  385. AI_LSWAP4(head->type);
  386. if ((iCursor += head->length) > this->fileSize)
  387. {
  388. //throw new ImportErrorException("LWOB: Invalid file, the size attribute of "
  389. // "a chunk points behind the end of the file");
  390. break;
  391. }
  392. mFileBuffer += sizeof(IFF::ChunkHeader);
  393. switch (head->type)
  394. {
  395. // vertex list
  396. case AI_LWO_PNTS:
  397. {
  398. mTempPoints.resize( head->length / 12 );
  399. #ifndef AI_BUILD_BIG_ENDIAN
  400. for (unsigned int i = 0; i < head->length>>2;++i)
  401. ByteSwap::Swap4( mFileBuffer + (i << 2));
  402. #endif
  403. ::memcpy(&mTempPoints[0],mFileBuffer,head->length);
  404. break;
  405. }
  406. // face list
  407. case AI_LWO_POLS:
  408. {
  409. // first find out how many faces and vertices we'll finally need
  410. const uint8_t* const end = mFileBuffer + head->length;
  411. LE_NCONST uint8_t* cursor = mFileBuffer;
  412. #ifndef AI_BUILD_BIG_ENDIAN
  413. while (cursor < end)ByteSwap::Swap2(cursor++);
  414. cursor = mFileBuffer;
  415. #endif
  416. unsigned int iNumFaces = 0,iNumVertices = 0;
  417. CountVertsAndFaces(iNumVertices,iNumFaces,cursor,end);
  418. // allocate the output array and copy face indices
  419. if (iNumFaces)
  420. {
  421. this->mTempPoints.resize(iNumVertices);
  422. this->mFaces.resize(iNumFaces);
  423. FaceList::iterator it = this->mFaces.begin();
  424. CopyFaceIndices(it,mFileBuffer,end);
  425. }
  426. break;
  427. }
  428. // surface chunk
  429. case AI_LWO_SURF:
  430. {
  431. LoadLWOBSurface(head->length);
  432. break;
  433. }
  434. }
  435. mFileBuffer += head->length;
  436. }
  437. }
  438. // ------------------------------------------------------------------------------------------------
  439. void LWOImporter::LoadLWO2File()
  440. {
  441. }