LWOLoader.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Declaration of the LWO importer class. */
  34. #ifndef AI_LWOLOADER_H_INCLUDED
  35. #define AI_LWOLOADER_H_INCLUDED
  36. #include "../include/aiTypes.h"
  37. #include "../include/DefaultLogger.h"
  38. #include "LWOFileData.h"
  39. #include "BaseImporter.h"
  40. #include "MaterialSystem.h"
  41. struct aiTexture;
  42. struct aiNode;
  43. namespace Assimp {
  44. using namespace LWO;
  45. // ---------------------------------------------------------------------------
  46. /** Class to load LWO files.
  47. *
  48. * @note Methods named "xxxLWO2[xxx]" are used with the newer LWO2 format.
  49. * Methods named "xxxLWOB[xxx]" are used with the older LWOB format.
  50. * Methods named "xxxLWO[xxx]" are used with both formats.
  51. * Methods named "xxx" are used to preprocess the loaded data -
  52. * they aren't specific to one format version
  53. */
  54. // ---------------------------------------------------------------------------
  55. class LWOImporter : public BaseImporter
  56. {
  57. friend class Importer;
  58. protected:
  59. /** Constructor to be privately used by Importer */
  60. LWOImporter();
  61. /** Destructor, private as well */
  62. ~LWOImporter();
  63. public:
  64. // -------------------------------------------------------------------
  65. /** Returns whether the class can handle the format of the given file.
  66. * See BaseImporter::CanRead() for details.
  67. */
  68. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  69. bool checkSig) const;
  70. // -------------------------------------------------------------------
  71. /** Called prior to ReadFile().
  72. * The function is a request to the importer to update its configuration
  73. * basing on the Importer's configuration property list.
  74. */
  75. void SetupProperties(const Importer* pImp);
  76. protected:
  77. // -------------------------------------------------------------------
  78. /** Called by Importer::GetExtensionList() for each loaded importer.
  79. * See BaseImporter::GetExtensionList() for details
  80. */
  81. void GetExtensionList(std::set<std::string>& extensions)
  82. {
  83. extensions.insert("lxo");
  84. extensions.insert("lwo");
  85. }
  86. // -------------------------------------------------------------------
  87. /** Imports the given file into the given scene structure.
  88. * See BaseImporter::InternReadFile() for details
  89. */
  90. void InternReadFile( const std::string& pFile, aiScene* pScene,
  91. IOSystem* pIOHandler);
  92. private:
  93. // -------------------------------------------------------------------
  94. /** Loads a LWO file in the older LWOB format (LW < 6)
  95. */
  96. void LoadLWOBFile();
  97. // -------------------------------------------------------------------
  98. /** Loads a LWO file in the newer LWO2 format (LW >= 6)
  99. */
  100. void LoadLWO2File();
  101. // -------------------------------------------------------------------
  102. /** Parsing functions used for all file format versions
  103. */
  104. inline void GetS0(std::string& out,unsigned int max);
  105. inline float GetF4();
  106. inline uint32_t GetU4();
  107. inline uint16_t GetU2();
  108. inline uint8_t GetU1();
  109. // -------------------------------------------------------------------
  110. /** Loads a surface chunk from an LWOB file
  111. * @param size Maximum size to be read, in bytes.
  112. */
  113. void LoadLWOBSurface(unsigned int size);
  114. // -------------------------------------------------------------------
  115. /** Loads a surface chunk from an LWO2 file
  116. * @param size Maximum size to be read, in bytes.
  117. */
  118. void LoadLWO2Surface(unsigned int size);
  119. // -------------------------------------------------------------------
  120. /** Loads a texture block from a LWO2 file.
  121. * @param size Maximum size to be read, in bytes.
  122. * @param head Header of the SUF.BLOK header
  123. */
  124. void LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader* head,
  125. unsigned int size );
  126. // -------------------------------------------------------------------
  127. /** Loads a shader block from a LWO2 file.
  128. * @param size Maximum size to be read, in bytes.
  129. * @param head Header of the SUF.BLOK header
  130. */
  131. void LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader* head,
  132. unsigned int size );
  133. // -------------------------------------------------------------------
  134. /** Loads an image map from a LWO2 file
  135. * @param size Maximum size to be read, in bytes.
  136. * @param tex Texture object to be filled
  137. */
  138. void LoadLWO2ImageMap(unsigned int size, LWO::Texture& tex );
  139. void LoadLWO2Gradient(unsigned int size, LWO::Texture& tex );
  140. void LoadLWO2Procedural(unsigned int size, LWO::Texture& tex );
  141. // loads the header - used by thethree functions above
  142. void LoadLWO2TextureHeader(unsigned int size, LWO::Texture& tex );
  143. // -------------------------------------------------------------------
  144. /** Loads the LWO tag list from the file
  145. * @param size Maximum size to be read, in bytes.
  146. */
  147. void LoadLWOTags(unsigned int size);
  148. // -------------------------------------------------------------------
  149. /** Load polygons from a POLS chunk
  150. * @param length Size of the chunk
  151. */
  152. void LoadLWO2Polygons(unsigned int length);
  153. void LoadLWOBPolygons(unsigned int length);
  154. // -------------------------------------------------------------------
  155. /** Load polygon tags from a PTAG chunk
  156. * @param length Size of the chunk
  157. */
  158. void LoadLWO2PolygonTags(unsigned int length);
  159. // -------------------------------------------------------------------
  160. /** Load a vertex map from a VMAP/VMAD chunk
  161. * @param length Size of the chunk
  162. * @param perPoly Operate on per-polygon base?
  163. */
  164. void LoadLWO2VertexMap(unsigned int length, bool perPoly);
  165. // -------------------------------------------------------------------
  166. /** Load polygons from a PNTS chunk
  167. * @param length Size of the chunk
  168. */
  169. void LoadLWOPoints(unsigned int length);
  170. // -------------------------------------------------------------------
  171. /** Load a clip from a CLIP chunk
  172. * @param length Size of the chunk
  173. */
  174. void LoadLWO2Clip(unsigned int length);
  175. // -------------------------------------------------------------------
  176. /** Load an envelope from an EVL chunk
  177. * @param length Size of the chunk
  178. */
  179. void LoadLWO2Envelope(unsigned int length);
  180. // -------------------------------------------------------------------
  181. /** Count vertices and faces in a LWOB/LWO2 file
  182. */
  183. void CountVertsAndFacesLWO2(unsigned int& verts,
  184. unsigned int& faces,
  185. uint16_t*& cursor,
  186. const uint16_t* const end,
  187. unsigned int max = UINT_MAX);
  188. void CountVertsAndFacesLWOB(unsigned int& verts,
  189. unsigned int& faces,
  190. LE_NCONST uint16_t*& cursor,
  191. const uint16_t* const end,
  192. unsigned int max = UINT_MAX);
  193. // -------------------------------------------------------------------
  194. /** Read vertices and faces in a LWOB/LWO2 file
  195. */
  196. void CopyFaceIndicesLWO2(LWO::FaceList::iterator& it,
  197. uint16_t*& cursor,
  198. const uint16_t* const end);
  199. // -------------------------------------------------------------------
  200. void CopyFaceIndicesLWOB(LWO::FaceList::iterator& it,
  201. LE_NCONST uint16_t*& cursor,
  202. const uint16_t* const end,
  203. unsigned int max = UINT_MAX);
  204. // -------------------------------------------------------------------
  205. /** Resolve the tag and surface lists that have been loaded.
  206. * Generates the mMapping table.
  207. */
  208. void ResolveTags();
  209. // -------------------------------------------------------------------
  210. /** Resolve the clip list that has been loaded.
  211. * Replaces clip references with real clips.
  212. */
  213. void ResolveClips();
  214. // -------------------------------------------------------------------
  215. /** Add a texture list to an output material description.
  216. *
  217. * @param pcMat Output material
  218. * @param in Input texture list
  219. * @param type Type identifier of the texture list
  220. */
  221. bool HandleTextures(MaterialHelper* pcMat, const TextureList& in,
  222. aiTextureType type);
  223. // -------------------------------------------------------------------
  224. /** Adjust a texture path
  225. */
  226. void AdjustTexturePath(std::string& out);
  227. // -------------------------------------------------------------------
  228. /** Convert a LWO surface description to an ASSIMP material
  229. */
  230. void ConvertMaterial(const LWO::Surface& surf,MaterialHelper* pcMat);
  231. // -------------------------------------------------------------------
  232. /** Get a list of all UV/VC channels required by a specific surface.
  233. *
  234. * @param surf Working surface
  235. * @param layer Working layer
  236. * @param out Output list. The members are indices into the
  237. * UV/VC channel lists of the layer
  238. */
  239. void FindUVChannels(/*const*/ LWO::Surface& surf,
  240. LWO::SortedRep& sorted,
  241. /*const*/ LWO::Layer& layer,
  242. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]);
  243. // -------------------------------------------------------------------
  244. char FindUVChannels(LWO::TextureList& list,
  245. LWO::Layer& layer,LWO::UVChannel& uv, unsigned int next);
  246. // -------------------------------------------------------------------
  247. void FindVCChannels(const LWO::Surface& surf,
  248. LWO::SortedRep& sorted,
  249. const LWO::Layer& layer,
  250. unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS]);
  251. // -------------------------------------------------------------------
  252. /** Generate the final node graph
  253. * Unused nodes are deleted.
  254. * @param apcNodes Flat list of nodes
  255. */
  256. void GenerateNodeGraph(std::vector<aiNode*>& apcNodes);
  257. // -------------------------------------------------------------------
  258. /** Add children to a node
  259. * @param node Node to become a father
  260. * @param parent Index of the node
  261. * @param apcNodes Flat list of nodes - used nodes are set to NULL.
  262. */
  263. void AddChildren(aiNode* node, uint16_t parent,
  264. std::vector<aiNode*>& apcNodes);
  265. // -------------------------------------------------------------------
  266. /** Read a variable sized integer
  267. * @param inout Input and output buffer
  268. */
  269. int ReadVSizedIntLWO2(uint8_t*& inout);
  270. // -------------------------------------------------------------------
  271. /** Assign a value from a VMAP to a vertex and all vertices
  272. * attached to it.
  273. * @param base VMAP destination data
  274. * @param numRead Number of float's to be read
  275. * @param idx Absolute index of the first vertex
  276. * @param data Value of the VMAP to be assigned - read numRead
  277. * floats from this array.
  278. */
  279. void DoRecursiveVMAPAssignment(VMapEntry* base, unsigned int numRead,
  280. unsigned int idx, float* data);
  281. // -------------------------------------------------------------------
  282. /** Compute normal vectors for a mesh
  283. * @param mesh Input mesh
  284. * @param smoothingGroups Smoothing-groups-per-face array
  285. * @param surface Surface for the mesh
  286. */
  287. void ComputeNormals(aiMesh* mesh, const std::vector<unsigned int>& smoothingGroups,
  288. const LWO::Surface& surface);
  289. // -------------------------------------------------------------------
  290. /** Setup a new texture after the corresponding chunk was
  291. * encountered in the file.
  292. * @param list Texture list
  293. * @param size Maximum number of bytes to be read
  294. * @return Pointer to new texture
  295. */
  296. LWO::Texture* SetupNewTextureLWOB(LWO::TextureList& list,
  297. unsigned int size);
  298. protected:
  299. /** true if the file is a LWO2 file*/
  300. bool mIsLWO2;
  301. /** true if the file is a LXOB file*/
  302. bool mIsLXOB;
  303. /** Temporary list of layers from the file */
  304. LayerList* mLayers;
  305. /** Pointer to the current layer */
  306. LWO::Layer* mCurLayer;
  307. /** Temporary tag list from the file */
  308. TagList* mTags;
  309. /** Mapping table to convert from tag to surface indices.
  310. UINT_MAX indicates that a no corresponding surface is available */
  311. TagMappingTable* mMapping;
  312. /** Temporary surface list from the file */
  313. SurfaceList* mSurfaces;
  314. /** Temporary clip list from the file */
  315. ClipList mClips;
  316. /** Temporary envelope list from the file */
  317. EnvelopeList mEnvelopes;
  318. /** file buffer */
  319. uint8_t* mFileBuffer;
  320. /** Size of the file, in bytes */
  321. unsigned int fileSize;
  322. /** Output scene */
  323. aiScene* pScene;
  324. /** Configuration option: speed flag set? */
  325. bool configSpeedFlag;
  326. /** Configuration option: index of layer to be loaded */
  327. unsigned int configLayerIndex;
  328. /** Configuration option: name of layer to be loaded */
  329. std::string configLayerName;
  330. /** True if we have a named layer */
  331. bool hasNamedLayer;
  332. };
  333. // ------------------------------------------------------------------------------------------------
  334. inline float LWOImporter::GetF4()
  335. {
  336. float f = *((float*)mFileBuffer);mFileBuffer += 4;
  337. AI_LSWAP4(f);
  338. return f;
  339. }
  340. // ------------------------------------------------------------------------------------------------
  341. inline uint32_t LWOImporter::GetU4()
  342. {
  343. uint32_t f = *((uint32_t*)mFileBuffer);mFileBuffer += 4;
  344. AI_LSWAP4(f);
  345. return f;
  346. }
  347. // ------------------------------------------------------------------------------------------------
  348. inline uint16_t LWOImporter::GetU2()
  349. {
  350. uint16_t f = *((uint16_t*)mFileBuffer);mFileBuffer += 2;
  351. AI_LSWAP2(f);
  352. return f;
  353. }
  354. // ------------------------------------------------------------------------------------------------
  355. inline uint8_t LWOImporter::GetU1()
  356. {
  357. return *mFileBuffer++;
  358. }
  359. // ------------------------------------------------------------------------------------------------
  360. inline int LWOImporter::ReadVSizedIntLWO2(uint8_t*& inout)
  361. {
  362. int i;
  363. int c = *inout;inout++;
  364. if(c != 0xFF)
  365. {
  366. i = c << 8;
  367. c = *inout;inout++;
  368. i |= c;
  369. }
  370. else
  371. {
  372. c = *inout;inout++;
  373. i = c << 16;
  374. c = *inout;inout++;
  375. i |= c << 8;
  376. c = *inout;inout++;
  377. i |= c;
  378. }
  379. return i;
  380. }
  381. // ------------------------------------------------------------------------------------------------
  382. inline void LWOImporter::GetS0(std::string& out,unsigned int max)
  383. {
  384. unsigned int iCursor = 0;
  385. const char*sz = (const char*)mFileBuffer;
  386. while (*mFileBuffer)
  387. {
  388. if (++iCursor > max)
  389. {
  390. DefaultLogger::get()->warn("LWO: Invalid file, string is is too long");
  391. break;
  392. }
  393. ++mFileBuffer;
  394. }
  395. size_t len = (size_t) ((const char*)mFileBuffer-sz);
  396. out = std::string(sz,len);
  397. mFileBuffer += (len&0x1 ? 1 : 2);
  398. }
  399. } // end of namespace Assimp
  400. #endif // AI_LWOIMPORTER_H_INCLUDED