MD5Parser.h 13 KB

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