Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

461 linhas
12 KiB

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