LWOLoader.h 16 KB

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