LWOLoader.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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 float GetF8();
  98. inline uint64_t GetU8();
  99. inline uint32_t GetU4();
  100. inline uint16_t GetU2();
  101. inline uint8_t GetU1();
  102. // -------------------------------------------------------------------
  103. /** Loads a surface chunk from an LWOB file
  104. * @param size Maximum size to be read, in bytes.
  105. */
  106. void LoadLWOBSurface(unsigned int size);
  107. // -------------------------------------------------------------------
  108. /** Loads a surface chunk from an LWO2 file
  109. * @param size Maximum size to be read, in bytes.
  110. */
  111. void LoadLWO2Surface(unsigned int size);
  112. void LoadLWO3Surface(unsigned int size);
  113. // -------------------------------------------------------------------
  114. /** Loads a texture block from a LWO2 file.
  115. * @param size Maximum size to be read, in bytes.
  116. * @param head Header of the SUF.BLOK header
  117. */
  118. void LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader *head,
  119. unsigned int size);
  120. // -------------------------------------------------------------------
  121. /** Loads a shader block from a LWO2 file.
  122. * @param size Maximum size to be read, in bytes.
  123. * @param head Header of the SUF.BLOK header
  124. */
  125. void LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader *head,
  126. unsigned int size);
  127. // -------------------------------------------------------------------
  128. /** Loads an image map from a LWO2 file
  129. * @param size Maximum size to be read, in bytes.
  130. * @param tex Texture object to be filled
  131. */
  132. void LoadLWO2ImageMap(unsigned int size, LWO::Texture &tex);
  133. void LoadLWO2Gradient(unsigned int size, LWO::Texture &tex);
  134. void LoadLWO2Procedural(unsigned int size, LWO::Texture &tex);
  135. // loads the header - used by thethree functions above
  136. void LoadLWO2TextureHeader(unsigned int size, LWO::Texture &tex);
  137. // -------------------------------------------------------------------
  138. /** Loads the LWO tag list from the file
  139. * @param size Maximum size to be read, in bytes.
  140. */
  141. void LoadLWOTags(unsigned int size);
  142. // -------------------------------------------------------------------
  143. /** Load polygons from a POLS chunk
  144. * @param length Size of the chunk
  145. */
  146. void LoadLWO2Polygons(unsigned int length);
  147. void LoadLWOBPolygons(unsigned int length);
  148. // -------------------------------------------------------------------
  149. /** Load polygon tags from a PTAG chunk
  150. * @param length Size of the chunk
  151. */
  152. void LoadLWO2PolygonTags(unsigned int length);
  153. // -------------------------------------------------------------------
  154. /** Load a vertex map from a VMAP/VMAD chunk
  155. * @param length Size of the chunk
  156. * @param perPoly Operate on per-polygon base?
  157. */
  158. void LoadLWO2VertexMap(unsigned int length, bool perPoly);
  159. // -------------------------------------------------------------------
  160. /** Load polygons from a PNTS chunk
  161. * @param length Size of the chunk
  162. */
  163. void LoadLWOPoints(unsigned int length);
  164. // -------------------------------------------------------------------
  165. /** Load a clip from a CLIP chunk
  166. * @param length Size of the chunk
  167. */
  168. void LoadLWO2Clip(unsigned int length);
  169. void LoadLWO3Clip(unsigned int length);
  170. // -------------------------------------------------------------------
  171. /** Load an envelope from an EVL chunk
  172. * @param length Size of the chunk
  173. */
  174. void LoadLWO2Envelope(unsigned int length);
  175. void LoadLWO3Envelope(unsigned int length);
  176. // -------------------------------------------------------------------
  177. /** Load an nodal blocks from surface form
  178. * @param length Size of the chunk
  179. */
  180. void LoadNodalBlocks(unsigned int length);
  181. void LoadNodes(unsigned int length);
  182. void LoadNodeTag(unsigned int length);
  183. void LoadNodeData(unsigned int length);
  184. // -------------------------------------------------------------------
  185. /** Count vertices and faces in a LWOB/LWO2 file
  186. */
  187. void CountVertsAndFacesLWO2(unsigned int &verts,
  188. unsigned int &faces,
  189. uint16_t *&cursor,
  190. const uint16_t *const end,
  191. unsigned int max = UINT_MAX);
  192. void CountVertsAndFacesLWOB(unsigned int &verts,
  193. unsigned int &faces,
  194. LE_NCONST uint16_t *&cursor,
  195. const uint16_t *const end,
  196. unsigned int max = UINT_MAX);
  197. // -------------------------------------------------------------------
  198. /** Read vertices and faces in a LWOB/LWO2 file
  199. */
  200. void CopyFaceIndicesLWO2(LWO::FaceList::iterator &it,
  201. uint16_t *&cursor,
  202. const uint16_t *const end);
  203. // -------------------------------------------------------------------
  204. void CopyFaceIndicesLWOB(LWO::FaceList::iterator &it,
  205. LE_NCONST uint16_t *&cursor,
  206. const uint16_t *const end,
  207. unsigned int max = UINT_MAX);
  208. // -------------------------------------------------------------------
  209. /** Resolve the tag and surface lists that have been loaded.
  210. * Generates the mMapping table.
  211. */
  212. void ResolveTags();
  213. // -------------------------------------------------------------------
  214. /** Resolve the clip list that has been loaded.
  215. * Replaces clip references with real clips.
  216. */
  217. void ResolveClips();
  218. // -------------------------------------------------------------------
  219. /** Add a texture list to an output material description.
  220. *
  221. * @param pcMat Output material
  222. * @param in Input texture list
  223. * @param type Type identifier of the texture list
  224. */
  225. bool HandleTextures(aiMaterial *pcMat, const TextureList &in,
  226. aiTextureType type);
  227. // -------------------------------------------------------------------
  228. /** Adjust a texture path
  229. */
  230. void AdjustTexturePath(std::string &out);
  231. // -------------------------------------------------------------------
  232. /** Convert a LWO surface description to an ASSIMP material
  233. */
  234. void ConvertMaterial(const LWO::Surface &surf, aiMaterial *pcMat);
  235. // -------------------------------------------------------------------
  236. /** Get a list of all UV/VC channels required by a specific surface.
  237. *
  238. * @param surf Working surface
  239. * @param layer Working layer
  240. * @param out Output list. The members are indices into the
  241. * UV/VC channel lists of the layer
  242. */
  243. void FindUVChannels(/*const*/ LWO::Surface &surf,
  244. LWO::SortedRep &sorted,
  245. /*const*/ LWO::Layer &layer,
  246. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]);
  247. // -------------------------------------------------------------------
  248. char FindUVChannels(LWO::TextureList &list,
  249. LWO::Layer &layer, LWO::UVChannel &uv, unsigned int next);
  250. // -------------------------------------------------------------------
  251. void FindVCChannels(const LWO::Surface &surf,
  252. LWO::SortedRep &sorted,
  253. const LWO::Layer &layer,
  254. unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS]);
  255. // -------------------------------------------------------------------
  256. /** Generate the final node graph
  257. * Unused nodes are deleted.
  258. * @param apcNodes Flat list of nodes
  259. */
  260. void GenerateNodeGraph(std::map<uint16_t, aiNode *> &apcNodes);
  261. // -------------------------------------------------------------------
  262. /** Add children to a node
  263. * @param node Node to become a father
  264. * @param parent Index of the node
  265. * @param apcNodes Flat list of nodes - used nodes are set to nullptr.
  266. */
  267. void AddChildren(aiNode *node, uint16_t parent,
  268. std::vector<aiNode *> &apcNodes);
  269. // -------------------------------------------------------------------
  270. /** Read a variable sized integer
  271. * @param inout Input and output buffer
  272. */
  273. int ReadVSizedIntLWO2(uint8_t *&inout);
  274. // -------------------------------------------------------------------
  275. /** Assign a value from a VMAP to a vertex and all vertices
  276. * attached to it.
  277. * @param base VMAP destination data
  278. * @param numRead Number of float's to be read
  279. * @param idx Absolute index of the first vertex
  280. * @param data Value of the VMAP to be assigned - read numRead
  281. * floats from this array.
  282. */
  283. void DoRecursiveVMAPAssignment(VMapEntry *base, unsigned int numRead,
  284. unsigned int idx, float *data);
  285. // -------------------------------------------------------------------
  286. /** Compute normal vectors for a mesh
  287. * @param mesh Input mesh
  288. * @param smoothingGroups Smoothing-groups-per-face array
  289. * @param surface Surface for the mesh
  290. */
  291. void ComputeNormals(aiMesh *mesh, const std::vector<unsigned int> &smoothingGroups,
  292. const LWO::Surface &surface);
  293. // -------------------------------------------------------------------
  294. /** Setup a new texture after the corresponding chunk was
  295. * encountered in the file.
  296. * @param list Texture list
  297. * @param size Maximum number of bytes to be read
  298. * @return Pointer to new texture
  299. */
  300. LWO::Texture *SetupNewTextureLWOB(LWO::TextureList &list,
  301. unsigned int size);
  302. protected:
  303. /** true if the file is a LWO2 file*/
  304. bool mIsLWO2;
  305. /** true if the file is a LXOB file*/
  306. bool mIsLXOB;
  307. bool mIsLWO3;
  308. /** Temporary list of layers from the file */
  309. LayerList *mLayers;
  310. /** Pointer to the current layer */
  311. LWO::Layer *mCurLayer;
  312. /** Temporary tag list from the file */
  313. TagList *mTags;
  314. /** Mapping table to convert from tag to surface indices.
  315. UINT_MAX indicates that a no corresponding surface is available */
  316. TagMappingTable *mMapping;
  317. /** Temporary surface list from the file */
  318. SurfaceList *mSurfaces;
  319. /** Temporary clip list from the file */
  320. ClipList mClips;
  321. /** Temporary envelope list from the file */
  322. EnvelopeList mEnvelopes;
  323. /** file buffer */
  324. uint8_t *mFileBuffer;
  325. /** Size of the file, in bytes */
  326. unsigned int fileSize;
  327. /** Output scene */
  328. aiScene *mScene;
  329. /** Configuration option: speed flag set? */
  330. bool configSpeedFlag;
  331. /** Configuration option: index of layer to be loaded */
  332. unsigned int configLayerIndex;
  333. /** Configuration option: name of layer to be loaded */
  334. std::string configLayerName;
  335. /** True if we have a named layer */
  336. bool hasNamedLayer;
  337. };
  338. // ------------------------------------------------------------------------------------------------
  339. inline float LWOImporter::GetF4() {
  340. float f;
  341. ::memcpy(&f, mFileBuffer, 4);
  342. mFileBuffer += 4;
  343. AI_LSWAP4(f);
  344. return f;
  345. }
  346. inline float LWOImporter::GetF8() {
  347. double f;
  348. ::memcpy(&f, mFileBuffer, 8);
  349. mFileBuffer += 8;
  350. AI_LSWAP8(f);
  351. return (float)f;
  352. }
  353. inline uint64_t LWOImporter::GetU8() {
  354. uint64_t f;
  355. ::memcpy(&f, mFileBuffer, 8);
  356. mFileBuffer += 8;
  357. AI_LSWAP8(f);
  358. return f;
  359. }
  360. // ------------------------------------------------------------------------------------------------
  361. inline uint32_t LWOImporter::GetU4() {
  362. uint32_t f;
  363. ::memcpy(&f, mFileBuffer, 4);
  364. mFileBuffer += 4;
  365. AI_LSWAP4(f);
  366. return f;
  367. }
  368. // ------------------------------------------------------------------------------------------------
  369. inline uint16_t LWOImporter::GetU2() {
  370. uint16_t f;
  371. ::memcpy(&f, mFileBuffer, 2);
  372. mFileBuffer += 2;
  373. AI_LSWAP2(f);
  374. return f;
  375. }
  376. // ------------------------------------------------------------------------------------------------
  377. inline uint8_t LWOImporter::GetU1() {
  378. return *mFileBuffer++;
  379. }
  380. // ------------------------------------------------------------------------------------------------
  381. inline int LWOImporter::ReadVSizedIntLWO2(uint8_t *&inout) {
  382. int i;
  383. int c = *inout;
  384. inout++;
  385. if (c != 0xFF) {
  386. i = c << 8;
  387. c = *inout;
  388. inout++;
  389. i |= c;
  390. } else {
  391. c = *inout;
  392. inout++;
  393. i = c << 16;
  394. c = *inout;
  395. inout++;
  396. i |= c << 8;
  397. c = *inout;
  398. inout++;
  399. i |= c;
  400. }
  401. return i;
  402. }
  403. // ------------------------------------------------------------------------------------------------
  404. inline void LWOImporter::GetS0(std::string &out, unsigned int max) {
  405. unsigned int iCursor = 0;
  406. const char *sz = (const char *)mFileBuffer;
  407. while (*mFileBuffer) {
  408. if (++iCursor > max) {
  409. ASSIMP_LOG_WARN("LWO: Invalid file, string is is too long");
  410. break;
  411. }
  412. ++mFileBuffer;
  413. }
  414. size_t len = (size_t)((const char *)mFileBuffer - sz);
  415. out = std::string(sz, len);
  416. mFileBuffer += (len & 0x1 ? 1 : 2);
  417. }
  418. } // end of namespace Assimp
  419. #endif // AI_LWOIMPORTER_H_INCLUDED