2
0

MD3Loader.h 9.6 KB

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