You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

479 line
15 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. #ifndef AI_LWOLOADER_H_INCLUDED
  35. #define AI_LWOLOADER_H_INCLUDED
  36. #include "../include/assimp/types.h"
  37. #include "../include/assimp/DefaultLogger.hpp"
  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. // Get list of supported extensions
  75. const aiImporterDesc* GetInfo () const;
  76. // -------------------------------------------------------------------
  77. /** Imports the given file into the given scene structure.
  78. * See BaseImporter::InternReadFile() for details
  79. */
  80. void InternReadFile( const std::string& pFile, aiScene* pScene,
  81. IOSystem* pIOHandler);
  82. private:
  83. // -------------------------------------------------------------------
  84. /** Loads a LWO file in the older LWOB format (LW < 6)
  85. */
  86. void LoadLWOBFile();
  87. // -------------------------------------------------------------------
  88. /** Loads a LWO file in the newer LWO2 format (LW >= 6)
  89. */
  90. void LoadLWO2File();
  91. // -------------------------------------------------------------------
  92. /** Parsing functions used for all file format versions
  93. */
  94. inline void GetS0(std::string& out,unsigned int max);
  95. inline float GetF4();
  96. inline uint32_t GetU4();
  97. inline uint16_t GetU2();
  98. inline uint8_t GetU1();
  99. // -------------------------------------------------------------------
  100. /** Loads a surface chunk from an LWOB file
  101. * @param size Maximum size to be read, in bytes.
  102. */
  103. void LoadLWOBSurface(unsigned int size);
  104. // -------------------------------------------------------------------
  105. /** Loads a surface chunk from an LWO2 file
  106. * @param size Maximum size to be read, in bytes.
  107. */
  108. void LoadLWO2Surface(unsigned int size);
  109. // -------------------------------------------------------------------
  110. /** Loads a texture block from a LWO2 file.
  111. * @param size Maximum size to be read, in bytes.
  112. * @param head Header of the SUF.BLOK header
  113. */
  114. void LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader* head,
  115. unsigned int size );
  116. // -------------------------------------------------------------------
  117. /** Loads a shader 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 LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader* head,
  122. unsigned int size );
  123. // -------------------------------------------------------------------
  124. /** Loads an image map from a LWO2 file
  125. * @param size Maximum size to be read, in bytes.
  126. * @param tex Texture object to be filled
  127. */
  128. void LoadLWO2ImageMap(unsigned int size, LWO::Texture& tex );
  129. void LoadLWO2Gradient(unsigned int size, LWO::Texture& tex );
  130. void LoadLWO2Procedural(unsigned int size, LWO::Texture& tex );
  131. // loads the header - used by thethree functions above
  132. void LoadLWO2TextureHeader(unsigned int size, LWO::Texture& tex );
  133. // -------------------------------------------------------------------
  134. /** Loads the LWO tag list from the file
  135. * @param size Maximum size to be read, in bytes.
  136. */
  137. void LoadLWOTags(unsigned int size);
  138. // -------------------------------------------------------------------
  139. /** Load polygons from a POLS chunk
  140. * @param length Size of the chunk
  141. */
  142. void LoadLWO2Polygons(unsigned int length);
  143. void LoadLWOBPolygons(unsigned int length);
  144. // -------------------------------------------------------------------
  145. /** Load polygon tags from a PTAG chunk
  146. * @param length Size of the chunk
  147. */
  148. void LoadLWO2PolygonTags(unsigned int length);
  149. // -------------------------------------------------------------------
  150. /** Load a vertex map from a VMAP/VMAD chunk
  151. * @param length Size of the chunk
  152. * @param perPoly Operate on per-polygon base?
  153. */
  154. void LoadLWO2VertexMap(unsigned int length, bool perPoly);
  155. // -------------------------------------------------------------------
  156. /** Load polygons from a PNTS chunk
  157. * @param length Size of the chunk
  158. */
  159. void LoadLWOPoints(unsigned int length);
  160. // -------------------------------------------------------------------
  161. /** Load a clip from a CLIP chunk
  162. * @param length Size of the chunk
  163. */
  164. void LoadLWO2Clip(unsigned int length);
  165. // -------------------------------------------------------------------
  166. /** Load an envelope from an EVL chunk
  167. * @param length Size of the chunk
  168. */
  169. void LoadLWO2Envelope(unsigned int length);
  170. // -------------------------------------------------------------------
  171. /** Count vertices and faces in a LWOB/LWO2 file
  172. */
  173. void CountVertsAndFacesLWO2(unsigned int& verts,
  174. unsigned int& faces,
  175. uint16_t*& cursor,
  176. const uint16_t* const end,
  177. unsigned int max = UINT_MAX);
  178. void CountVertsAndFacesLWOB(unsigned int& verts,
  179. unsigned int& faces,
  180. LE_NCONST uint16_t*& cursor,
  181. const uint16_t* const end,
  182. unsigned int max = UINT_MAX);
  183. // -------------------------------------------------------------------
  184. /** Read vertices and faces in a LWOB/LWO2 file
  185. */
  186. void CopyFaceIndicesLWO2(LWO::FaceList::iterator& it,
  187. uint16_t*& cursor,
  188. const uint16_t* const end);
  189. // -------------------------------------------------------------------
  190. void CopyFaceIndicesLWOB(LWO::FaceList::iterator& it,
  191. LE_NCONST uint16_t*& cursor,
  192. const uint16_t* const end,
  193. unsigned int max = UINT_MAX);
  194. // -------------------------------------------------------------------
  195. /** Resolve the tag and surface lists that have been loaded.
  196. * Generates the mMapping table.
  197. */
  198. void ResolveTags();
  199. // -------------------------------------------------------------------
  200. /** Resolve the clip list that has been loaded.
  201. * Replaces clip references with real clips.
  202. */
  203. void ResolveClips();
  204. // -------------------------------------------------------------------
  205. /** Add a texture list to an output material description.
  206. *
  207. * @param pcMat Output material
  208. * @param in Input texture list
  209. * @param type Type identifier of the texture list
  210. */
  211. bool HandleTextures(aiMaterial* pcMat, const TextureList& in,
  212. aiTextureType type);
  213. // -------------------------------------------------------------------
  214. /** Adjust a texture path
  215. */
  216. void AdjustTexturePath(std::string& out);
  217. // -------------------------------------------------------------------
  218. /** Convert a LWO surface description to an ASSIMP material
  219. */
  220. void ConvertMaterial(const LWO::Surface& surf,aiMaterial* pcMat);
  221. // -------------------------------------------------------------------
  222. /** Get a list of all UV/VC channels required by a specific surface.
  223. *
  224. * @param surf Working surface
  225. * @param layer Working layer
  226. * @param out Output list. The members are indices into the
  227. * UV/VC channel lists of the layer
  228. */
  229. void FindUVChannels(/*const*/ LWO::Surface& surf,
  230. LWO::SortedRep& sorted,
  231. /*const*/ LWO::Layer& layer,
  232. unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]);
  233. // -------------------------------------------------------------------
  234. char FindUVChannels(LWO::TextureList& list,
  235. LWO::Layer& layer,LWO::UVChannel& uv, unsigned int next);
  236. // -------------------------------------------------------------------
  237. void FindVCChannels(const LWO::Surface& surf,
  238. LWO::SortedRep& sorted,
  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::map<uint16_t,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, uint16_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. /** true if the file is a LXOB file*/
  292. bool mIsLXOB;
  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. UINT_MAX 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. /** Temporary envelope list from the file */
  307. EnvelopeList mEnvelopes;
  308. /** file buffer */
  309. uint8_t* mFileBuffer;
  310. /** Size of the file, in bytes */
  311. unsigned int fileSize;
  312. /** Output scene */
  313. aiScene* pScene;
  314. /** Configuration option: speed flag set? */
  315. bool configSpeedFlag;
  316. /** Configuration option: index of layer to be loaded */
  317. unsigned int configLayerIndex;
  318. /** Configuration option: name of layer to be loaded */
  319. std::string configLayerName;
  320. /** True if we have a named layer */
  321. bool hasNamedLayer;
  322. };
  323. // ------------------------------------------------------------------------------------------------
  324. inline float LWOImporter::GetF4()
  325. {
  326. float f = *((float*)mFileBuffer);mFileBuffer += 4;
  327. AI_LSWAP4(f);
  328. return f;
  329. }
  330. // ------------------------------------------------------------------------------------------------
  331. inline uint32_t LWOImporter::GetU4()
  332. {
  333. uint32_t f = *((uint32_t*)mFileBuffer);mFileBuffer += 4;
  334. AI_LSWAP4(f);
  335. return f;
  336. }
  337. // ------------------------------------------------------------------------------------------------
  338. inline uint16_t LWOImporter::GetU2()
  339. {
  340. uint16_t f = *((uint16_t*)mFileBuffer);mFileBuffer += 2;
  341. AI_LSWAP2(f);
  342. return f;
  343. }
  344. // ------------------------------------------------------------------------------------------------
  345. inline uint8_t LWOImporter::GetU1()
  346. {
  347. return *mFileBuffer++;
  348. }
  349. // ------------------------------------------------------------------------------------------------
  350. inline int LWOImporter::ReadVSizedIntLWO2(uint8_t*& inout)
  351. {
  352. int i;
  353. int c = *inout;inout++;
  354. if(c != 0xFF)
  355. {
  356. i = c << 8;
  357. c = *inout;inout++;
  358. i |= c;
  359. }
  360. else
  361. {
  362. c = *inout;inout++;
  363. i = c << 16;
  364. c = *inout;inout++;
  365. i |= c << 8;
  366. c = *inout;inout++;
  367. i |= c;
  368. }
  369. return i;
  370. }
  371. // ------------------------------------------------------------------------------------------------
  372. inline void LWOImporter::GetS0(std::string& out,unsigned int max)
  373. {
  374. unsigned int iCursor = 0;
  375. const char*sz = (const char*)mFileBuffer;
  376. while (*mFileBuffer)
  377. {
  378. if (++iCursor > max)
  379. {
  380. DefaultLogger::get()->warn("LWO: Invalid file, string is is too long");
  381. break;
  382. }
  383. ++mFileBuffer;
  384. }
  385. size_t len = (size_t) ((const char*)mFileBuffer-sz);
  386. out = std::string(sz,len);
  387. mFileBuffer += (len&0x1 ? 1 : 2);
  388. }
  389. } // end of namespace Assimp
  390. #endif // AI_LWOIMPORTER_H_INCLUDED