SAnimatedMesh.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #pragma once
  5. #include <vector>
  6. #include "IAnimatedMesh.h"
  7. #include "IMesh.h"
  8. #include "aabbox3d.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. //! Simple implementation of the IAnimatedMesh interface.
  14. struct SAnimatedMesh final : public IAnimatedMesh
  15. {
  16. //! constructor
  17. SAnimatedMesh(scene::IMesh *mesh = 0, scene::E_ANIMATED_MESH_TYPE type = scene::EAMT_UNKNOWN) :
  18. 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 (auto *mesh : Meshes)
  31. mesh->drop();
  32. }
  33. f32 getMaxFrameNumber() const override
  34. {
  35. return static_cast<f32>(Meshes.size() - 1);
  36. }
  37. //! Gets the default animation speed of the animated mesh.
  38. /** \return Amount of frames per second. If the amount is 0, it is a static, non animated mesh. */
  39. f32 getAnimationSpeed() const override
  40. {
  41. return FramesPerSecond;
  42. }
  43. //! Gets the frame count of the animated mesh.
  44. /** \param fps Frames per second to play the animation with. If the amount is 0, it is not animated.
  45. The actual speed is set in the scene node the mesh is instantiated in.*/
  46. void setAnimationSpeed(f32 fps) override
  47. {
  48. FramesPerSecond = fps;
  49. }
  50. //! Returns the IMesh interface for a frame.
  51. /** \param frame: Frame number as zero based index.
  52. \return The animated mesh based for the given frame */
  53. IMesh *getMesh(f32 frame) override
  54. {
  55. if (Meshes.empty())
  56. return nullptr;
  57. return Meshes[static_cast<s32>(frame)];
  58. }
  59. //! adds a Mesh
  60. void addMesh(IMesh *mesh)
  61. {
  62. if (mesh) {
  63. mesh->grab();
  64. Meshes.push_back(mesh);
  65. }
  66. }
  67. //! Returns an axis aligned bounding box of the mesh.
  68. /** \return A bounding box of this mesh is returned. */
  69. const core::aabbox3d<f32> &getBoundingBox() const override
  70. {
  71. return Box;
  72. }
  73. //! set user axis aligned bounding box
  74. void setBoundingBox(const core::aabbox3df &box) override
  75. {
  76. Box = box;
  77. }
  78. //! Recalculates the bounding box.
  79. void recalculateBoundingBox()
  80. {
  81. Box.reset(0, 0, 0);
  82. if (Meshes.empty())
  83. return;
  84. Box = Meshes[0]->getBoundingBox();
  85. for (u32 i = 1; i < Meshes.size(); ++i)
  86. Box.addInternalBox(Meshes[i]->getBoundingBox());
  87. }
  88. //! Returns the type of the animated mesh.
  89. E_ANIMATED_MESH_TYPE getMeshType() const override
  90. {
  91. return Type;
  92. }
  93. //! returns amount of mesh buffers.
  94. u32 getMeshBufferCount() const override
  95. {
  96. if (Meshes.empty())
  97. return 0;
  98. return Meshes[0]->getMeshBufferCount();
  99. }
  100. //! returns pointer to a mesh buffer
  101. IMeshBuffer *getMeshBuffer(u32 nr) const override
  102. {
  103. if (Meshes.empty())
  104. return 0;
  105. return Meshes[0]->getMeshBuffer(nr);
  106. }
  107. //! Returns pointer to a mesh buffer which fits a material
  108. /** \param material: material to search for
  109. \return Returns the pointer to the mesh buffer or
  110. NULL if there is no such mesh buffer. */
  111. IMeshBuffer *getMeshBuffer(const video::SMaterial &material) const override
  112. {
  113. if (Meshes.empty())
  114. return 0;
  115. return Meshes[0]->getMeshBuffer(material);
  116. }
  117. //! set the hardware mapping hint, for driver
  118. void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  119. {
  120. for (u32 i = 0; i < Meshes.size(); ++i)
  121. Meshes[i]->setHardwareMappingHint(newMappingHint, buffer);
  122. }
  123. //! flags the meshbuffer as changed, reloads hardware buffers
  124. void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  125. {
  126. for (u32 i = 0; i < Meshes.size(); ++i)
  127. Meshes[i]->setDirty(buffer);
  128. }
  129. //! All meshes defining the animated mesh
  130. std::vector<IMesh *> Meshes;
  131. //! The bounding box of this mesh
  132. core::aabbox3d<f32> Box;
  133. //! Default animation speed of this mesh.
  134. f32 FramesPerSecond;
  135. //! The type of the mesh.
  136. E_ANIMATED_MESH_TYPE Type;
  137. };
  138. } // end namespace scene
  139. } // end namespace irr