MDLLoader.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. //!
  34. //! @file Definition of MDL importer class
  35. //!
  36. #ifndef AI_MDLLOADER_H_INCLUDED
  37. #define AI_MDLLOADER_H_INCLUDED
  38. #include "BaseImporter.h"
  39. #include "../include/aiTypes.h"
  40. #include "../include/aiTexture.h"
  41. #include "../include/aiMaterial.h"
  42. struct aiNode;
  43. #include "MDLFileData.h"
  44. #include "HalfLifeFileData.h"
  45. namespace Assimp
  46. {
  47. class MaterialHelper;
  48. using namespace MDL;
  49. // ---------------------------------------------------------------------------
  50. /** Used to load MDL files
  51. */
  52. class MDLImporter : public BaseImporter
  53. {
  54. friend class Importer;
  55. protected:
  56. /** Constructor to be privately used by Importer */
  57. MDLImporter();
  58. /** Destructor, private as well */
  59. ~MDLImporter();
  60. public:
  61. // -------------------------------------------------------------------
  62. /** Returns whether the class can handle the format of the given file.
  63. * See BaseImporter::CanRead() for details. */
  64. bool CanRead( const std::string& pFile, IOSystem* pIOHandler) const;
  65. protected:
  66. // -------------------------------------------------------------------
  67. /** Called by Importer::GetExtensionList() for each loaded importer.
  68. * See BaseImporter::GetExtensionList() for details
  69. */
  70. void GetExtensionList(std::string& append)
  71. {
  72. append.append("*.mdl");
  73. }
  74. // -------------------------------------------------------------------
  75. /** Imports the given file into the given scene structure.
  76. * See BaseImporter::InternReadFile() for details
  77. */
  78. void InternReadFile( const std::string& pFile, aiScene* pScene,
  79. IOSystem* pIOHandler);
  80. protected:
  81. // -------------------------------------------------------------------
  82. /** Import a quake 1 MDL file
  83. */
  84. void InternReadFile_Quake1( );
  85. // -------------------------------------------------------------------
  86. /** Import a GameStudio A4/A5 file
  87. */
  88. void InternReadFile_GameStudio( );
  89. // -------------------------------------------------------------------
  90. /** Import a GameStudio A7 file
  91. */
  92. void InternReadFile_GameStudioA7( );
  93. // -------------------------------------------------------------------
  94. /** Import a CS:S/HL2 MDL file
  95. */
  96. void InternReadFile_HL2( );
  97. // -------------------------------------------------------------------
  98. /** Load a paletized texture from the file and convert it to 32bpp
  99. */
  100. void CreateTextureARGB8(const unsigned char* szData);
  101. // -------------------------------------------------------------------
  102. /** Used to load textures from MDL3/4
  103. * \param szData Input data
  104. * \param iType Color data type
  105. * \param piSkip Receive: Size to skip
  106. */
  107. void CreateTextureARGB8_GS4(const unsigned char* szData,
  108. unsigned int iType,
  109. unsigned int* piSkip);
  110. // -------------------------------------------------------------------
  111. /** Used to load textures from MDL5
  112. * \param szData Input data
  113. * \param iType Color data type
  114. * \param piSkip Receive: Size to skip
  115. */
  116. void CreateTextureARGB8_GS5(const unsigned char* szData,
  117. unsigned int iType,
  118. unsigned int* piSkip);
  119. // -------------------------------------------------------------------
  120. /** Parse a skin lump in a MDL7 file with all of its features
  121. * \param szCurrent Current data pointer
  122. * \param szCurrentOut Output data pointer
  123. * \param pcMats Material list for this group. To be filled ...
  124. */
  125. void ParseSkinLump_GameStudioA7(
  126. const unsigned char* szCurrent,
  127. const unsigned char** szCurrentOut,
  128. std::vector<MaterialHelper*>& pcMats);
  129. // -------------------------------------------------------------------
  130. /** Parse texture color data for MDL5, MDL6 and MDL7 formats
  131. * \param szData Current data pointer
  132. * \param iType type of the texture data. No DDS or external
  133. * \param piSkip Receive the number of bytes to skip
  134. * \param pcNew Must point to fully initialized data. Width and
  135. * height must be set.
  136. */
  137. void ParseTextureColorData(const unsigned char* szData,
  138. unsigned int iType,
  139. unsigned int* piSkip,
  140. aiTexture* pcNew);
  141. // -------------------------------------------------------------------
  142. /** Validate the header data structure of a game studio MDL7 file
  143. * \param pcHeader Input header to be validated
  144. */
  145. void ValidateHeader_GameStudioA7(const MDL::Header_MDL7* pcHeader);
  146. // -------------------------------------------------------------------
  147. /** Join two materials / skins. Setup UV source ... etc
  148. * \param pcMat1 First input material
  149. * \param pcMat2 Second input material
  150. * \param pcMatOut Output material instance to be filled. Must be empty
  151. */
  152. void JoinSkins_GameStudioA7(MaterialHelper* pcMat1,
  153. MaterialHelper* pcMat2,
  154. MaterialHelper* pcMatOut);
  155. // -------------------------------------------------------------------
  156. /** Generate the final output meshes for a7 models
  157. * \param aiSplit Face-per-material list
  158. * \param pcMats List of all materials
  159. * \param avOutList Output: List of all meshes
  160. * \param pcFaces List of all input faces
  161. * \param vPositions List of all input vectors
  162. * \param vNormals List of all input normal vectors
  163. * \param vTextureCoords1 List of all input UV coords #1
  164. * \param vTextureCoords2 List of all input UV coords #2
  165. */
  166. void GenerateOutputMeshes_GameStudioA7(
  167. const std::vector<unsigned int>** aiSplit,
  168. const std::vector<MaterialHelper*>& pcMats,
  169. std::vector<aiMesh*>& avOutList,
  170. const MDL::IntFace_MDL7* pcFaces,
  171. const std::vector<aiVector3D>& vPositions,
  172. const std::vector<aiVector3D>& vNormals,
  173. const std::vector<aiVector3D>& vTextureCoords1,
  174. const std::vector<aiVector3D>& vTextureCoords2);
  175. // -------------------------------------------------------------------
  176. /** Try to load a palette from the current directory (colormap.lmp)
  177. * If it is not found the default palette of Quake1 is returned
  178. */
  179. void SearchPalette(const unsigned char** pszColorMap);
  180. // -------------------------------------------------------------------
  181. /** Free a palette created with a previous call to SearchPalette()
  182. */
  183. void FreePalette(const unsigned char* pszColorMap);
  184. // -------------------------------------------------------------------
  185. /** Try to determine whether the normals of the model are flipped
  186. * Some MDL7 models seem to have flipped normals (and there is also
  187. * an option "flip normals" in MED). However, I don't see a proper
  188. * way to read from the file whether all normals are correctly
  189. * facing outwards ...
  190. */
  191. void FlipNormals(aiMesh* pcMesh);
  192. private:
  193. /** Header of the MDL file */
  194. const MDL::Header* m_pcHeader;
  195. /** Buffer to hold the loaded file */
  196. unsigned char* mBuffer;
  197. /** For GameStudio MDL files: The number in the magic
  198. word, either 3,4 or 5*/
  199. unsigned int iGSFileVersion;
  200. /** Output I/O handler. used to load external lmp files
  201. */
  202. IOSystem* pIOHandler;
  203. /** Output scene to be filled
  204. */
  205. aiScene* pScene;
  206. };
  207. }; // end of namespace Assimp
  208. #endif // AI_3DSIMPORTER_H_INC