SMDLoader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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/aiTypes.h"
  43. #include "../include/aiTexture.h"
  44. #include "../include/aiAnim.h"
  45. #include "../include/aiMaterial.h"
  46. struct aiNode;
  47. // STL headers
  48. #include <vector>
  49. namespace Assimp {
  50. class MaterialHelper;
  51. namespace SMD {
  52. // ---------------------------------------------------------------------------
  53. /** Data structure for a vertex in a SMD file
  54. */
  55. struct Vertex
  56. {
  57. Vertex() : iParentNode(0xffffffff)
  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. {
  74. Face() : iTexture(0x0)
  75. {}
  76. //! Texture index for the face
  77. unsigned int iTexture;
  78. //! The three vertices of the face
  79. Vertex avVertices[3];
  80. };
  81. // ---------------------------------------------------------------------------
  82. /** Data structure for a bone in a SMD file
  83. */
  84. struct Bone
  85. {
  86. //! Default constructor
  87. Bone() : iParent(0xffffffff), bIsUsed(false)
  88. {
  89. }
  90. //! Destructor
  91. ~Bone()
  92. {
  93. }
  94. //! Name of the bone
  95. std::string mName;
  96. //! Parent of the bone
  97. uint32_t iParent;
  98. //! Animation of the bone
  99. struct Animation
  100. {
  101. //! Public default constructor
  102. Animation()
  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 SMDImporter : public BaseImporter
  136. {
  137. friend class Importer;
  138. protected:
  139. /** Constructor to be privately used by Importer */
  140. SMDImporter();
  141. /** Destructor, private as well */
  142. ~SMDImporter();
  143. public:
  144. // -------------------------------------------------------------------
  145. /** Returns whether the class can handle the format of the given file.
  146. * See BaseImporter::CanRead() for details.
  147. */
  148. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  149. bool checkSig) const;
  150. // -------------------------------------------------------------------
  151. /** Called prior to ReadFile().
  152. * The function is a request to the importer to update its configuration
  153. * basing on the Importer's configuration property list.
  154. */
  155. void SetupProperties(const Importer* pImp);
  156. protected:
  157. // -------------------------------------------------------------------
  158. /** Called by Importer::GetExtensionList() for each loaded importer.
  159. * See BaseImporter::GetExtensionList() for details
  160. */
  161. void GetExtensionList(std::string& append);
  162. // -------------------------------------------------------------------
  163. /** Imports the given file into the given scene structure.
  164. * See BaseImporter::InternReadFile() for details
  165. */
  166. void InternReadFile( const std::string& pFile, aiScene* pScene,
  167. IOSystem* pIOHandler);
  168. protected:
  169. // -------------------------------------------------------------------
  170. /** Parse the SMD file and create the output scene
  171. */
  172. void ParseFile();
  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. /** Computes absolute bone transformations
  236. * All output transformations are in worldspace.
  237. */
  238. void ComputeAbsoluteBoneTransformations();
  239. // -------------------------------------------------------------------
  240. /** Parse a line in the skeleton section
  241. */
  242. void ParseSkeletonElement(const char* szCurrent,
  243. const char** szCurrentOut,int iTime);
  244. // -------------------------------------------------------------------
  245. /** Parse a line in the nodes section
  246. */
  247. void ParseNodeInfo(const char* szCurrent,
  248. const char** szCurrentOut);
  249. // -------------------------------------------------------------------
  250. /** Parse a floating-point value
  251. */
  252. bool ParseFloat(const char* szCurrent,
  253. const char** szCurrentOut, float& out);
  254. // -------------------------------------------------------------------
  255. /** Parse an unsigned integer. There may be no sign!
  256. */
  257. bool ParseUnsignedInt(const char* szCurrent,
  258. const char** szCurrentOut, unsigned int& out);
  259. // -------------------------------------------------------------------
  260. /** Parse a signed integer. Signs (+,-) are handled.
  261. */
  262. bool ParseSignedInt(const char* szCurrent,
  263. const char** szCurrentOut, int& out);
  264. // -------------------------------------------------------------------
  265. /** Fix invalid time values in the file
  266. */
  267. void FixTimeValues();
  268. // -------------------------------------------------------------------
  269. /** Add all children of a bone as subnodes to a node
  270. * \param pcNode Parent node
  271. * \param iParent Parent bone index
  272. */
  273. void AddBoneChildren(aiNode* pcNode, uint32_t iParent);
  274. // -------------------------------------------------------------------
  275. /** Build output meshes/materials/nodes/animations
  276. */
  277. void CreateOutputMeshes();
  278. void CreateOutputNodes();
  279. void CreateOutputAnimations();
  280. void CreateOutputMaterials();
  281. // -------------------------------------------------------------------
  282. /** Print a log message together with the current line number
  283. */
  284. void LogErrorNoThrow(const char* msg);
  285. void LogWarning(const char* msg);
  286. // -------------------------------------------------------------------
  287. inline bool SkipLine( const char* in, const char** out)
  288. {
  289. Assimp::SkipLine(in,out);
  290. ++iLineNumber;
  291. return true;
  292. }
  293. // -------------------------------------------------------------------
  294. inline bool SkipSpacesAndLineEnd( const char* in, const char** out)
  295. {
  296. ++iLineNumber;
  297. return Assimp::SkipSpacesAndLineEnd(in,out);
  298. }
  299. private:
  300. /** Configuration option: frame to be loaded */
  301. unsigned int configFrameID;
  302. /** Buffer to hold the loaded file */
  303. const char* mBuffer;
  304. /** Output scene to be filled
  305. */
  306. aiScene* pScene;
  307. /** Size of the input file in bytes
  308. */
  309. unsigned int iFileSize;
  310. /** Array of textures found in the file
  311. */
  312. std::vector<std::string> aszTextures;
  313. /** Array of triangles found in the file
  314. */
  315. std::vector<SMD::Face> asTriangles;
  316. /** Array of bones found in the file
  317. */
  318. std::vector<SMD::Bone> asBones;
  319. /** Smallest frame index found in the skeleton
  320. */
  321. int iSmallestFrame;
  322. /** Length of the whole animation, in frames
  323. */
  324. double dLengthOfAnim;
  325. /** Do we have texture coordinates?
  326. */
  327. bool bHasUVs;
  328. /** Current line numer
  329. */
  330. unsigned int iLineNumber;
  331. };
  332. } // end of namespace Assimp
  333. #endif // AI_SMDIMPORTER_H_INC