SMDLoader.h 12 KB

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