IAnimatedMeshMD3.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 IRR_I_ANIMATED_MESH_MD3_H_INCLUDED
  5. #define IRR_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-structure
  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. // construct for searching
  131. SMD3QuaternionTag( const core::stringc& name )
  132. : Name ( name ) {}
  133. // construct from a position and euler angles in degrees
  134. SMD3QuaternionTag ( const core::vector3df &pos, const core::vector3df &angle )
  135. : position(pos), rotation(angle * core::DEGTORAD) {}
  136. // set to matrix
  137. void setto ( core::matrix4 &m )
  138. {
  139. rotation.getMatrix ( m, position );
  140. }
  141. bool operator == ( const SMD3QuaternionTag &other ) const
  142. {
  143. return Name == other.Name;
  144. }
  145. core::stringc Name;
  146. core::vector3df position;
  147. core::quaternion rotation;
  148. };
  149. //! holds a associative list of named quaternions
  150. struct SMD3QuaternionTagList
  151. {
  152. SMD3QuaternionTagList()
  153. {
  154. Container.setAllocStrategy(core::ALLOC_STRATEGY_SAFE);
  155. }
  156. SMD3QuaternionTag* get(const core::stringc& name)
  157. {
  158. SMD3QuaternionTag search ( name );
  159. const s32 index = Container.linear_search ( search );
  160. if ( index >= 0 )
  161. return &Container[index];
  162. return 0;
  163. }
  164. u32 size () const
  165. {
  166. return Container.size();
  167. }
  168. void set_used(u32 new_size)
  169. {
  170. const s32 diff = (s32) new_size - (s32) Container.allocated_size();
  171. if ( diff > 0 )
  172. {
  173. SMD3QuaternionTag e("");
  174. for ( s32 i = 0; i < diff; ++i )
  175. Container.push_back(e);
  176. }
  177. }
  178. const SMD3QuaternionTag& operator[](u32 index) const
  179. {
  180. return Container[index];
  181. }
  182. SMD3QuaternionTag& operator[](u32 index)
  183. {
  184. return Container[index];
  185. }
  186. void push_back(const SMD3QuaternionTag& other)
  187. {
  188. Container.push_back(other);
  189. }
  190. private:
  191. core::array < SMD3QuaternionTag > Container;
  192. };
  193. //! Holding Frames Buffers and Tag Infos
  194. struct SMD3Mesh: public IReferenceCounted
  195. {
  196. SMD3Mesh ()
  197. {
  198. MD3Header.numFrames = 0;
  199. }
  200. virtual ~SMD3Mesh()
  201. {
  202. for (u32 i=0; i<Buffer.size(); ++i)
  203. Buffer[i]->drop();
  204. }
  205. core::stringc Name;
  206. core::array<SMD3MeshBuffer*> Buffer;
  207. SMD3QuaternionTagList TagList;
  208. SMD3Header MD3Header;
  209. };
  210. //! Interface for using some special functions of MD3 meshes
  211. class IAnimatedMeshMD3 : public IAnimatedMesh
  212. {
  213. public:
  214. //! tune how many frames you want to render in between.
  215. virtual void setInterpolationShift(u32 shift, u32 loopMode) =0;
  216. //! get the tag list of the mesh.
  217. virtual SMD3QuaternionTagList* getTagList(s32 frame, s32 detailLevel, s32 startFrameLoop, s32 endFrameLoop) =0;
  218. //! get the original md3 mesh.
  219. virtual SMD3Mesh* getOriginalMesh() =0;
  220. };
  221. } // end namespace scene
  222. } // end namespace irr
  223. #endif