MD5Parser.h 13 KB

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