LWOLoader.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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");
  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. This is the string
  212. * that appears in the middle of all material keys - e.g. "diffuse",
  213. * "shininess", "glossiness" or "specular".
  214. */
  215. bool HandleTextures(MaterialHelper* pcMat, const TextureList& in,
  216. const char* type);
  217. // -------------------------------------------------------------------
  218. /** Adjust a texture path
  219. */
  220. void AdjustTexturePath(std::string& out);
  221. // -------------------------------------------------------------------
  222. /** Convert a LWO surface description to an ASSIMP material
  223. */
  224. void ConvertMaterial(const LWO::Surface& surf,MaterialHelper* pcMat);
  225. // -------------------------------------------------------------------
  226. /** Get a list of all UV/VC channels required by a specific surface.
  227. *
  228. * @param surf Working surface
  229. * @param layer Working layer
  230. * @param out Output list. The members are indices into the
  231. * UV/VC channel lists of the layer
  232. */
  233. void FindUVChannels(/*const*/ LWO::Surface& surf,
  234. /*const*/ LWO::Layer& layer,
  235. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]);
  236. // -------------------------------------------------------------------
  237. void FindUVChannels(LWO::TextureList& list, LWO::Layer& layer,
  238. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS], unsigned int& next);
  239. // -------------------------------------------------------------------
  240. void FindVCChannels(const LWO::Surface& surf,
  241. const LWO::Layer& layer,
  242. unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS]);
  243. // -------------------------------------------------------------------
  244. /** Generate the final node graph
  245. * Unused nodes are deleted.
  246. * @param apcNodes Flat list of nodes
  247. */
  248. void GenerateNodeGraph(std::vector<aiNode*>& apcNodes);
  249. // -------------------------------------------------------------------
  250. /** Add children to a node
  251. * @param node Node to become a father
  252. * @param parent Index of the node
  253. * @param apcNodes Flat list of nodes - used nodes are set to NULL.
  254. */
  255. void AddChildren(aiNode* node, uintptr_t parent,
  256. std::vector<aiNode*>& apcNodes);
  257. // -------------------------------------------------------------------
  258. /** Read a variable sized integer
  259. * @param inout Input and output buffer
  260. */
  261. int ReadVSizedIntLWO2(uint8_t*& inout);
  262. // -------------------------------------------------------------------
  263. /** Assign a value from a VMAP to a vertex and all vertices
  264. * attached to it.
  265. * @param base VMAP destination data
  266. * @param numRead Number of float's to be read
  267. * @param idx Absolute index of the first vertex
  268. * @param data Value of the VMAP to be assigned - read numRead
  269. * floats from this array.
  270. */
  271. void DoRecursiveVMAPAssignment(VMapEntry* base, unsigned int numRead,
  272. unsigned int idx, float* data);
  273. // -------------------------------------------------------------------
  274. /** Compute normal vectors for a mesh
  275. * @param mesh Input mesh
  276. * @param smoothingGroups Smoothing-groups-per-face array
  277. * @param surface Surface for the mesh
  278. */
  279. void ComputeNormals(aiMesh* mesh, const std::vector<unsigned int>& smoothingGroups,
  280. const LWO::Surface& surface);
  281. // -------------------------------------------------------------------
  282. /** Setup a new texture after the corresponding chunk was
  283. * encountered in the file.
  284. * @param list Texture list
  285. * @param size Maximum number of bytes to be read
  286. * @return Pointer to new texture
  287. */
  288. LWO::Texture* SetupNewTextureLWOB(LWO::TextureList& list,
  289. unsigned int size);
  290. protected:
  291. /** true if the file is a LWO2 file*/
  292. bool mIsLWO2;
  293. /** Temporary list of layers from the file */
  294. LayerList* mLayers;
  295. /** Pointer to the current layer */
  296. LWO::Layer* mCurLayer;
  297. /** Temporary tag list from the file */
  298. TagList* mTags;
  299. /** Mapping table to convert from tag to surface indices.
  300. 0xffffffff indicates that a no corresponding surface is available */
  301. TagMappingTable* mMapping;
  302. /** Temporary surface list from the file */
  303. SurfaceList* mSurfaces;
  304. /** Temporary clip list from the file */
  305. ClipList mClips;
  306. /** file buffer */
  307. uint8_t* mFileBuffer;
  308. /** Size of the file, in bytes */
  309. unsigned int fileSize;
  310. /** Output scene */
  311. aiScene* pScene;
  312. bool configSpeedFlag;
  313. unsigned int configLayerIndex;
  314. std::string configLayerName;
  315. };
  316. // ------------------------------------------------------------------------------------------------
  317. inline float LWOImporter::GetF4()
  318. {
  319. float f = *((float*)mFileBuffer);mFileBuffer += 4;
  320. AI_LSWAP4(f);
  321. return f;
  322. }
  323. // ------------------------------------------------------------------------------------------------
  324. inline uint32_t LWOImporter::GetU4()
  325. {
  326. uint32_t f = *((uint32_t*)mFileBuffer);mFileBuffer += 4;
  327. AI_LSWAP4(f);
  328. return f;
  329. }
  330. // ------------------------------------------------------------------------------------------------
  331. inline uint16_t LWOImporter::GetU2()
  332. {
  333. uint16_t f = *((uint16_t*)mFileBuffer);mFileBuffer += 2;
  334. AI_LSWAP2(f);
  335. return f;
  336. }
  337. // ------------------------------------------------------------------------------------------------
  338. inline uint8_t LWOImporter::GetU1()
  339. {
  340. return *mFileBuffer++;
  341. }
  342. // ------------------------------------------------------------------------------------------------
  343. inline int LWOImporter::ReadVSizedIntLWO2(uint8_t*& inout)
  344. {
  345. int i;
  346. int c = *inout;inout++;
  347. if(c != 0xFF)
  348. {
  349. i = c << 8;
  350. c = *inout;inout++;
  351. i |= c;
  352. }
  353. else
  354. {
  355. c = *inout;inout++;
  356. i = c << 16;
  357. c = *inout;inout++;
  358. i |= c << 8;
  359. c = *inout;inout++;
  360. i |= c;
  361. }
  362. return i;
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. inline void LWOImporter::GetS0(std::string& out,unsigned int max)
  366. {
  367. unsigned int iCursor = 0;
  368. const char*sz = (const char*)mFileBuffer;
  369. while (*mFileBuffer)
  370. {
  371. if (++iCursor > max)
  372. {
  373. DefaultLogger::get()->warn("LWO: Invalid file, string is is too long");
  374. break;
  375. }
  376. ++mFileBuffer;
  377. }
  378. unsigned int len = (unsigned int) ((const char*)mFileBuffer-sz);
  379. out = std::string(sz,len);
  380. mFileBuffer += (len&0x1 ? 1 : 2);
  381. }
  382. } // end of namespace Assimp
  383. #endif // AI_LWOIMPORTER_H_INCLUDED