MD5Parser.h 13 KB

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