LWOLoader.h 15 KB

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