SSharedMeshBuffer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_SHARED_MESH_BUFFER_H_INCLUDED__
  5. #define __S_SHARED_MESH_BUFFER_H_INCLUDED__
  6. #include "irrArray.h"
  7. #include "IMeshBuffer.h"
  8. namespace irr
  9. {
  10. namespace scene
  11. {
  12. //! Implementation of the IMeshBuffer interface with shared vertex list
  13. struct SSharedMeshBuffer : public IMeshBuffer
  14. {
  15. //! constructor
  16. SSharedMeshBuffer()
  17. : IMeshBuffer()
  18. , Vertices(0), ChangedID_Vertex(1), ChangedID_Index(1)
  19. , MappingHintVertex(EHM_NEVER), MappingHintIndex(EHM_NEVER)
  20. , PrimitiveType(EPT_TRIANGLES)
  21. {
  22. #ifdef _DEBUG
  23. setDebugName("SSharedMeshBuffer");
  24. #endif
  25. }
  26. //! constructor
  27. SSharedMeshBuffer(core::array<video::S3DVertex> *vertices) : IMeshBuffer(), Vertices(vertices), ChangedID_Vertex(1), ChangedID_Index(1), MappingHintVertex(EHM_NEVER), MappingHintIndex(EHM_NEVER)
  28. {
  29. #ifdef _DEBUG
  30. setDebugName("SSharedMeshBuffer");
  31. #endif
  32. }
  33. //! returns the material of this meshbuffer
  34. virtual const video::SMaterial& getMaterial() const _IRR_OVERRIDE_
  35. {
  36. return Material;
  37. }
  38. //! returns the material of this meshbuffer
  39. virtual video::SMaterial& getMaterial() _IRR_OVERRIDE_
  40. {
  41. return Material;
  42. }
  43. //! returns pointer to vertices
  44. virtual const void* getVertices() const _IRR_OVERRIDE_
  45. {
  46. if (Vertices)
  47. return Vertices->const_pointer();
  48. else
  49. return 0;
  50. }
  51. //! returns pointer to vertices
  52. virtual void* getVertices() _IRR_OVERRIDE_
  53. {
  54. if (Vertices)
  55. return Vertices->pointer();
  56. else
  57. return 0;
  58. }
  59. //! returns amount of vertices
  60. virtual u32 getVertexCount() const _IRR_OVERRIDE_
  61. {
  62. if (Vertices)
  63. return Vertices->size();
  64. else
  65. return 0;
  66. }
  67. //! returns pointer to indices
  68. virtual const u16* getIndices() const _IRR_OVERRIDE_
  69. {
  70. return Indices.const_pointer();
  71. }
  72. //! returns pointer to indices
  73. virtual u16* getIndices() _IRR_OVERRIDE_
  74. {
  75. return Indices.pointer();
  76. }
  77. //! returns amount of indices
  78. virtual u32 getIndexCount() const _IRR_OVERRIDE_
  79. {
  80. return Indices.size();
  81. }
  82. //! Get type of index data which is stored in this meshbuffer.
  83. virtual video::E_INDEX_TYPE getIndexType() const _IRR_OVERRIDE_
  84. {
  85. return video::EIT_16BIT;
  86. }
  87. //! returns an axis aligned bounding box
  88. virtual const core::aabbox3d<f32>& getBoundingBox() const _IRR_OVERRIDE_
  89. {
  90. return BoundingBox;
  91. }
  92. //! set user axis aligned bounding box
  93. virtual void setBoundingBox( const core::aabbox3df& box) _IRR_OVERRIDE_
  94. {
  95. BoundingBox = box;
  96. }
  97. //! returns which type of vertex data is stored.
  98. virtual video::E_VERTEX_TYPE getVertexType() const _IRR_OVERRIDE_
  99. {
  100. return video::EVT_STANDARD;
  101. }
  102. //! recalculates the bounding box. should be called if the mesh changed.
  103. virtual void recalculateBoundingBox() _IRR_OVERRIDE_
  104. {
  105. if (!Vertices || Vertices->empty() || Indices.empty())
  106. BoundingBox.reset(0,0,0);
  107. else
  108. {
  109. BoundingBox.reset((*Vertices)[Indices[0]].Pos);
  110. for (u32 i=1; i<Indices.size(); ++i)
  111. BoundingBox.addInternalPoint((*Vertices)[Indices[i]].Pos);
  112. }
  113. }
  114. //! returns position of vertex i
  115. virtual const core::vector3df& getPosition(u32 i) const _IRR_OVERRIDE_
  116. {
  117. _IRR_DEBUG_BREAK_IF(!Vertices);
  118. return (*Vertices)[Indices[i]].Pos;
  119. }
  120. //! returns position of vertex i
  121. virtual core::vector3df& getPosition(u32 i) _IRR_OVERRIDE_
  122. {
  123. _IRR_DEBUG_BREAK_IF(!Vertices);
  124. return (*Vertices)[Indices[i]].Pos;
  125. }
  126. //! returns normal of vertex i
  127. virtual const core::vector3df& getNormal(u32 i) const _IRR_OVERRIDE_
  128. {
  129. _IRR_DEBUG_BREAK_IF(!Vertices);
  130. return (*Vertices)[Indices[i]].Normal;
  131. }
  132. //! returns normal of vertex i
  133. virtual core::vector3df& getNormal(u32 i) _IRR_OVERRIDE_
  134. {
  135. _IRR_DEBUG_BREAK_IF(!Vertices);
  136. return (*Vertices)[Indices[i]].Normal;
  137. }
  138. //! returns texture coord of vertex i
  139. virtual const core::vector2df& getTCoords(u32 i) const _IRR_OVERRIDE_
  140. {
  141. _IRR_DEBUG_BREAK_IF(!Vertices);
  142. return (*Vertices)[Indices[i]].TCoords;
  143. }
  144. //! returns texture coord of vertex i
  145. virtual core::vector2df& getTCoords(u32 i) _IRR_OVERRIDE_
  146. {
  147. _IRR_DEBUG_BREAK_IF(!Vertices);
  148. return (*Vertices)[Indices[i]].TCoords;
  149. }
  150. //! append the vertices and indices to the current buffer
  151. virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) _IRR_OVERRIDE_ {}
  152. //! append the meshbuffer to the current buffer
  153. virtual void append(const IMeshBuffer* const other) _IRR_OVERRIDE_ {}
  154. //! get the current hardware mapping hint
  155. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const _IRR_OVERRIDE_
  156. {
  157. return MappingHintVertex;
  158. }
  159. //! get the current hardware mapping hint
  160. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const _IRR_OVERRIDE_
  161. {
  162. return MappingHintIndex;
  163. }
  164. //! set the hardware mapping hint, for driver
  165. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) _IRR_OVERRIDE_
  166. {
  167. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_VERTEX)
  168. MappingHintVertex=NewMappingHint;
  169. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_INDEX)
  170. MappingHintIndex=NewMappingHint;
  171. }
  172. //! Describe what kind of primitive geometry is used by the meshbuffer
  173. virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) _IRR_OVERRIDE_
  174. {
  175. PrimitiveType = type;
  176. }
  177. //! Get the kind of primitive geometry which is used by the meshbuffer
  178. virtual E_PRIMITIVE_TYPE getPrimitiveType() const _IRR_OVERRIDE_
  179. {
  180. return PrimitiveType;
  181. }
  182. //! flags the mesh as changed, reloads hardware buffers
  183. virtual void setDirty(E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX) _IRR_OVERRIDE_
  184. {
  185. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_VERTEX)
  186. ++ChangedID_Vertex;
  187. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_INDEX)
  188. ++ChangedID_Index;
  189. }
  190. //! Get the currently used ID for identification of changes.
  191. /** This shouldn't be used for anything outside the VideoDriver. */
  192. virtual u32 getChangedID_Vertex() const _IRR_OVERRIDE_ {return ChangedID_Vertex;}
  193. //! Get the currently used ID for identification of changes.
  194. /** This shouldn't be used for anything outside the VideoDriver. */
  195. virtual u32 getChangedID_Index() const _IRR_OVERRIDE_ {return ChangedID_Index;}
  196. //! Material of this meshBuffer
  197. video::SMaterial Material;
  198. //! Shared Array of vertices
  199. core::array<video::S3DVertex> *Vertices;
  200. //! Array of indices
  201. core::array<u16> Indices;
  202. //! ID used for hardware buffer management
  203. u32 ChangedID_Vertex;
  204. //! ID used for hardware buffer management
  205. u32 ChangedID_Index;
  206. //! Bounding box
  207. core::aabbox3df BoundingBox;
  208. //! hardware mapping hint
  209. E_HARDWARE_MAPPING MappingHintVertex;
  210. E_HARDWARE_MAPPING MappingHintIndex;
  211. //! Primitive type used for rendering (triangles, lines, ...)
  212. E_PRIMITIVE_TYPE PrimitiveType;
  213. };
  214. } // end namespace scene
  215. } // end namespace irr
  216. #endif