MD5Parser.h 13 KB

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