SMDLoader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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 "BaseImporter.h"
  40. #include "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. {
  56. Vertex() : iParentNode(UINT_MAX)
  57. {}
  58. //! Vertex position, normal and texture coordinate
  59. aiVector3D pos,nor,uv;
  60. //! Vertex parent node
  61. unsigned int iParentNode;
  62. //! Links to bones: pair.first is the bone index,
  63. //! pair.second is the vertex weight.
  64. //! WARN: The remaining weight (to reach 1.0f) is assigned
  65. //! to the parent node/bone
  66. std::vector<std::pair<unsigned int, float> > aiBoneLinks;
  67. };
  68. // ---------------------------------------------------------------------------
  69. /** Data structure for a face in a SMD file
  70. */
  71. struct Face
  72. {
  73. Face() : iTexture(0x0)
  74. {}
  75. //! Texture index for the face
  76. unsigned int iTexture;
  77. //! The three vertices of the face
  78. Vertex avVertices[3];
  79. };
  80. // ---------------------------------------------------------------------------
  81. /** Data structure for a bone in a SMD file
  82. */
  83. struct Bone
  84. {
  85. //! Default constructor
  86. Bone() : iParent(UINT_MAX), bIsUsed(false)
  87. {
  88. }
  89. //! Destructor
  90. ~Bone()
  91. {
  92. }
  93. //! Name of the bone
  94. std::string mName;
  95. //! Parent of the bone
  96. uint32_t iParent;
  97. //! Animation of the bone
  98. struct Animation
  99. {
  100. //! Public default constructor
  101. Animation()
  102. : iFirstTimeKey()
  103. {
  104. asKeys.reserve(20);
  105. }
  106. //! Data structure for a matrix key
  107. struct MatrixKey
  108. {
  109. //! Matrix at this time
  110. aiMatrix4x4 matrix;
  111. //! Absolute transformation matrix
  112. aiMatrix4x4 matrixAbsolute;
  113. //! Position
  114. aiVector3D vPos;
  115. //! Rotation (euler angles)
  116. aiVector3D vRot;
  117. //! Current time. may be negative, this
  118. //! will be fixed later
  119. double dTime;
  120. };
  121. //! Index of the key with the smallest time value
  122. uint32_t iFirstTimeKey;
  123. //! Array of matrix keys
  124. std::vector<MatrixKey> asKeys;
  125. } sAnim;
  126. //! Offset matrix of the bone
  127. aiMatrix4x4 mOffsetMatrix;
  128. //! true if the bone is referenced by at least one mesh
  129. bool bIsUsed;
  130. };
  131. } //! namespace SMD
  132. // ---------------------------------------------------------------------------
  133. /** Used to load Half-life 1 and 2 SMD models
  134. */
  135. class ASSIMP_API SMDImporter : public BaseImporter
  136. {
  137. public:
  138. SMDImporter();
  139. ~SMDImporter();
  140. public:
  141. // -------------------------------------------------------------------
  142. /** Returns whether the class can handle the format of the given file.
  143. * See BaseImporter::CanRead() for details.
  144. */
  145. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  146. bool checkSig) const;
  147. // -------------------------------------------------------------------
  148. /** Called prior to ReadFile().
  149. * The function is a request to the importer to update its configuration
  150. * basing on the Importer's configuration property list.
  151. */
  152. void SetupProperties(const Importer* pImp);
  153. protected:
  154. // -------------------------------------------------------------------
  155. /** Return importer meta information.
  156. * See #BaseImporter::GetInfo for the details
  157. */
  158. const aiImporterDesc* GetInfo () const;
  159. // -------------------------------------------------------------------
  160. /** Imports the given file into the given scene structure.
  161. * See BaseImporter::InternReadFile() for details
  162. */
  163. void InternReadFile( const std::string& pFile, aiScene* pScene,
  164. IOSystem* pIOHandler);
  165. protected:
  166. // -------------------------------------------------------------------
  167. /** Parse the SMD file and create the output scene
  168. */
  169. void ParseFile();
  170. // -------------------------------------------------------------------
  171. /** Parse the triangles section of the SMD file
  172. * \param szCurrent Current position in the file. Points to the first
  173. * data line of the section.
  174. * \param szCurrentOut Receives a pointer to the heading line of
  175. * the next section (or to EOF)
  176. */
  177. void ParseTrianglesSection(const char* szCurrent,
  178. const char** szCurrentOut);
  179. // -------------------------------------------------------------------
  180. /** Parse the vertex animation section in VTA files
  181. * \param szCurrent Current position in the file. Points to the first
  182. * data line of the section.
  183. * \param szCurrentOut Receives a pointer to the heading line of
  184. * the next section (or to EOF)
  185. */
  186. void ParseVASection(const char* szCurrent,
  187. const char** szCurrentOut);
  188. // -------------------------------------------------------------------
  189. /** Parse the nodes section of the SMD file
  190. * \param szCurrent Current position in the file. Points to the first
  191. * data line of the section.
  192. * \param szCurrentOut Receives a pointer to the heading line of
  193. * the next section (or to EOF)
  194. */
  195. void ParseNodesSection(const char* szCurrent,
  196. const char** szCurrentOut);
  197. // -------------------------------------------------------------------
  198. /** Parse the skeleton section of the SMD file
  199. * \param szCurrent Current position in the file. Points to the first
  200. * data line of the section.
  201. * \param szCurrentOut Receives a pointer to the heading line of
  202. * the next section (or to EOF)
  203. */
  204. void ParseSkeletonSection(const char* szCurrent,
  205. const char** szCurrentOut);
  206. // -------------------------------------------------------------------
  207. /** Parse a single triangle in the SMD file
  208. * \param szCurrent Current position in the file. Points to the first
  209. * data line of the section.
  210. * \param szCurrentOut Receives the output cursor position
  211. */
  212. void ParseTriangle(const char* szCurrent,
  213. const char** szCurrentOut);
  214. // -------------------------------------------------------------------
  215. /** Parse a single vertex in the SMD file
  216. * \param szCurrent Current position in the file. Points to the first
  217. * data line of the section.
  218. * \param szCurrentOut Receives the output cursor position
  219. * \param vertex Vertex to be filled
  220. */
  221. void ParseVertex(const char* szCurrent,
  222. const char** szCurrentOut, SMD::Vertex& vertex,
  223. bool bVASection = false);
  224. // -------------------------------------------------------------------
  225. /** Get the index of a texture. If the texture was not yet known
  226. * it will be added to the internal texture list.
  227. * \param filename Name of the texture
  228. * \return Value texture index
  229. */
  230. unsigned int GetTextureIndex(const std::string& filename);
  231. // -------------------------------------------------------------------
  232. /** Computes absolute bone transformations
  233. * All output transformations are in worldspace.
  234. */
  235. void ComputeAbsoluteBoneTransformations();
  236. // -------------------------------------------------------------------
  237. /** Parse a line in the skeleton section
  238. */
  239. void ParseSkeletonElement(const char* szCurrent,
  240. const char** szCurrentOut,int iTime);
  241. // -------------------------------------------------------------------
  242. /** Parse a line in the nodes section
  243. */
  244. void ParseNodeInfo(const char* szCurrent,
  245. const char** szCurrentOut);
  246. // -------------------------------------------------------------------
  247. /** Parse a floating-point value
  248. */
  249. bool ParseFloat(const char* szCurrent,
  250. const char** szCurrentOut, float& out);
  251. // -------------------------------------------------------------------
  252. /** Parse an unsigned integer. There may be no sign!
  253. */
  254. bool ParseUnsignedInt(const char* szCurrent,
  255. const char** szCurrentOut, unsigned int& out);
  256. // -------------------------------------------------------------------
  257. /** Parse a signed integer. Signs (+,-) are handled.
  258. */
  259. bool ParseSignedInt(const char* szCurrent,
  260. const char** szCurrentOut, int& out);
  261. // -------------------------------------------------------------------
  262. /** Fix invalid time values in the file
  263. */
  264. void FixTimeValues();
  265. // -------------------------------------------------------------------
  266. /** Add all children of a bone as subnodes to a node
  267. * \param pcNode Parent node
  268. * \param iParent Parent bone index
  269. */
  270. void AddBoneChildren(aiNode* pcNode, uint32_t iParent);
  271. // -------------------------------------------------------------------
  272. /** Build output meshes/materials/nodes/animations
  273. */
  274. void CreateOutputMeshes();
  275. void CreateOutputNodes();
  276. void CreateOutputAnimations();
  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. };
  329. } // end of namespace Assimp
  330. #endif // AI_SMDIMPORTER_H_INC