SMDLoader.h 13 KB

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