SMDLoader.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 SMDLoader.h
  34. * @brief Definition of the Valve SMD file format
  35. */
  36. #pragma once
  37. #ifndef AI_SMDLOADER_H_INCLUDED
  38. #define AI_SMDLOADER_H_INCLUDED
  39. #include <assimp/BaseImporter.h>
  40. #include <assimp/ParsingUtils.h>
  41. #include <assimp/types.h>
  42. #include <assimp/texture.h>
  43. #include <assimp/anim.h>
  44. #include <assimp/material.h>
  45. #include <vector>
  46. struct aiNode;
  47. namespace Assimp {
  48. namespace SMD {
  49. // ---------------------------------------------------------------------------
  50. /** Data structure for a vertex in a SMD file
  51. */
  52. struct Vertex {
  53. Vertex() AI_NO_EXCEPT : iParentNode(UINT_MAX) {
  54. // empty
  55. }
  56. //! Vertex position, normal and texture coordinate
  57. aiVector3D pos,nor,uv;
  58. //! Vertex parent node
  59. unsigned int iParentNode;
  60. //! Links to bones: pair.first is the bone index,
  61. //! pair.second is the vertex weight.
  62. //! WARN: The remaining weight (to reach 1.0f) is assigned
  63. //! to the parent node/bone
  64. std::vector<std::pair<unsigned int, float> > aiBoneLinks;
  65. };
  66. // ---------------------------------------------------------------------------
  67. /** Data structure for a face in a SMD file
  68. */
  69. struct Face {
  70. Face() AI_NO_EXCEPT :
  71. iTexture(0x0) {
  72. // empty
  73. }
  74. //! Texture index for the face
  75. unsigned int iTexture;
  76. //! The three vertices of the face
  77. Vertex avVertices[3];
  78. };
  79. // ---------------------------------------------------------------------------
  80. /** Data structure for a bone in a SMD file
  81. */
  82. struct Bone {
  83. //! Default constructor
  84. Bone() AI_NO_EXCEPT : iParent(UINT_MAX), bIsUsed(false) {
  85. // empty
  86. }
  87. //! Name of the bone
  88. std::string mName;
  89. //! Parent of the bone
  90. uint32_t iParent;
  91. //! Animation of the bone
  92. struct Animation {
  93. //! Public default constructor
  94. Animation() AI_NO_EXCEPT : iFirstTimeKey() {
  95. asKeys.reserve(20);
  96. }
  97. //! Data structure for a matrix key
  98. struct MatrixKey {
  99. //! Matrix at this time
  100. aiMatrix4x4 matrix;
  101. //! Absolute transformation matrix
  102. aiMatrix4x4 matrixAbsolute;
  103. //! Position
  104. aiVector3D vPos;
  105. //! Rotation (euler angles)
  106. aiVector3D vRot;
  107. //! Current time. may be negative, this
  108. //! will be fixed later
  109. double dTime;
  110. };
  111. //! Index of the key with the smallest time value
  112. uint32_t iFirstTimeKey;
  113. //! Array of matrix keys
  114. std::vector<MatrixKey> asKeys;
  115. } sAnim;
  116. //! Offset matrix of the bone
  117. aiMatrix4x4 mOffsetMatrix;
  118. //! true if the bone is referenced by at least one mesh
  119. bool bIsUsed;
  120. };
  121. } //! namespace SMD
  122. // ---------------------------------------------------------------------------
  123. /** Used to load Half-life 1 and 2 SMD models
  124. */
  125. class ASSIMP_API SMDImporter : public BaseImporter {
  126. public:
  127. SMDImporter();
  128. ~SMDImporter() override = default;
  129. // -------------------------------------------------------------------
  130. /** Returns whether the class can handle the format of the given file.
  131. * See BaseImporter::CanRead() for details.
  132. */
  133. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  134. bool checkSig) const override;
  135. // -------------------------------------------------------------------
  136. /** Called prior to ReadFile().
  137. * The function is a request to the importer to update its configuration
  138. * basing on the Importer's configuration property list.
  139. */
  140. void SetupProperties(const Importer* pImp) override;
  141. protected:
  142. // -------------------------------------------------------------------
  143. /** Return importer meta information.
  144. * See #BaseImporter::GetInfo for the details
  145. */
  146. const aiImporterDesc* GetInfo () const override;
  147. // -------------------------------------------------------------------
  148. /** Imports the given file into the given scene structure.
  149. * See BaseImporter::InternReadFile() for details
  150. */
  151. void InternReadFile( const std::string& pFile, aiScene* pScene,
  152. IOSystem* pIOHandler) override;
  153. // -------------------------------------------------------------------
  154. /** Parse the SMD file and create the output scene
  155. */
  156. void ParseFile();
  157. void ReadSmd(const std::string &pFile, IOSystem* pIOHandler);
  158. // -------------------------------------------------------------------
  159. /** Parse the triangles section of the SMD file
  160. * \param szCurrent Current position in the file. Points to the first
  161. * data line of the section.
  162. * \param szCurrentOut Receives a pointer to the heading line of
  163. * the next section (or to EOF)
  164. */
  165. void ParseTrianglesSection(const char* szCurrent,
  166. const char **szCurrentOut, const char *end);
  167. // -------------------------------------------------------------------
  168. /** Parse the vertex animation section in VTA files
  169. * \param szCurrent Current position in the file. Points to the first
  170. * data line of the section.
  171. * \param szCurrentOut Receives a pointer to the heading line of
  172. * the next section (or to EOF)
  173. */
  174. void ParseVASection(const char* szCurrent,
  175. const char **szCurrentOu, const char *end);
  176. // -------------------------------------------------------------------
  177. /** Parse the nodes section of the SMD file
  178. * \param szCurrent Current position in the file. Points to the first
  179. * data line of the section.
  180. * \param szCurrentOut Receives a pointer to the heading line of
  181. * the next section (or to EOF)
  182. */
  183. void ParseNodesSection(const char* szCurrent,
  184. const char **szCurrentOut, const char *end);
  185. // -------------------------------------------------------------------
  186. /** Parse the skeleton section of the SMD file
  187. * \param szCurrent Current position in the file. Points to the first
  188. * data line of the section.
  189. * \param szCurrentOut Receives a pointer to the heading line of
  190. * the next section (or to EOF)
  191. */
  192. void ParseSkeletonSection(const char* szCurrent,
  193. const char **szCurrentOut, const char *end);
  194. // -------------------------------------------------------------------
  195. /** Parse a single triangle in the SMD file
  196. * \param szCurrent Current position in the file. Points to the first
  197. * data line of the section.
  198. * \param szCurrentOut Receives the output cursor position
  199. */
  200. void ParseTriangle(const char* szCurrent,
  201. const char **szCurrentOut, const char *end);
  202. // -------------------------------------------------------------------
  203. /** Parse a single vertex in the SMD file
  204. * \param szCurrent Current position in the file. Points to the first
  205. * data line of the section.
  206. * \param szCurrentOut Receives the output cursor position
  207. * \param vertex Vertex to be filled
  208. */
  209. void ParseVertex(const char* szCurrent,
  210. const char **szCurrentOut, const char *end, SMD::Vertex &vertex,
  211. bool bVASection = false);
  212. // -------------------------------------------------------------------
  213. /** Get the index of a texture. If the texture was not yet known
  214. * it will be added to the internal texture list.
  215. * \param filename Name of the texture
  216. * \return Value texture index
  217. */
  218. unsigned int GetTextureIndex(const std::string& filename);
  219. // -------------------------------------------------------------------
  220. /** Parse a line in the skeleton section
  221. */
  222. void ParseSkeletonElement(const char* szCurrent,
  223. const char **szCurrentOut, const char *end, int iTime);
  224. // -------------------------------------------------------------------
  225. /** Parse a line in the nodes section
  226. */
  227. void ParseNodeInfo(const char* szCurrent,
  228. const char **szCurrentOut, const char *end);
  229. // -------------------------------------------------------------------
  230. /** Parse a floating-point value
  231. */
  232. bool ParseFloat(const char* szCurrent,
  233. const char **szCurrentOut, const char *end, float &out);
  234. // -------------------------------------------------------------------
  235. /** Parse an unsigned integer. There may be no sign!
  236. */
  237. bool ParseUnsignedInt(const char* szCurrent,
  238. const char **szCurrentOut, const char *end, unsigned int &out);
  239. // -------------------------------------------------------------------
  240. /** Parse a signed integer. Signs (+,-) are handled.
  241. */
  242. bool ParseSignedInt(const char* szCurrent,
  243. const char **szCurrentOut, const char *end, int &out);
  244. // -------------------------------------------------------------------
  245. /** Fix invalid time values in the file
  246. */
  247. void FixTimeValues();
  248. // -------------------------------------------------------------------
  249. /** Add all children of a bone as sub-nodes to a node
  250. * \param pcNode Parent node
  251. * \param iParent Parent bone index
  252. */
  253. void AddBoneChildren(aiNode* pcNode, uint32_t iParent);
  254. // -------------------------------------------------------------------
  255. /** Build output meshes/materials/nodes/animations
  256. */
  257. void CreateOutputMeshes();
  258. void CreateOutputNodes();
  259. void CreateOutputAnimations(const std::string &pFile, IOSystem* pIOHandler);
  260. void CreateOutputAnimation(int index, const std::string &name);
  261. void GetAnimationFileList(const std::string &pFile, IOSystem* pIOHandler, std::vector<std::tuple<std::string, std::string>>& outList);
  262. void CreateOutputMaterials();
  263. // -------------------------------------------------------------------
  264. /** Print a log message together with the current line number
  265. */
  266. void LogErrorNoThrow(const char* msg);
  267. void LogWarning(const char* msg);
  268. // -------------------------------------------------------------------
  269. inline bool SkipLine( const char* in, const char** out, const char *end) {
  270. Assimp::SkipLine(in, out, end);
  271. ++iLineNumber;
  272. return true;
  273. }
  274. // -------------------------------------------------------------------
  275. inline bool SkipSpacesAndLineEnd(const char *in, const char **out, const char *end) {
  276. ++iLineNumber;
  277. return Assimp::SkipSpacesAndLineEnd(in, out, end);
  278. }
  279. private:
  280. /** Configuration option: frame to be loaded */
  281. unsigned int configFrameID;
  282. /** Buffer to hold the loaded file */
  283. std::vector<char> mBuffer;
  284. char *mEnd;
  285. /** Output scene to be filled
  286. */
  287. aiScene* pScene;
  288. /** Size of the input file in bytes
  289. */
  290. unsigned int iFileSize;
  291. /** Array of textures found in the file
  292. */
  293. std::vector<std::string> aszTextures;
  294. /** Array of triangles found in the file
  295. */
  296. std::vector<SMD::Face> asTriangles;
  297. /** Array of bones found in the file
  298. */
  299. std::vector<SMD::Bone> asBones;
  300. /** Smallest frame index found in the skeleton
  301. */
  302. int iSmallestFrame;
  303. /** Length of the whole animation, in frames
  304. */
  305. double dLengthOfAnim;
  306. /** Do we have texture coordinates?
  307. */
  308. bool bHasUVs;
  309. /** Current line number
  310. */
  311. unsigned int iLineNumber;
  312. bool bLoadAnimationList = true;
  313. bool noSkeletonMesh = false;
  314. };
  315. } // end of namespace Assimp
  316. #endif // AI_SMDIMPORTER_H_INC