SMesh.h 3.5 KB

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