SMesh.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "IMesh.h"
  6. #include "IMeshBuffer.h"
  7. #include "aabbox3d.h"
  8. #include "irrArray.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. //! Simple implementation of the IMesh interface.
  14. struct SMesh : public IMesh
  15. {
  16. //! constructor
  17. SMesh()
  18. {
  19. #ifdef _DEBUG
  20. setDebugName("SMesh");
  21. #endif
  22. }
  23. //! destructor
  24. virtual ~SMesh()
  25. {
  26. // drop buffers
  27. for (u32 i = 0; i < MeshBuffers.size(); ++i)
  28. MeshBuffers[i]->drop();
  29. }
  30. //! clean mesh
  31. virtual void clear()
  32. {
  33. for (u32 i = 0; i < MeshBuffers.size(); ++i)
  34. MeshBuffers[i]->drop();
  35. MeshBuffers.clear();
  36. BoundingBox.reset(0.f, 0.f, 0.f);
  37. }
  38. //! returns amount of mesh buffers.
  39. u32 getMeshBufferCount() const override
  40. {
  41. return MeshBuffers.size();
  42. }
  43. //! returns pointer to a mesh buffer
  44. IMeshBuffer *getMeshBuffer(u32 nr) const override
  45. {
  46. return MeshBuffers[nr];
  47. }
  48. //! returns a meshbuffer which fits a material
  49. /** reverse search */
  50. IMeshBuffer *getMeshBuffer(const video::SMaterial &material) const override
  51. {
  52. for (s32 i = (s32)MeshBuffers.size() - 1; i >= 0; --i) {
  53. if (material == MeshBuffers[i]->getMaterial())
  54. return MeshBuffers[i];
  55. }
  56. return 0;
  57. }
  58. //! returns an axis aligned bounding box
  59. const core::aabbox3d<f32> &getBoundingBox() const override
  60. {
  61. return BoundingBox;
  62. }
  63. //! set user axis aligned bounding box
  64. void setBoundingBox(const core::aabbox3df &box) override
  65. {
  66. BoundingBox = box;
  67. }
  68. //! recalculates the bounding box
  69. void recalculateBoundingBox()
  70. {
  71. bool hasMeshBufferBBox = false;
  72. for (u32 i = 0; i < MeshBuffers.size(); ++i) {
  73. const core::aabbox3df &bb = MeshBuffers[i]->getBoundingBox();
  74. if (!bb.isEmpty()) {
  75. if (!hasMeshBufferBBox) {
  76. hasMeshBufferBBox = true;
  77. BoundingBox = bb;
  78. } else {
  79. BoundingBox.addInternalBox(bb);
  80. }
  81. }
  82. }
  83. if (!hasMeshBufferBBox)
  84. BoundingBox.reset(0.0f, 0.0f, 0.0f);
  85. }
  86. //! adds a MeshBuffer
  87. /** The bounding box is not updated automatically. */
  88. void addMeshBuffer(IMeshBuffer *buf)
  89. {
  90. if (buf) {
  91. buf->grab();
  92. MeshBuffers.push_back(buf);
  93. }
  94. }
  95. //! set the hardware mapping hint, for driver
  96. void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  97. {
  98. for (u32 i = 0; i < MeshBuffers.size(); ++i)
  99. MeshBuffers[i]->setHardwareMappingHint(newMappingHint, buffer);
  100. }
  101. //! flags the meshbuffer as changed, reloads hardware buffers
  102. void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  103. {
  104. for (u32 i = 0; i < MeshBuffers.size(); ++i)
  105. MeshBuffers[i]->setDirty(buffer);
  106. }
  107. //! The meshbuffers of this mesh
  108. core::array<IMeshBuffer *> MeshBuffers;
  109. //! The bounding box of this mesh
  110. core::aabbox3d<f32> BoundingBox;
  111. };
  112. } // end namespace scene
  113. } // end namespace irr