SMesh.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. //! destructor
  19. virtual ~SMesh()
  20. {
  21. // drop buffers
  22. for (auto *buf : MeshBuffers)
  23. buf->drop();
  24. }
  25. //! clean mesh
  26. virtual void clear()
  27. {
  28. for (auto *buf : MeshBuffers)
  29. buf->drop();
  30. MeshBuffers.clear();
  31. BoundingBox.reset(0.f, 0.f, 0.f);
  32. }
  33. //! returns amount of mesh buffers.
  34. u32 getMeshBufferCount() const override
  35. {
  36. return static_cast<u32>(MeshBuffers.size());
  37. }
  38. //! returns pointer to a mesh buffer
  39. IMeshBuffer *getMeshBuffer(u32 nr) const override
  40. {
  41. return MeshBuffers[nr];
  42. }
  43. //! returns a meshbuffer which fits a material
  44. /** reverse search */
  45. IMeshBuffer *getMeshBuffer(const video::SMaterial &material) const override
  46. {
  47. for (auto it = MeshBuffers.rbegin(); it != MeshBuffers.rend(); it++) {
  48. if (material == (*it)->getMaterial())
  49. return *it;
  50. }
  51. return nullptr;
  52. }
  53. u32 getTextureSlot(u32 meshbufNr) const override
  54. {
  55. return TextureSlots.at(meshbufNr);
  56. }
  57. void setTextureSlot(u32 meshbufNr, u32 textureSlot)
  58. {
  59. TextureSlots.at(meshbufNr) = textureSlot;
  60. }
  61. //! returns an axis aligned bounding box
  62. const core::aabbox3d<f32> &getBoundingBox() const override
  63. {
  64. return BoundingBox;
  65. }
  66. //! set user axis aligned bounding box
  67. void setBoundingBox(const core::aabbox3df &box) override
  68. {
  69. BoundingBox = box;
  70. }
  71. //! recalculates the bounding box
  72. void recalculateBoundingBox()
  73. {
  74. bool hasMeshBufferBBox = false;
  75. for (auto *buf : MeshBuffers) {
  76. const core::aabbox3df &bb = buf->getBoundingBox();
  77. if (!bb.isEmpty()) {
  78. if (!hasMeshBufferBBox) {
  79. hasMeshBufferBBox = true;
  80. BoundingBox = bb;
  81. } else {
  82. BoundingBox.addInternalBox(bb);
  83. }
  84. }
  85. }
  86. if (!hasMeshBufferBBox)
  87. BoundingBox.reset(0.0f, 0.0f, 0.0f);
  88. }
  89. //! adds a MeshBuffer
  90. /** The bounding box is not updated automatically. */
  91. void addMeshBuffer(IMeshBuffer *buf)
  92. {
  93. if (buf) {
  94. buf->grab();
  95. MeshBuffers.push_back(buf);
  96. TextureSlots.push_back(getMeshBufferCount() - 1);
  97. }
  98. }
  99. //! set the hardware mapping hint, for driver
  100. void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  101. {
  102. for (auto *buf : MeshBuffers)
  103. buf->setHardwareMappingHint(newMappingHint, buffer);
  104. }
  105. //! flags the meshbuffer as changed, reloads hardware buffers
  106. void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override
  107. {
  108. for (auto *buf : MeshBuffers)
  109. buf->setDirty(buffer);
  110. }
  111. //! The meshbuffers of this mesh
  112. std::vector<IMeshBuffer *> MeshBuffers;
  113. //! Mapping from meshbuffer number to bindable texture slot
  114. std::vector<u32> TextureSlots;
  115. //! The bounding box of this mesh
  116. core::aabbox3d<f32> BoundingBox{{0, 0, 0}};
  117. };
  118. } // end namespace scene
  119. } // end namespace irr