MD3Loader.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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 Md3Loader.h
  34. * @brief Declaration of the .MD3 importer class.
  35. */
  36. #ifndef AI_MD3LOADER_H_INCLUDED
  37. #define AI_MD3LOADER_H_INCLUDED
  38. #include "MD3FileData.h"
  39. #include <assimp/BaseImporter.h>
  40. #include <assimp/ByteSwapper.h>
  41. #include <assimp/StringComparison.h>
  42. #include <assimp/types.h>
  43. #include <list>
  44. struct aiMaterial;
  45. namespace Assimp {
  46. using namespace MD3;
  47. namespace Q3Shader {
  48. // ---------------------------------------------------------------------------
  49. /** @brief Tiny utility data structure to hold the data of a .skin file
  50. */
  51. struct SkinData {
  52. //! A single entry in texture list
  53. struct TextureEntry : public std::pair<std::string, std::string> {
  54. // did we resolve this texture entry?
  55. bool resolved;
  56. // for std::find()
  57. bool operator==(const std::string &f) const {
  58. return f == first;
  59. }
  60. };
  61. //! List of textures
  62. std::list<TextureEntry> textures;
  63. // rest is ignored for the moment
  64. };
  65. // ---------------------------------------------------------------------------
  66. /** @brief Specifies cull modi for Quake shader files.
  67. */
  68. enum ShaderCullMode {
  69. CULL_NONE,
  70. CULL_CW,
  71. CULL_CCW
  72. };
  73. // ---------------------------------------------------------------------------
  74. /** @brief Specifies alpha blend modi (src + dest) for Quake shader files
  75. */
  76. enum BlendFunc {
  77. BLEND_NONE,
  78. BLEND_GL_ONE,
  79. BLEND_GL_ZERO,
  80. BLEND_GL_DST_COLOR,
  81. BLEND_GL_ONE_MINUS_DST_COLOR,
  82. BLEND_GL_SRC_ALPHA,
  83. BLEND_GL_ONE_MINUS_SRC_ALPHA
  84. };
  85. // ---------------------------------------------------------------------------
  86. /** @brief Specifies alpha test modi for Quake texture maps
  87. */
  88. enum AlphaTestFunc {
  89. AT_NONE,
  90. AT_GT0,
  91. AT_LT128,
  92. AT_GE128
  93. };
  94. // ---------------------------------------------------------------------------
  95. /** @brief Tiny utility data structure to hold a .shader map data block
  96. */
  97. struct ShaderMapBlock {
  98. ShaderMapBlock() AI_NO_EXCEPT
  99. : blend_src(BLEND_NONE),
  100. blend_dest(BLEND_NONE),
  101. alpha_test(AT_NONE) {}
  102. //! Name of referenced map
  103. std::string name;
  104. //! Blend and alpha test settings for texture
  105. BlendFunc blend_src, blend_dest;
  106. AlphaTestFunc alpha_test;
  107. //! For std::find()
  108. bool operator==(const std::string &o) const {
  109. return !ASSIMP_stricmp(o, name);
  110. }
  111. };
  112. // ---------------------------------------------------------------------------
  113. /** @brief Tiny utility data structure to hold a .shader data block
  114. */
  115. struct ShaderDataBlock {
  116. ShaderDataBlock() AI_NO_EXCEPT
  117. : cull(CULL_CW) {}
  118. //! Name of referenced data element
  119. std::string name;
  120. //! Cull mode for the element
  121. ShaderCullMode cull;
  122. //! Maps defined in the shader
  123. std::list<ShaderMapBlock> maps;
  124. //! For std::find()
  125. bool operator==(const std::string &o) const {
  126. return !ASSIMP_stricmp(o, name);
  127. }
  128. };
  129. // ---------------------------------------------------------------------------
  130. /** @brief Tiny utility data structure to hold the data of a .shader file
  131. */
  132. struct ShaderData {
  133. //! Shader data blocks
  134. std::list<ShaderDataBlock> blocks;
  135. };
  136. // ---------------------------------------------------------------------------
  137. /** @brief Load a shader file
  138. *
  139. * Generally, parsing is error tolerant. There's no failure.
  140. * @param fill Receives output data
  141. * @param file File to be read.
  142. * @param io IOSystem to be used for reading
  143. * @return false if file is not accessible
  144. */
  145. bool LoadShader(ShaderData &fill, const std::string &file, IOSystem *io);
  146. // ---------------------------------------------------------------------------
  147. /** @brief Convert a Q3Shader to an aiMaterial
  148. *
  149. * @param[out] out Material structure to be filled.
  150. * @param[in] shader Input shader
  151. */
  152. void ConvertShaderToMaterial(aiMaterial *out, const ShaderDataBlock &shader);
  153. // ---------------------------------------------------------------------------
  154. /** @brief Load a skin file
  155. *
  156. * Generally, parsing is error tolerant. There's no failure.
  157. * @param fill Receives output data
  158. * @param file File to be read.
  159. * @param io IOSystem to be used for reading
  160. * @return false if file is not accessible
  161. */
  162. bool LoadSkin(SkinData &fill, const std::string &file, IOSystem *io);
  163. } // namespace Q3Shader
  164. // ---------------------------------------------------------------------------
  165. /** @brief Importer class to load MD3 files
  166. */
  167. class MD3Importer : public BaseImporter {
  168. public:
  169. MD3Importer();
  170. ~MD3Importer() override;
  171. // -------------------------------------------------------------------
  172. /** Returns whether the class can handle the format of the given file.
  173. * See BaseImporter::CanRead() for details. */
  174. bool CanRead(const std::string &pFile, IOSystem *pIOHandler,
  175. bool checkSig) const override;
  176. // -------------------------------------------------------------------
  177. /** Called prior to ReadFile().
  178. * The function is a request to the importer to update its configuration
  179. * basing on the Importer's configuration property list.
  180. */
  181. void SetupProperties(const Importer *pImp) override;
  182. protected:
  183. // -------------------------------------------------------------------
  184. /** Return importer meta information.
  185. * See #BaseImporter::GetInfo for the details
  186. */
  187. const aiImporterDesc *GetInfo() const override;
  188. // -------------------------------------------------------------------
  189. /** Imports the given file into the given scene structure.
  190. * See BaseImporter::InternReadFile() for details
  191. */
  192. void InternReadFile(const std::string &pFile, aiScene *pScene,
  193. IOSystem *pIOHandler) override;
  194. // -------------------------------------------------------------------
  195. /** Validate offsets in the header
  196. */
  197. void ValidateHeaderOffsets();
  198. void ValidateSurfaceHeaderOffsets(const MD3::Surface *pcSurfHeader);
  199. // -------------------------------------------------------------------
  200. /** Read a Q3 multipart file
  201. * @return true if multi part has been processed
  202. */
  203. bool ReadMultipartFile();
  204. // -------------------------------------------------------------------
  205. /** Try to read the skin for a MD3 file
  206. * @param fill Receives output information
  207. */
  208. void ReadSkin(Q3Shader::SkinData &fill) const;
  209. // -------------------------------------------------------------------
  210. /** Try to read the shader for a MD3 file
  211. * @param fill Receives output information
  212. */
  213. void ReadShader(Q3Shader::ShaderData &fill) const;
  214. // -------------------------------------------------------------------
  215. /** Convert a texture path in a MD3 file to a proper value
  216. * @param[in] texture_name Path to be converted
  217. * @param[in] header_path Base path specified in MD3 header
  218. * @param[out] out Receives the converted output string
  219. */
  220. void ConvertPath(const char *texture_name, const char *header_path,
  221. std::string &out) const;
  222. protected:
  223. /** Configuration option: frame to be loaded */
  224. unsigned int configFrameID;
  225. /** Configuration option: process multi-part files */
  226. bool configHandleMP;
  227. /** Configuration option: name of skin file to be read */
  228. std::string configSkinFile;
  229. /** Configuration option: whether to load shaders */
  230. bool configLoadShaders;
  231. /** Configuration option: name or path of shader */
  232. std::string configShaderFile;
  233. /** Configuration option: speed flag was set? */
  234. bool configSpeedFlag;
  235. /** Header of the MD3 file */
  236. BE_NCONST MD3::Header *pcHeader;
  237. /** File buffer */
  238. BE_NCONST unsigned char *mBuffer;
  239. /** Size of the file, in bytes */
  240. unsigned int fileSize;
  241. /** Current file name */
  242. std::string mFile;
  243. /** Current base directory */
  244. std::string path;
  245. /** Pure file we're currently reading */
  246. std::string filename;
  247. /** Output scene to be filled */
  248. aiScene *mScene;
  249. /** IO system to be used to access the data*/
  250. IOSystem *mIOHandler;
  251. };
  252. } // end of namespace Assimp
  253. #endif // AI_3DSIMPORTER_H_INC