SAnimatedMesh.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __S_ANIMATED_MESH_H_INCLUDED__
  5. #define __S_ANIMATED_MESH_H_INCLUDED__
  6. #include "IAnimatedMesh.h"
  7. #include "IMesh.h"
  8. #include "aabbox3d.h"
  9. #include "irrArray.h"
  10. namespace irr
  11. {
  12. namespace scene
  13. {
  14. //! Simple implementation of the IAnimatedMesh interface.
  15. struct SAnimatedMesh : public IAnimatedMesh
  16. {
  17. //! constructor
  18. SAnimatedMesh(scene::IMesh* mesh=0, scene::E_ANIMATED_MESH_TYPE type=scene::EAMT_UNKNOWN) : IAnimatedMesh(), FramesPerSecond(25.f), Type(type)
  19. {
  20. #ifdef _DEBUG
  21. setDebugName("SAnimatedMesh");
  22. #endif
  23. addMesh(mesh);
  24. recalculateBoundingBox();
  25. }
  26. //! destructor
  27. virtual ~SAnimatedMesh()
  28. {
  29. // drop meshes
  30. for (u32 i=0; i<Meshes.size(); ++i)
  31. Meshes[i]->drop();
  32. }
  33. //! Gets the frame count of the animated mesh.
  34. /** \return Amount of frames. If the amount is 1, it is a static, non animated mesh. */
  35. virtual u32 getFrameCount() const _IRR_OVERRIDE_
  36. {
  37. return Meshes.size();
  38. }
  39. //! Gets the default animation speed of the animated mesh.
  40. /** \return Amount of frames per second. If the amount is 0, it is a static, non animated mesh. */
  41. virtual f32 getAnimationSpeed() const _IRR_OVERRIDE_
  42. {
  43. return FramesPerSecond;
  44. }
  45. //! Gets the frame count of the animated mesh.
  46. /** \param fps Frames per second to play the animation with. If the amount is 0, it is not animated.
  47. The actual speed is set in the scene node the mesh is instantiated in.*/
  48. virtual void setAnimationSpeed(f32 fps) _IRR_OVERRIDE_
  49. {
  50. FramesPerSecond=fps;
  51. }
  52. //! Returns the IMesh interface for a frame.
  53. /** \param frame: Frame number as zero based index. The maximum frame number is
  54. getFrameCount() - 1;
  55. \param detailLevel: Level of detail. 0 is the lowest,
  56. 255 the highest level of detail. Most meshes will ignore the detail level.
  57. \param startFrameLoop: start frame
  58. \param endFrameLoop: end frame
  59. \return The animated mesh based on a detail level. */
  60. virtual IMesh* getMesh(s32 frame, s32 detailLevel=255, s32 startFrameLoop=-1, s32 endFrameLoop=-1) _IRR_OVERRIDE_
  61. {
  62. if (Meshes.empty())
  63. return 0;
  64. return Meshes[frame];
  65. }
  66. //! adds a Mesh
  67. void addMesh(IMesh* mesh)
  68. {
  69. if (mesh)
  70. {
  71. mesh->grab();
  72. Meshes.push_back(mesh);
  73. }
  74. }
  75. //! Returns an axis aligned bounding box of the mesh.
  76. /** \return A bounding box of this mesh is returned. */
  77. virtual const core::aabbox3d<f32>& getBoundingBox() const _IRR_OVERRIDE_
  78. {
  79. return Box;
  80. }
  81. //! set user axis aligned bounding box
  82. virtual void setBoundingBox(const core::aabbox3df& box) _IRR_OVERRIDE_
  83. {
  84. Box = box;
  85. }
  86. //! Recalculates the bounding box.
  87. void recalculateBoundingBox()
  88. {
  89. Box.reset(0,0,0);
  90. if (Meshes.empty())
  91. return;
  92. Box = Meshes[0]->getBoundingBox();
  93. for (u32 i=1; i<Meshes.size(); ++i)
  94. Box.addInternalBox(Meshes[i]->getBoundingBox());
  95. }
  96. //! Returns the type of the animated mesh.
  97. virtual E_ANIMATED_MESH_TYPE getMeshType() const _IRR_OVERRIDE_
  98. {
  99. return Type;
  100. }
  101. //! returns amount of mesh buffers.
  102. virtual u32 getMeshBufferCount() const _IRR_OVERRIDE_
  103. {
  104. if (Meshes.empty())
  105. return 0;
  106. return Meshes[0]->getMeshBufferCount();
  107. }
  108. //! returns pointer to a mesh buffer
  109. virtual IMeshBuffer* getMeshBuffer(u32 nr) const _IRR_OVERRIDE_
  110. {
  111. if (Meshes.empty())
  112. return 0;
  113. return Meshes[0]->getMeshBuffer(nr);
  114. }
  115. //! Returns pointer to a mesh buffer which fits a material
  116. /** \param material: material to search for
  117. \return Returns the pointer to the mesh buffer or
  118. NULL if there is no such mesh buffer. */
  119. virtual IMeshBuffer* getMeshBuffer( const video::SMaterial &material) const _IRR_OVERRIDE_
  120. {
  121. if (Meshes.empty())
  122. return 0;
  123. return Meshes[0]->getMeshBuffer(material);
  124. }
  125. //! Set a material flag for all meshbuffers of this mesh.
  126. virtual void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue) _IRR_OVERRIDE_
  127. {
  128. for (u32 i=0; i<Meshes.size(); ++i)
  129. Meshes[i]->setMaterialFlag(flag, newvalue);
  130. }
  131. //! set the hardware mapping hint, for driver
  132. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) _IRR_OVERRIDE_
  133. {
  134. for (u32 i=0; i<Meshes.size(); ++i)
  135. Meshes[i]->setHardwareMappingHint(newMappingHint, buffer);
  136. }
  137. //! flags the meshbuffer as changed, reloads hardware buffers
  138. virtual void setDirty(E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX) _IRR_OVERRIDE_
  139. {
  140. for (u32 i=0; i<Meshes.size(); ++i)
  141. Meshes[i]->setDirty(buffer);
  142. }
  143. //! All meshes defining the animated mesh
  144. core::array<IMesh*> Meshes;
  145. //! The bounding box of this mesh
  146. core::aabbox3d<f32> Box;
  147. //! Default animation speed of this mesh.
  148. f32 FramesPerSecond;
  149. //! The type of the mesh.
  150. E_ANIMATED_MESH_TYPE Type;
  151. };
  152. } // end namespace scene
  153. } // end namespace irr
  154. #endif