LWOLoader.h 16 KB

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