SMesh.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "IMesh.h"
  7. #include "IMeshBuffer.h"
  8. #include "aabbox3d.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. //! Simple implementation of the IMesh interface.
  14. struct SMesh final : 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 (auto *buf : MeshBuffers)
  28. buf->drop();
  29. }
  30. //! clean mesh
  31. virtual void clear()
  32. {
  33. for (auto *buf : MeshBuffers)
  34. buf->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 static_cast<u32>(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 (auto it = MeshBuffers.rbegin(); it != MeshBuffers.rend(); it++) {
  53. if (material == (*it)->getMaterial())
  54. return *it;
  55. }
  56. return nullptr;
  57. }
  58. u32 getTextureSlot(u32 meshbufNr) const override
  59. {
  60. return TextureSlots.at(meshbufNr);
  61. }
  62. void setTextureSlot(u32 meshbufNr, u32 textureSlot)
  63. {
  64. TextureSlots.at(meshbufNr) = textureSlot;
  65. }
  66. //! returns an axis aligned bounding box
  67. const core::aabbox3d<f32> &getBoundingBox() const override
  68. {
  69. return BoundingBox;
  70. }
  71. //! set user axis aligned bounding box
  72. void setBoundingBox(const core::aabbox3df &box) override
  73. {
  74. BoundingBox = box;
  75. }
  76. //! recalculates the bounding box
  77. void recalculateBoundingBox()
  78. {
  79. bool hasMeshBufferBBox = false;
  80. for (auto *buf : MeshBuffers) {
  81. const core::aabbox3df &bb = buf->getBoundingBox();
  82. if (!bb.isEmpty()) {
  83. if (!hasMeshBufferBBox) {
  84. hasMeshBufferBBox = true;
  85. BoundingBox = bb;
  86. } else {
  87. BoundingBox.addInternalBox(bb);
  88. }
  89. }
  90. }
  91. if (!hasMeshBufferBBox)
  92. BoundingBox.reset(0.0f, 0.0f, 0.0f);
  93. }
  94. //! adds a MeshBuffer
  95. /** The bounding box is not updated automatically. */
  96. void addMeshBuffer(IMeshBuffer *buf)
  97. {
  98. if (buf) {
  99. buf->grab();
  100. MeshBuffers.push_back(buf);
  101. TextureSlots.push_back(getMeshBufferCount() - 1);
  102. }
  103. }
  104. //! set the hardware mapping hint, for driver
  105. void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  106. {
  107. for (auto *buf : MeshBuffers)
  108. buf->setHardwareMappingHint(newMappingHint, buffer);
  109. }
  110. //! flags the meshbuffer as changed, reloads hardware buffers
  111. void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  112. {
  113. for (auto *buf : MeshBuffers)
  114. buf->setDirty(buffer);
  115. }
  116. //! The meshbuffers of this mesh
  117. std::vector<IMeshBuffer *> MeshBuffers;
  118. //! Mapping from meshbuffer number to bindable texture slot
  119. std::vector<u32> TextureSlots;
  120. //! The bounding box of this mesh
  121. core::aabbox3d<f32> BoundingBox;
  122. };
  123. } // end namespace scene
  124. } // end namespace irr