SSharedMeshBuffer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. //! returns color of vertex i
  151. virtual video::SColor& getColor(u32 i) IRR_OVERRIDE
  152. {
  153. IRR_DEBUG_BREAK_IF(!Vertices);
  154. return (*Vertices)[Indices[i]].Color;
  155. }
  156. //! returns color of vertex i
  157. virtual const video::SColor& getColor(u32 i) const IRR_OVERRIDE
  158. {
  159. IRR_DEBUG_BREAK_IF(!Vertices);
  160. return (*Vertices)[Indices[i]].Color;
  161. }
  162. //! append the vertices and indices to the current buffer
  163. virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices, bool updateBoundingBox) IRR_OVERRIDE
  164. {
  165. // can't do that as it doesn't own the vertex memory
  166. }
  167. //! append the meshbuffer to the current buffer
  168. virtual void append(const IMeshBuffer* const other, bool updateBoundingBox) IRR_OVERRIDE
  169. {
  170. // can't do that as it doesn't own the vertex memory
  171. }
  172. //! get the current hardware mapping hint
  173. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const IRR_OVERRIDE
  174. {
  175. return MappingHintVertex;
  176. }
  177. //! get the current hardware mapping hint
  178. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const IRR_OVERRIDE
  179. {
  180. return MappingHintIndex;
  181. }
  182. //! set the hardware mapping hint, for driver
  183. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) IRR_OVERRIDE
  184. {
  185. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_VERTEX)
  186. MappingHintVertex=NewMappingHint;
  187. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_INDEX)
  188. MappingHintIndex=NewMappingHint;
  189. }
  190. //! Describe what kind of primitive geometry is used by the meshbuffer
  191. virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) IRR_OVERRIDE
  192. {
  193. PrimitiveType = type;
  194. }
  195. //! Get the kind of primitive geometry which is used by the meshbuffer
  196. virtual E_PRIMITIVE_TYPE getPrimitiveType() const IRR_OVERRIDE
  197. {
  198. return PrimitiveType;
  199. }
  200. //! flags the mesh as changed, reloads hardware buffers
  201. virtual void setDirty(E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX) IRR_OVERRIDE
  202. {
  203. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_VERTEX)
  204. ++ChangedID_Vertex;
  205. if (buffer==EBT_VERTEX_AND_INDEX || buffer==EBT_INDEX)
  206. ++ChangedID_Index;
  207. }
  208. //! Get the currently used ID for identification of changes.
  209. /** This shouldn't be used for anything outside the VideoDriver. */
  210. virtual u32 getChangedID_Vertex() const IRR_OVERRIDE {return ChangedID_Vertex;}
  211. //! Get the currently used ID for identification of changes.
  212. /** This shouldn't be used for anything outside the VideoDriver. */
  213. virtual u32 getChangedID_Index() const IRR_OVERRIDE {return ChangedID_Index;}
  214. //! Returns type of the class implementing the IMeshBuffer
  215. virtual EMESH_BUFFER_TYPE getType() const IRR_OVERRIDE
  216. {
  217. return EMBT_SHARED;
  218. }
  219. //! Create copy of the meshbuffer
  220. virtual IMeshBuffer* createClone(int cloneFlags) const IRR_OVERRIDE
  221. {
  222. SSharedMeshBuffer * clone = new SSharedMeshBuffer();
  223. if (cloneFlags & ECF_VERTICES)
  224. {
  225. clone->Vertices = Vertices;
  226. clone->BoundingBox = BoundingBox;
  227. }
  228. if (cloneFlags & ECF_INDICES)
  229. {
  230. clone->Indices = Indices;
  231. }
  232. clone->Material = Material;
  233. clone->MappingHintVertex = MappingHintVertex;
  234. clone->MappingHintIndex = MappingHintIndex;
  235. clone->PrimitiveType = PrimitiveType;
  236. return clone;
  237. }
  238. //! Material of this meshBuffer
  239. video::SMaterial Material;
  240. //! Shared Array of vertices
  241. core::array<video::S3DVertex> *Vertices;
  242. //! Array of indices
  243. core::array<u16> Indices;
  244. //! ID used for hardware buffer management
  245. u32 ChangedID_Vertex;
  246. //! ID used for hardware buffer management
  247. u32 ChangedID_Index;
  248. //! Bounding box
  249. core::aabbox3df BoundingBox;
  250. //! hardware mapping hint
  251. E_HARDWARE_MAPPING MappingHintVertex;
  252. E_HARDWARE_MAPPING MappingHintIndex;
  253. //! Primitive type used for rendering (triangles, lines, ...)
  254. E_PRIMITIVE_TYPE PrimitiveType;
  255. };
  256. } // end namespace scene
  257. } // end namespace irr
  258. #endif