IAnimatedMeshMD3.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright (C) 2007-2012 Nikolaus Gebhardt / Thomas Alten
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __I_ANIMATED_MESH_MD3_H_INCLUDED__
  5. #define __I_ANIMATED_MESH_MD3_H_INCLUDED__
  6. #include "IAnimatedMesh.h"
  7. #include "IQ3Shader.h"
  8. #include "quaternion.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. enum eMD3Models
  14. {
  15. EMD3_HEAD = 0,
  16. EMD3_UPPER,
  17. EMD3_LOWER,
  18. EMD3_WEAPON,
  19. EMD3_NUMMODELS
  20. };
  21. //! Animation list
  22. enum EMD3_ANIMATION_TYPE
  23. {
  24. // Animations for both lower and upper parts of the player
  25. EMD3_BOTH_DEATH_1 = 0,
  26. EMD3_BOTH_DEAD_1,
  27. EMD3_BOTH_DEATH_2,
  28. EMD3_BOTH_DEAD_2,
  29. EMD3_BOTH_DEATH_3,
  30. EMD3_BOTH_DEAD_3,
  31. // Animations for the upper part
  32. EMD3_TORSO_GESTURE,
  33. EMD3_TORSO_ATTACK_1,
  34. EMD3_TORSO_ATTACK_2,
  35. EMD3_TORSO_DROP,
  36. EMD3_TORSO_RAISE,
  37. EMD3_TORSO_STAND_1,
  38. EMD3_TORSO_STAND_2,
  39. // Animations for the lower part
  40. EMD3_LEGS_WALK_CROUCH,
  41. EMD3_LEGS_WALK,
  42. EMD3_LEGS_RUN,
  43. EMD3_LEGS_BACK,
  44. EMD3_LEGS_SWIM,
  45. EMD3_LEGS_JUMP_1,
  46. EMD3_LEGS_LAND_1,
  47. EMD3_LEGS_JUMP_2,
  48. EMD3_LEGS_LAND_2,
  49. EMD3_LEGS_IDLE,
  50. EMD3_LEGS_IDLE_CROUCH,
  51. EMD3_LEGS_TURN,
  52. //! Not an animation, but amount of animation types.
  53. EMD3_ANIMATION_COUNT
  54. };
  55. struct SMD3AnimationInfo
  56. {
  57. //! First frame
  58. s32 first;
  59. //! Last frame
  60. s32 num;
  61. //! Looping frames
  62. s32 looping;
  63. //! Frames per second
  64. s32 fps;
  65. };
  66. // byte-align structures
  67. #include "irrpack.h"
  68. //! this holds the header info of the MD3 file
  69. struct SMD3Header
  70. {
  71. c8 headerID[4]; //id of file, always "IDP3"
  72. s32 Version; //this is a version number, always 15
  73. s8 fileName[68];//sometimes left Blank... 65 chars, 32bit aligned == 68 chars
  74. s32 numFrames; //number of KeyFrames
  75. s32 numTags; //number of 'tags' per frame
  76. s32 numMeshes; //number of meshes/skins
  77. s32 numMaxSkins;//maximum number of unique skins used in md3 file. artefact md2
  78. s32 frameStart; //starting position of frame-structur
  79. s32 tagStart; //starting position of tag-structures
  80. s32 tagEnd; //ending position of tag-structures/starting position of mesh-structures
  81. s32 fileSize;
  82. } PACK_STRUCT;
  83. //! this holds the header info of an MD3 mesh section
  84. struct SMD3MeshHeader
  85. {
  86. c8 meshID[4]; //id, must be IDP3
  87. c8 meshName[68]; //name of mesh 65 chars, 32 bit aligned == 68 chars
  88. s32 numFrames; //number of meshframes in mesh
  89. s32 numShader; //number of skins in mesh
  90. s32 numVertices; //number of vertices
  91. s32 numTriangles; //number of Triangles
  92. s32 offset_triangles; //starting position of Triangle data, relative to start of Mesh_Header
  93. s32 offset_shaders; //size of header
  94. s32 offset_st; //starting position of texvector data, relative to start of Mesh_Header
  95. s32 vertexStart; //starting position of vertex data,relative to start of Mesh_Header
  96. s32 offset_end;
  97. } PACK_STRUCT;
  98. //! Compressed Vertex Data
  99. struct SMD3Vertex
  100. {
  101. s16 position[3];
  102. u8 normal[2];
  103. } PACK_STRUCT;
  104. //! Texture Coordinate
  105. struct SMD3TexCoord
  106. {
  107. f32 u;
  108. f32 v;
  109. } PACK_STRUCT;
  110. //! Triangle Index
  111. struct SMD3Face
  112. {
  113. s32 Index[3];
  114. } PACK_STRUCT;
  115. // Default alignment
  116. #include "irrunpack.h"
  117. //! Holding Frame Data for a Mesh
  118. struct SMD3MeshBuffer : public IReferenceCounted
  119. {
  120. SMD3MeshHeader MeshHeader;
  121. core::stringc Shader;
  122. core::array < s32 > Indices;
  123. core::array < SMD3Vertex > Vertices;
  124. core::array < SMD3TexCoord > Tex;
  125. };
  126. //! hold a tag info for connecting meshes
  127. /** Basically its an alternate way to describe a transformation. */
  128. struct SMD3QuaternionTag
  129. {
  130. virtual ~SMD3QuaternionTag()
  131. {
  132. position.X = 0.f;
  133. }
  134. // construct copy constructor
  135. SMD3QuaternionTag( const SMD3QuaternionTag & copyMe )
  136. {
  137. *this = copyMe;
  138. }
  139. // construct for searching
  140. SMD3QuaternionTag( const core::stringc& name )
  141. : Name ( name ) {}
  142. // construct from a position and euler angles in degrees
  143. SMD3QuaternionTag ( const core::vector3df &pos, const core::vector3df &angle )
  144. : position(pos), rotation(angle * core::DEGTORAD) {}
  145. // set to matrix
  146. void setto ( core::matrix4 &m )
  147. {
  148. rotation.getMatrix ( m, position );
  149. }
  150. bool operator == ( const SMD3QuaternionTag &other ) const
  151. {
  152. return Name == other.Name;
  153. }
  154. SMD3QuaternionTag & operator=( const SMD3QuaternionTag & copyMe )
  155. {
  156. Name = copyMe.Name;
  157. position = copyMe.position;
  158. rotation = copyMe.rotation;
  159. return *this;
  160. }
  161. core::stringc Name;
  162. core::vector3df position;
  163. core::quaternion rotation;
  164. };
  165. //! holds a associative list of named quaternions
  166. struct SMD3QuaternionTagList
  167. {
  168. SMD3QuaternionTagList()
  169. {
  170. Container.setAllocStrategy(core::ALLOC_STRATEGY_SAFE);
  171. }
  172. // construct copy constructor
  173. SMD3QuaternionTagList(const SMD3QuaternionTagList& copyMe)
  174. {
  175. *this = copyMe;
  176. }
  177. virtual ~SMD3QuaternionTagList() {}
  178. SMD3QuaternionTag* get(const core::stringc& name)
  179. {
  180. SMD3QuaternionTag search ( name );
  181. s32 index = Container.linear_search ( search );
  182. if ( index >= 0 )
  183. return &Container[index];
  184. return 0;
  185. }
  186. u32 size () const
  187. {
  188. return Container.size();
  189. }
  190. void set_used(u32 new_size)
  191. {
  192. s32 diff = (s32) new_size - (s32) Container.allocated_size();
  193. if ( diff > 0 )
  194. {
  195. SMD3QuaternionTag e("");
  196. for ( s32 i = 0; i < diff; ++i )
  197. Container.push_back(e);
  198. }
  199. }
  200. const SMD3QuaternionTag& operator[](u32 index) const
  201. {
  202. return Container[index];
  203. }
  204. SMD3QuaternionTag& operator[](u32 index)
  205. {
  206. return Container[index];
  207. }
  208. void push_back(const SMD3QuaternionTag& other)
  209. {
  210. Container.push_back(other);
  211. }
  212. SMD3QuaternionTagList& operator = (const SMD3QuaternionTagList & copyMe)
  213. {
  214. Container = copyMe.Container;
  215. return *this;
  216. }
  217. private:
  218. core::array < SMD3QuaternionTag > Container;
  219. };
  220. //! Holding Frames Buffers and Tag Infos
  221. struct SMD3Mesh: public IReferenceCounted
  222. {
  223. SMD3Mesh ()
  224. {
  225. MD3Header.numFrames = 0;
  226. }
  227. virtual ~SMD3Mesh()
  228. {
  229. for (u32 i=0; i<Buffer.size(); ++i)
  230. Buffer[i]->drop();
  231. }
  232. core::stringc Name;
  233. core::array<SMD3MeshBuffer*> Buffer;
  234. SMD3QuaternionTagList TagList;
  235. SMD3Header MD3Header;
  236. };
  237. //! Interface for using some special functions of MD3 meshes
  238. class IAnimatedMeshMD3 : public IAnimatedMesh
  239. {
  240. public:
  241. //! tune how many frames you want to render in between.
  242. virtual void setInterpolationShift(u32 shift, u32 loopMode) =0;
  243. //! get the tag list of the mesh.
  244. virtual SMD3QuaternionTagList* getTagList(s32 frame, s32 detailLevel, s32 startFrameLoop, s32 endFrameLoop) =0;
  245. //! get the original md3 mesh.
  246. virtual SMD3Mesh* getOriginalMesh() =0;
  247. };
  248. } // end namespace scene
  249. } // end namespace irr
  250. #endif