SMDLoader.h 12 KB

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