MD5Parser.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2015, 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 MD5Parser.h
  34. * @brief Definition of the .MD5 parser class.
  35. * http://www.modwiki.net/wiki/MD5_(file_format)
  36. */
  37. #ifndef AI_MD5PARSER_H_INCLUDED
  38. #define AI_MD5PARSER_H_INCLUDED
  39. #include "../include/assimp/types.h"
  40. #include "ParsingUtils.h"
  41. #include <vector>
  42. // Urho3D: VS2008 compatibility
  43. #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
  44. #include <stdint.h>
  45. #else
  46. #include "../include/assimp/Compiler/pstdint.h"
  47. #endif
  48. struct aiFace;
  49. namespace Assimp {
  50. namespace MD5 {
  51. // ---------------------------------------------------------------------------
  52. /** Represents a single element in a MD5 file
  53. *
  54. * Elements are always contained in sections.
  55. */
  56. struct Element
  57. {
  58. //! Points to the starting point of the element
  59. //! Whitespace at the beginning and at the end have been removed,
  60. //! Elements are terminated with \0
  61. char* szStart;
  62. //! Original line number (can be used in error messages
  63. //! if a parsing error occurs)
  64. unsigned int iLineNumber;
  65. };
  66. typedef std::vector< Element > ElementList;
  67. // ---------------------------------------------------------------------------
  68. /** Represents a section of a MD5 file (such as the mesh or the joints section)
  69. *
  70. * A section is always enclosed in { and } brackets.
  71. */
  72. struct Section
  73. {
  74. //! Original line number (can be used in error messages
  75. //! if a parsing error occurs)
  76. unsigned int iLineNumber;
  77. //! List of all elements which have been parsed in this section.
  78. ElementList mElements;
  79. //! Name of the section
  80. std::string mName;
  81. //! For global elements: the value of the element as string
  82. //! Iif !length() the section is not a global element
  83. std::string mGlobalValue;
  84. };
  85. typedef std::vector< Section> SectionList;
  86. // ---------------------------------------------------------------------------
  87. /** Basic information about a joint
  88. */
  89. struct BaseJointDescription
  90. {
  91. //! Name of the bone
  92. aiString mName;
  93. //! Parent index of the bone
  94. int mParentIndex;
  95. };
  96. // ---------------------------------------------------------------------------
  97. /** Represents a bone (joint) descriptor in a MD5Mesh file
  98. */
  99. struct BoneDesc : BaseJointDescription
  100. {
  101. //! Absolute position of the bone
  102. aiVector3D mPositionXYZ;
  103. //! Absolute rotation of the bone
  104. aiVector3D mRotationQuat;
  105. aiQuaternion mRotationQuatConverted;
  106. //! Absolute transformation of the bone
  107. //! (temporary)
  108. aiMatrix4x4 mTransform;
  109. //! Inverse transformation of the bone
  110. //! (temporary)
  111. aiMatrix4x4 mInvTransform;
  112. //! Internal
  113. unsigned int mMap;
  114. };
  115. typedef std::vector< BoneDesc > BoneList;
  116. // ---------------------------------------------------------------------------
  117. /** Represents a bone (joint) descriptor in a MD5Anim file
  118. */
  119. struct AnimBoneDesc : BaseJointDescription
  120. {
  121. //! Flags (AI_MD5_ANIMATION_FLAG_xxx)
  122. unsigned int iFlags;
  123. //! Index of the first key that corresponds to this anim bone
  124. unsigned int iFirstKeyIndex;
  125. };
  126. typedef std::vector< AnimBoneDesc > AnimBoneList;
  127. // ---------------------------------------------------------------------------
  128. /** Represents a base frame descriptor in a MD5Anim file
  129. */
  130. struct BaseFrameDesc
  131. {
  132. aiVector3D vPositionXYZ;
  133. aiVector3D vRotationQuat;
  134. };
  135. typedef std::vector< BaseFrameDesc > BaseFrameList;
  136. // ---------------------------------------------------------------------------
  137. /** Represents a camera animation frame in a MDCamera file
  138. */
  139. struct CameraAnimFrameDesc : BaseFrameDesc
  140. {
  141. float fFOV;
  142. };
  143. typedef std::vector< CameraAnimFrameDesc > CameraFrameList;
  144. // ---------------------------------------------------------------------------
  145. /** Represents a frame descriptor in a MD5Anim file
  146. */
  147. struct FrameDesc
  148. {
  149. //! Index of the frame
  150. unsigned int iIndex;
  151. //! Animation keyframes - a large blob of data at first
  152. std::vector< float > mValues;
  153. };
  154. typedef std::vector< FrameDesc > FrameList;
  155. // ---------------------------------------------------------------------------
  156. /** Represents a vertex descriptor in a MD5 file
  157. */
  158. struct VertexDesc
  159. {
  160. VertexDesc()
  161. : mFirstWeight (0)
  162. , mNumWeights (0)
  163. {}
  164. //! UV cordinate of the vertex
  165. aiVector2D mUV;
  166. //! Index of the first weight of the vertex in
  167. //! the vertex weight list
  168. unsigned int mFirstWeight;
  169. //! Number of weights assigned to this vertex
  170. unsigned int mNumWeights;
  171. };
  172. typedef std::vector< VertexDesc > VertexList;
  173. // ---------------------------------------------------------------------------
  174. /** Represents a vertex weight descriptor in a MD5 file
  175. */
  176. struct WeightDesc
  177. {
  178. //! Index of the bone to which this weight refers
  179. unsigned int mBone;
  180. //! The weight value
  181. float mWeight;
  182. //! The offset position of this weight
  183. // ! (in the coordinate system defined by the parent bone)
  184. aiVector3D vOffsetPosition;
  185. };
  186. typedef std::vector< WeightDesc > WeightList;
  187. typedef std::vector< aiFace > FaceList;
  188. // ---------------------------------------------------------------------------
  189. /** Represents a mesh in a MD5 file
  190. */
  191. struct MeshDesc
  192. {
  193. //! Weights of the mesh
  194. WeightList mWeights;
  195. //! Vertices of the mesh
  196. VertexList mVertices;
  197. //! Faces of the mesh
  198. FaceList mFaces;
  199. //! Name of the shader (=texture) to be assigned to the mesh
  200. aiString mShader;
  201. };
  202. typedef std::vector< MeshDesc > MeshList;
  203. // ---------------------------------------------------------------------------
  204. // Convert a quaternion to its usual representation
  205. inline void ConvertQuaternion (const aiVector3D& in, aiQuaternion& out) {
  206. out.x = in.x;
  207. out.y = in.y;
  208. out.z = in.z;
  209. const float t = 1.0f - (in.x*in.x) - (in.y*in.y) - (in.z*in.z);
  210. if (t < 0.0f)
  211. out.w = 0.0f;
  212. else out.w = std::sqrt (t);
  213. // Assimp convention.
  214. out.w *= -1.f;
  215. }
  216. // ---------------------------------------------------------------------------
  217. /** Parses the data sections of a MD5 mesh file
  218. */
  219. class MD5MeshParser
  220. {
  221. public:
  222. // -------------------------------------------------------------------
  223. /** Constructs a new MD5MeshParser instance from an existing
  224. * preparsed list of file sections.
  225. *
  226. * @param mSections List of file sections (output of MD5Parser)
  227. */
  228. explicit MD5MeshParser(SectionList& mSections);
  229. //! List of all meshes
  230. MeshList mMeshes;
  231. //! List of all joints
  232. BoneList mJoints;
  233. };
  234. // remove this flag if you need to the bounding box data
  235. #define AI_MD5_PARSE_NO_BOUNDS
  236. // ---------------------------------------------------------------------------
  237. /** Parses the data sections of a MD5 animation file
  238. */
  239. class MD5AnimParser
  240. {
  241. public:
  242. // -------------------------------------------------------------------
  243. /** Constructs a new MD5AnimParser instance from an existing
  244. * preparsed list of file sections.
  245. *
  246. * @param mSections List of file sections (output of MD5Parser)
  247. */
  248. explicit MD5AnimParser(SectionList& mSections);
  249. //! Output frame rate
  250. float fFrameRate;
  251. //! List of animation bones
  252. AnimBoneList mAnimatedBones;
  253. //! List of base frames
  254. BaseFrameList mBaseFrames;
  255. //! List of animation frames
  256. FrameList mFrames;
  257. //! Number of animated components
  258. unsigned int mNumAnimatedComponents;
  259. };
  260. // ---------------------------------------------------------------------------
  261. /** Parses the data sections of a MD5 camera animation file
  262. */
  263. class MD5CameraParser
  264. {
  265. public:
  266. // -------------------------------------------------------------------
  267. /** Constructs a new MD5CameraParser instance from an existing
  268. * preparsed list of file sections.
  269. *
  270. * @param mSections List of file sections (output of MD5Parser)
  271. */
  272. explicit MD5CameraParser(SectionList& mSections);
  273. //! Output frame rate
  274. float fFrameRate;
  275. //! List of cuts
  276. std::vector<unsigned int> cuts;
  277. //! Frames
  278. CameraFrameList frames;
  279. };
  280. // ---------------------------------------------------------------------------
  281. /** Parses the block structure of MD5MESH and MD5ANIM files (but does no
  282. * further processing)
  283. */
  284. class MD5Parser
  285. {
  286. public:
  287. // -------------------------------------------------------------------
  288. /** Constructs a new MD5Parser instance from an existing buffer.
  289. *
  290. * @param buffer File buffer
  291. * @param fileSize Length of the file in bytes (excluding a terminal 0)
  292. */
  293. MD5Parser(char* buffer, unsigned int fileSize);
  294. // -------------------------------------------------------------------
  295. /** Report a specific error message and throw an exception
  296. * @param error Error message to be reported
  297. * @param line Index of the line where the error occured
  298. */
  299. AI_WONT_RETURN static void ReportError (const char* error, unsigned int line) AI_WONT_RETURN_SUFFIX;
  300. // -------------------------------------------------------------------
  301. /** Report a specific warning
  302. * @param warn Warn message to be reported
  303. * @param line Index of the line where the error occured
  304. */
  305. static void ReportWarning (const char* warn, unsigned int line);
  306. void ReportError (const char* error) {
  307. return ReportError(error, lineNumber);
  308. }
  309. void ReportWarning (const char* warn) {
  310. return ReportWarning(warn, lineNumber);
  311. }
  312. public:
  313. //! List of all sections which have been read
  314. SectionList mSections;
  315. private:
  316. // -------------------------------------------------------------------
  317. /** Parses a file section. The current file pointer must be outside
  318. * of a section.
  319. * @param out Receives the section data
  320. * @return true if the end of the file has been reached
  321. * @throws ImportErrorException if an error occurs
  322. */
  323. bool ParseSection(Section& out);
  324. // -------------------------------------------------------------------
  325. /** Parses the file header
  326. * @throws ImportErrorException if an error occurs
  327. */
  328. void ParseHeader();
  329. // override these functions to make sure the line counter gets incremented
  330. // -------------------------------------------------------------------
  331. bool SkipLine( const char* in, const char** out)
  332. {
  333. ++lineNumber;
  334. return Assimp::SkipLine(in,out);
  335. }
  336. // -------------------------------------------------------------------
  337. bool SkipLine( )
  338. {
  339. return SkipLine(buffer,(const char**)&buffer);
  340. }
  341. // -------------------------------------------------------------------
  342. bool SkipSpacesAndLineEnd( const char* in, const char** out)
  343. {
  344. bool bHad = false;
  345. bool running = true;
  346. while (running) {
  347. if( *in == '\r' || *in == '\n') {
  348. // we open files in binary mode, so there could be \r\n sequences ...
  349. if (!bHad) {
  350. bHad = true;
  351. ++lineNumber;
  352. }
  353. }
  354. else if (*in == '\t' || *in == ' ')bHad = false;
  355. else break;
  356. in++;
  357. }
  358. *out = in;
  359. return *in != '\0';
  360. }
  361. // -------------------------------------------------------------------
  362. bool SkipSpacesAndLineEnd( )
  363. {
  364. return SkipSpacesAndLineEnd(buffer,(const char**)&buffer);
  365. }
  366. // -------------------------------------------------------------------
  367. bool SkipSpaces( )
  368. {
  369. return Assimp::SkipSpaces((const char**)&buffer);
  370. }
  371. char* buffer;
  372. unsigned int fileSize;
  373. unsigned int lineNumber;
  374. };
  375. }}
  376. #endif // AI_MD5PARSER_H_INCLUDED