MDLLoader.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 MDLLoader.h
  34. * @brief Declaration of the loader for MDL files
  35. */
  36. #ifndef AI_MDLLOADER_H_INCLUDED
  37. #define AI_MDLLOADER_H_INCLUDED
  38. #include "BaseImporter.h"
  39. struct aiNode;
  40. #include "MDLFileData.h"
  41. #include "HalfLifeFileData.h"
  42. namespace Assimp {
  43. using namespace MDL;
  44. // --------------------------------------------------------------------------------------
  45. // Include file/line information in debug builds
  46. #ifdef ASSIMP_BUILD_DEBUG
  47. # define VALIDATE_FILE_SIZE(msg) SizeCheck(msg,__FILE__,__LINE__)
  48. #else
  49. # define VALIDATE_FILE_SIZE(msg) SizeCheck(msg)
  50. #endif
  51. // --------------------------------------------------------------------------------------
  52. /** @brief Class to load MDL files.
  53. *
  54. * Several subformats exist:
  55. * <ul>
  56. * <li>Quake I</li>
  57. * <li>3D Game Studio MDL3, MDL4</li>
  58. * <li>3D Game Studio MDL5</li>
  59. * <li>3D Game Studio MDL7</li>
  60. * <li>Halflife 2</li>
  61. * </ul>
  62. * These formats are partially identical and it would be possible to load
  63. * them all with a single 1000-line function-beast. However, it has been
  64. * split into several code paths to make the code easier to read and maintain.
  65. */
  66. class MDLImporter : public BaseImporter
  67. {
  68. public:
  69. MDLImporter();
  70. ~MDLImporter();
  71. public:
  72. // -------------------------------------------------------------------
  73. /** Returns whether the class can handle the format of the given file.
  74. * See BaseImporter::CanRead() for details. */
  75. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  76. bool checkSig) const;
  77. // -------------------------------------------------------------------
  78. /** Called prior to ReadFile().
  79. * The function is a request to the importer to update its configuration
  80. * basing on the Importer's configuration property list.
  81. */
  82. void SetupProperties(const Importer* pImp);
  83. protected:
  84. // -------------------------------------------------------------------
  85. /** Return importer meta information.
  86. * See #BaseImporter::GetInfo for the details
  87. */
  88. const aiImporterDesc* GetInfo () const;
  89. // -------------------------------------------------------------------
  90. /** Imports the given file into the given scene structure.
  91. * See BaseImporter::InternReadFile() for details
  92. */
  93. void InternReadFile( const std::string& pFile, aiScene* pScene,
  94. IOSystem* pIOHandler);
  95. protected:
  96. // -------------------------------------------------------------------
  97. /** Import a quake 1 MDL file (IDPO)
  98. */
  99. void InternReadFile_Quake1( );
  100. // -------------------------------------------------------------------
  101. /** Import a GameStudio A4/A5 file (MDL 3,4,5)
  102. */
  103. void InternReadFile_3DGS_MDL345( );
  104. // -------------------------------------------------------------------
  105. /** Import a GameStudio A7 file (MDL 7)
  106. */
  107. void InternReadFile_3DGS_MDL7( );
  108. // -------------------------------------------------------------------
  109. /** Import a CS:S/HL2 MDL file (not fully implemented)
  110. */
  111. void InternReadFile_HL2( );
  112. // -------------------------------------------------------------------
  113. /** Check whether a given position is inside the valid range
  114. * Throw a DeadlyImportError if it is not
  115. * \param szPos Cursor position
  116. * \param szFile Name of the source file from which the function was called
  117. * \param iLine Source code line from which the function was called
  118. */
  119. void SizeCheck(const void* szPos);
  120. void SizeCheck(const void* szPos, const char* szFile, unsigned int iLine);
  121. // -------------------------------------------------------------------
  122. /** Validate the header data structure of a game studio MDL7 file
  123. * \param pcHeader Input header to be validated
  124. */
  125. void ValidateHeader_3DGS_MDL7(const MDL::Header_MDL7* pcHeader);
  126. // -------------------------------------------------------------------
  127. /** Validate the header data structure of a Quake 1 model
  128. * \param pcHeader Input header to be validated
  129. */
  130. void ValidateHeader_Quake1(const MDL::Header* pcHeader);
  131. // -------------------------------------------------------------------
  132. /** Try to load a palette from the current directory (colormap.lmp)
  133. * If it is not found the default palette of Quake1 is returned
  134. */
  135. void SearchPalette(const unsigned char** pszColorMap);
  136. // -------------------------------------------------------------------
  137. /** Free a palette created with a previous call to SearchPalette()
  138. */
  139. void FreePalette(const unsigned char* pszColorMap);
  140. // -------------------------------------------------------------------
  141. /** Load a paletized texture from the file and convert it to 32bpp
  142. */
  143. void CreateTextureARGB8_3DGS_MDL3(const unsigned char* szData);
  144. // -------------------------------------------------------------------
  145. /** Used to load textures from MDL3/4
  146. * \param szData Input data
  147. * \param iType Color data type
  148. * \param piSkip Receive: Size to skip, in bytes
  149. */
  150. void CreateTexture_3DGS_MDL4(const unsigned char* szData,
  151. unsigned int iType,
  152. unsigned int* piSkip);
  153. // -------------------------------------------------------------------
  154. /** Used to load textures from MDL5
  155. * \param szData Input data
  156. * \param iType Color data type
  157. * \param piSkip Receive: Size to skip, in bytes
  158. */
  159. void CreateTexture_3DGS_MDL5(const unsigned char* szData,
  160. unsigned int iType,
  161. unsigned int* piSkip);
  162. // -------------------------------------------------------------------
  163. /** Checks whether a texture can be replaced with a single color
  164. * This is useful for all file formats before MDL7 (all those
  165. * that are not containing material colors separate from textures).
  166. * MED seems to write dummy 8x8 monochrome images instead.
  167. * \param pcTexture Input texture
  168. * \return aiColor.r is set to qnan if the function fails and no
  169. * color can be found.
  170. */
  171. aiColor4D ReplaceTextureWithColor(const aiTexture* pcTexture);
  172. // -------------------------------------------------------------------
  173. /** Converts the absolute texture coordinates in MDL5 files to
  174. * relative in a range between 0 and 1
  175. */
  176. void CalculateUVCoordinates_MDL5();
  177. // -------------------------------------------------------------------
  178. /** Read an UV coordinate from the file. If the file format is not
  179. * MDL5, the function calculates relative texture coordinates
  180. * \param vOut Receives the output UV coord
  181. * \param pcSrc UV coordinate buffer
  182. * \param UV coordinate index
  183. */
  184. void ImportUVCoordinate_3DGS_MDL345( aiVector3D& vOut,
  185. const MDL::TexCoord_MDL3* pcSrc,
  186. unsigned int iIndex);
  187. // -------------------------------------------------------------------
  188. /** Setup the material properties for Quake and MDL<7 models.
  189. * These formats don't support more than one material per mesh,
  190. * therefore the method processes only ONE skin and removes
  191. * all others.
  192. */
  193. void SetupMaterialProperties_3DGS_MDL5_Quake1( );
  194. // -------------------------------------------------------------------
  195. /** Parse a skin lump in a MDL7/HMP7 file with all of its features
  196. * variant 1: Current cursor position is the beginning of the skin header
  197. * \param szCurrent Current data pointer
  198. * \param szCurrentOut Output data pointer
  199. * \param pcMats Material list for this group. To be filled ...
  200. */
  201. void ParseSkinLump_3DGS_MDL7(
  202. const unsigned char* szCurrent,
  203. const unsigned char** szCurrentOut,
  204. std::vector<aiMaterial*>& pcMats);
  205. // -------------------------------------------------------------------
  206. /** Parse a skin lump in a MDL7/HMP7 file with all of its features
  207. * variant 2: Current cursor position is the beginning of the skin data
  208. * \param szCurrent Current data pointer
  209. * \param szCurrentOut Output data pointer
  210. * \param pcMatOut Output material
  211. * \param iType header.typ
  212. * \param iWidth header.width
  213. * \param iHeight header.height
  214. */
  215. void ParseSkinLump_3DGS_MDL7(
  216. const unsigned char* szCurrent,
  217. const unsigned char** szCurrentOut,
  218. aiMaterial* pcMatOut,
  219. unsigned int iType,
  220. unsigned int iWidth,
  221. unsigned int iHeight);
  222. // -------------------------------------------------------------------
  223. /** Skip a skin lump in a MDL7/HMP7 file
  224. * \param szCurrent Current data pointer
  225. * \param szCurrentOut Output data pointer. Points to the byte just
  226. * behind the last byte of the skin.
  227. * \param iType header.typ
  228. * \param iWidth header.width
  229. * \param iHeight header.height
  230. */
  231. void SkipSkinLump_3DGS_MDL7(const unsigned char* szCurrent,
  232. const unsigned char** szCurrentOut,
  233. unsigned int iType,
  234. unsigned int iWidth,
  235. unsigned int iHeight);
  236. // -------------------------------------------------------------------
  237. /** Parse texture color data for MDL5, MDL6 and MDL7 formats
  238. * \param szData Current data pointer
  239. * \param iType type of the texture data. No DDS or external
  240. * \param piSkip Receive the number of bytes to skip
  241. * \param pcNew Must point to fully initialized data. Width and
  242. * height must be set. If pcNew->pcData is set to UINT_MAX,
  243. * piSkip will receive the size of the texture, in bytes, but no
  244. * color data will be read.
  245. */
  246. void ParseTextureColorData(const unsigned char* szData,
  247. unsigned int iType,
  248. unsigned int* piSkip,
  249. aiTexture* pcNew);
  250. // -------------------------------------------------------------------
  251. /** Join two materials / skins. Setup UV source ... etc
  252. * \param pcMat1 First input material
  253. * \param pcMat2 Second input material
  254. * \param pcMatOut Output material instance to be filled. Must be empty
  255. */
  256. void JoinSkins_3DGS_MDL7(aiMaterial* pcMat1,
  257. aiMaterial* pcMat2,
  258. aiMaterial* pcMatOut);
  259. // -------------------------------------------------------------------
  260. /** Add a bone transformation key to an animation
  261. * \param iTrafo Index of the transformation (always==frame index?)
  262. * No need to validate this index, it is always valid.
  263. * \param pcBoneTransforms Bone transformation for this index
  264. * \param apcOutBones Output bones array
  265. */
  266. void AddAnimationBoneTrafoKey_3DGS_MDL7(unsigned int iTrafo,
  267. const MDL::BoneTransform_MDL7* pcBoneTransforms,
  268. MDL::IntBone_MDL7** apcBonesOut);
  269. // -------------------------------------------------------------------
  270. /** Load the bone list of a MDL7 file
  271. * \return If the bones could be loaded successfully, a valid
  272. * array containing pointers to a temporary bone
  273. * representation. NULL if the bones could not be loaded.
  274. */
  275. MDL::IntBone_MDL7** LoadBones_3DGS_MDL7();
  276. // -------------------------------------------------------------------
  277. /** Load bone transformation keyframes from a file chunk
  278. * \param groupInfo -> doc of data structure
  279. * \param frame -> doc of data structure
  280. * \param shared -> doc of data structure
  281. */
  282. void ParseBoneTrafoKeys_3DGS_MDL7(
  283. const MDL::IntGroupInfo_MDL7& groupInfo,
  284. IntFrameInfo_MDL7& frame,
  285. MDL::IntSharedData_MDL7& shared);
  286. // -------------------------------------------------------------------
  287. /** Calculate absolute bone animation matrices for each bone
  288. * \param apcOutBones Output bones array
  289. */
  290. void CalcAbsBoneMatrices_3DGS_MDL7(MDL::IntBone_MDL7** apcOutBones);
  291. // -------------------------------------------------------------------
  292. /** Add all bones to the nodegraph (as children of the root node)
  293. * \param apcBonesOut List of bones
  294. * \param pcParent Parent node. New nodes will be added to this node
  295. * \param iParentIndex Index of the parent bone
  296. */
  297. void AddBonesToNodeGraph_3DGS_MDL7(const MDL::IntBone_MDL7** apcBonesOut,
  298. aiNode* pcParent,uint16_t iParentIndex);
  299. // -------------------------------------------------------------------
  300. /** Build output animations
  301. * \param apcBonesOut List of bones
  302. */
  303. void BuildOutputAnims_3DGS_MDL7(const MDL::IntBone_MDL7** apcBonesOut);
  304. // -------------------------------------------------------------------
  305. /** Handles materials that are just referencing another material
  306. * There is no test file for this feature, but Conitec's doc
  307. * say it is used.
  308. */
  309. void HandleMaterialReferences_3DGS_MDL7();
  310. // -------------------------------------------------------------------
  311. /** Copies only the material that are referenced by at least one
  312. * mesh to the final output material list. All other materials
  313. * will be discarded.
  314. * \param shared -> doc of data structure
  315. */
  316. void CopyMaterials_3DGS_MDL7(MDL::IntSharedData_MDL7 &shared);
  317. // -------------------------------------------------------------------
  318. /** Process the frame section at the end of a group
  319. * \param groupInfo -> doc of data structure
  320. * \param shared -> doc of data structure
  321. * \param szCurrent Pointer to the start of the frame section
  322. * \param szCurrentOut Receives a pointer to the first byte of the
  323. * next data section.
  324. * \return false to read no further groups (a small workaround for
  325. * some tiny and unsolved problems ... )
  326. */
  327. bool ProcessFrames_3DGS_MDL7(const MDL::IntGroupInfo_MDL7& groupInfo,
  328. MDL::IntGroupData_MDL7& groupData,
  329. MDL::IntSharedData_MDL7& shared,
  330. const unsigned char* szCurrent,
  331. const unsigned char** szCurrentOut);
  332. // -------------------------------------------------------------------
  333. /** Sort all faces by their materials. If the mesh is using
  334. * multiple materials per face (that are blended together) the function
  335. * might create new materials.
  336. * \param groupInfo -> doc of data structure
  337. * \param groupData -> doc of data structure
  338. * \param splitGroupData -> doc of data structure
  339. */
  340. void SortByMaterials_3DGS_MDL7(
  341. const MDL::IntGroupInfo_MDL7& groupInfo,
  342. MDL::IntGroupData_MDL7& groupData,
  343. MDL::IntSplitGroupData_MDL7& splitGroupData);
  344. // -------------------------------------------------------------------
  345. /** Read all faces and vertices from a MDL7 group. The function fills
  346. * preallocated memory buffers.
  347. * \param groupInfo -> doc of data structure
  348. * \param groupData -> doc of data structure
  349. */
  350. void ReadFaces_3DGS_MDL7(const MDL::IntGroupInfo_MDL7& groupInfo,
  351. MDL::IntGroupData_MDL7& groupData);
  352. // -------------------------------------------------------------------
  353. /** Generate the final output meshes for a7 models
  354. * \param groupData -> doc of data structure
  355. * \param splitGroupData -> doc of data structure
  356. */
  357. void GenerateOutputMeshes_3DGS_MDL7(
  358. MDL::IntGroupData_MDL7& groupData,
  359. MDL::IntSplitGroupData_MDL7& splitGroupData);
  360. protected:
  361. /** Configuration option: frame to be loaded */
  362. unsigned int configFrameID;
  363. /** Configuration option: palette to be used to decode palletized images*/
  364. std::string configPalette;
  365. /** Buffer to hold the loaded file */
  366. unsigned char* mBuffer;
  367. /** For GameStudio MDL files: The number in the magic word, either 3,4 or 5
  368. * (MDL7 doesn't need this, the format has a separate loader) */
  369. unsigned int iGSFileVersion;
  370. /** Output I/O handler. used to load external lmp files */
  371. IOSystem* pIOHandler;
  372. /** Output scene to be filled */
  373. aiScene* pScene;
  374. /** Size of the input file in bytes */
  375. unsigned int iFileSize;
  376. };
  377. } // end of namespace Assimp
  378. #endif // AI_3DSIMPORTER_H_INC