SAnimatedMesh.h 3.9 KB

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