SAnimatedMesh.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "IAnimatedMesh.h"
  6. #include "IMesh.h"
  7. #include "aabbox3d.h"
  8. #include "irrArray.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. //! Simple implementation of the IAnimatedMesh interface.
  14. struct SAnimatedMesh : 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 (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. u32 getFrameCount() const 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. f32 getAnimationSpeed() const 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. void setAnimationSpeed(f32 fps) 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. IMesh *getMesh(s32 frame, s32 detailLevel = 255, s32 startFrameLoop = -1, s32 endFrameLoop = -1) 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. mesh->grab();
  71. Meshes.push_back(mesh);
  72. }
  73. }
  74. //! Returns an axis aligned bounding box of the mesh.
  75. /** \return A bounding box of this mesh is returned. */
  76. const core::aabbox3d<f32> &getBoundingBox() const override
  77. {
  78. return Box;
  79. }
  80. //! set user axis aligned bounding box
  81. void setBoundingBox(const core::aabbox3df &box) override
  82. {
  83. Box = box;
  84. }
  85. //! Recalculates the bounding box.
  86. void recalculateBoundingBox()
  87. {
  88. Box.reset(0, 0, 0);
  89. if (Meshes.empty())
  90. return;
  91. Box = Meshes[0]->getBoundingBox();
  92. for (u32 i = 1; i < Meshes.size(); ++i)
  93. Box.addInternalBox(Meshes[i]->getBoundingBox());
  94. }
  95. //! Returns the type of the animated mesh.
  96. E_ANIMATED_MESH_TYPE getMeshType() const override
  97. {
  98. return Type;
  99. }
  100. //! returns amount of mesh buffers.
  101. u32 getMeshBufferCount() const override
  102. {
  103. if (Meshes.empty())
  104. return 0;
  105. return Meshes[0]->getMeshBufferCount();
  106. }
  107. //! returns pointer to a mesh buffer
  108. IMeshBuffer *getMeshBuffer(u32 nr) const override
  109. {
  110. if (Meshes.empty())
  111. return 0;
  112. return Meshes[0]->getMeshBuffer(nr);
  113. }
  114. //! Returns pointer to a mesh buffer which fits a material
  115. /** \param material: material to search for
  116. \return Returns the pointer to the mesh buffer or
  117. NULL if there is no such mesh buffer. */
  118. IMeshBuffer *getMeshBuffer(const video::SMaterial &material) const override
  119. {
  120. if (Meshes.empty())
  121. return 0;
  122. return Meshes[0]->getMeshBuffer(material);
  123. }
  124. //! set the hardware mapping hint, for driver
  125. void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  126. {
  127. for (u32 i = 0; i < Meshes.size(); ++i)
  128. Meshes[i]->setHardwareMappingHint(newMappingHint, buffer);
  129. }
  130. //! flags the meshbuffer as changed, reloads hardware buffers
  131. void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  132. {
  133. for (u32 i = 0; i < Meshes.size(); ++i)
  134. Meshes[i]->setDirty(buffer);
  135. }
  136. //! All meshes defining the animated mesh
  137. core::array<IMesh *> Meshes;
  138. //! The bounding box of this mesh
  139. core::aabbox3d<f32> Box;
  140. //! Default animation speed of this mesh.
  141. f32 FramesPerSecond;
  142. //! The type of the mesh.
  143. E_ANIMATED_MESH_TYPE Type;
  144. };
  145. } // end namespace scene
  146. } // end namespace irr