CVertexBuffer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright (C) 2008-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 IRR_C_VERTEX_BUFFER_H_INCLUDED
  5. #define IRR_C_VERTEX_BUFFER_H_INCLUDED
  6. #include "IVertexBuffer.h"
  7. namespace irr
  8. {
  9. namespace scene
  10. {
  11. class CVertexBuffer : public IVertexBuffer
  12. {
  13. class IVertexList
  14. {
  15. public:
  16. virtual ~IVertexList(){};
  17. virtual u32 stride() const =0;
  18. virtual u32 size() const =0;
  19. virtual void push_back (const video::S3DVertex &element) =0;
  20. virtual void push_back(const video::S3DVertex2TCoords &element) =0;
  21. virtual void push_back(const video::S3DVertexTangents &element) =0;
  22. virtual void setValue(u32 index, const video::S3DVertex &value) =0;
  23. virtual void setValue(u32 index, const video::S3DVertex2TCoords &value) =0;
  24. virtual void setValue(u32 index, const video::S3DVertexTangents &value) =0;
  25. virtual video::S3DVertex& operator [](u32 index) = 0;
  26. virtual video::S3DVertex& operator [](const u32 index) const =0;
  27. virtual video::S3DVertex& getLast() =0;
  28. virtual void set_used(u32 usedNow) =0;
  29. virtual void reallocate(u32 new_size, bool canShrink=true) =0;
  30. virtual u32 allocated_size() const =0;
  31. virtual void* pointer() =0;
  32. virtual const void* const_pointer() const =0;
  33. virtual video::E_VERTEX_TYPE getType() const =0;
  34. };
  35. template <class T>
  36. class CSpecificVertexList : public IVertexList
  37. {
  38. public:
  39. core::array<T> Vertices;
  40. virtual u32 stride() const IRR_OVERRIDE {return sizeof(T);}
  41. virtual u32 size() const IRR_OVERRIDE {return Vertices.size();}
  42. virtual void push_back (const video::S3DVertex &element) IRR_OVERRIDE
  43. {Vertices.push_back(element);}
  44. virtual void push_back(const video::S3DVertex2TCoords &element) IRR_OVERRIDE
  45. {Vertices.push_back(element);}
  46. virtual void push_back(const video::S3DVertexTangents &element) IRR_OVERRIDE
  47. {Vertices.push_back(element);}
  48. virtual void setValue(u32 index, const video::S3DVertex &value) IRR_OVERRIDE
  49. {Vertices[index] = value;}
  50. virtual void setValue(u32 index, const video::S3DVertex2TCoords &value) IRR_OVERRIDE
  51. {Vertices[index] = value;}
  52. virtual void setValue(u32 index, const video::S3DVertexTangents &value) IRR_OVERRIDE
  53. {Vertices[index] = value;}
  54. virtual video::S3DVertex& operator [](u32 index) IRR_OVERRIDE
  55. {return (video::S3DVertex&)Vertices[index];}
  56. virtual video::S3DVertex& operator [](const u32 index) const IRR_OVERRIDE
  57. {return (video::S3DVertex&)Vertices[index];}
  58. virtual video::S3DVertex& getLast() IRR_OVERRIDE
  59. {return (video::S3DVertex&)Vertices.getLast();}
  60. virtual void set_used(u32 usedNow) IRR_OVERRIDE
  61. {Vertices.set_used(usedNow);}
  62. virtual void reallocate(u32 new_size, bool canShrink) IRR_OVERRIDE
  63. {Vertices.reallocate(new_size, canShrink);}
  64. virtual u32 allocated_size() const IRR_OVERRIDE
  65. {
  66. return Vertices.allocated_size();
  67. }
  68. virtual void* pointer() IRR_OVERRIDE {return Vertices.pointer();}
  69. virtual const void* const_pointer() const IRR_OVERRIDE {return Vertices.const_pointer();}
  70. virtual video::E_VERTEX_TYPE getType() const IRR_OVERRIDE {return T::getType();}
  71. };
  72. public:
  73. IVertexList *Vertices;
  74. CVertexBuffer(video::E_VERTEX_TYPE vertexType) : Vertices(0),
  75. MappingHint(EHM_NEVER), ChangedID(1)
  76. {
  77. CVertexBuffer::setType(vertexType);
  78. }
  79. CVertexBuffer(const IVertexBuffer &VertexBufferCopy) :
  80. Vertices(0), MappingHint(EHM_NEVER),
  81. ChangedID(1)
  82. {
  83. CVertexBuffer::setType(VertexBufferCopy.getType());
  84. CVertexBuffer::reallocate(VertexBufferCopy.size());
  85. for (u32 n=0;n<VertexBufferCopy.size();++n)
  86. CVertexBuffer::push_back(VertexBufferCopy[n]);
  87. }
  88. virtual ~CVertexBuffer()
  89. {
  90. delete Vertices;
  91. }
  92. virtual void setType(video::E_VERTEX_TYPE vertexType) IRR_OVERRIDE
  93. {
  94. if ( Vertices && Vertices->getType() == vertexType )
  95. return;
  96. IVertexList *NewVertices=0;
  97. switch (vertexType)
  98. {
  99. case video::EVT_STANDARD:
  100. {
  101. NewVertices=new CSpecificVertexList<video::S3DVertex>;
  102. break;
  103. }
  104. case video::EVT_2TCOORDS:
  105. {
  106. NewVertices=new CSpecificVertexList<video::S3DVertex2TCoords>;
  107. break;
  108. }
  109. case video::EVT_TANGENTS:
  110. {
  111. NewVertices=new CSpecificVertexList<video::S3DVertexTangents>;
  112. break;
  113. }
  114. }
  115. if (Vertices)
  116. {
  117. NewVertices->reallocate( Vertices->size() );
  118. switch (Vertices->getType()) // old type
  119. {
  120. case video::EVT_STANDARD:
  121. {
  122. for(u32 n=0;n<Vertices->size();++n)
  123. NewVertices->push_back((*Vertices)[n]);
  124. break;
  125. }
  126. case video::EVT_2TCOORDS:
  127. {
  128. for(u32 n=0;n<Vertices->size();++n)
  129. NewVertices->push_back((video::S3DVertex2TCoords&)(*Vertices)[n]);
  130. break;
  131. }
  132. case video::EVT_TANGENTS:
  133. {
  134. for(u32 n=0;n<Vertices->size();++n)
  135. NewVertices->push_back((video::S3DVertexTangents&)(*Vertices)[n]);
  136. break;
  137. }
  138. }
  139. delete Vertices;
  140. }
  141. Vertices=NewVertices;
  142. }
  143. virtual void* getData() IRR_OVERRIDE {return Vertices->pointer();}
  144. virtual const void* getData() const IRR_OVERRIDE {return Vertices->const_pointer();}
  145. virtual video::E_VERTEX_TYPE getType() const IRR_OVERRIDE {return Vertices->getType();}
  146. virtual u32 stride() const IRR_OVERRIDE {return Vertices->stride();}
  147. virtual u32 size() const IRR_OVERRIDE
  148. {
  149. return Vertices->size();
  150. }
  151. virtual void push_back (const video::S3DVertex &element) IRR_OVERRIDE
  152. {
  153. Vertices->push_back(element);
  154. }
  155. virtual void push_back(const video::S3DVertex2TCoords &element) IRR_OVERRIDE
  156. {
  157. Vertices->push_back(element);
  158. }
  159. virtual void push_back(const video::S3DVertexTangents &element) IRR_OVERRIDE
  160. {
  161. Vertices->push_back(element);
  162. }
  163. virtual void setValue(u32 index, const video::S3DVertex &value) IRR_OVERRIDE
  164. {
  165. Vertices->setValue(index, value);
  166. }
  167. virtual void setValue(u32 index, const video::S3DVertex2TCoords &value) IRR_OVERRIDE
  168. {
  169. Vertices->setValue(index, value);
  170. }
  171. virtual void setValue(u32 index, const video::S3DVertexTangents &value) IRR_OVERRIDE
  172. {
  173. Vertices->setValue(index, value);
  174. }
  175. virtual video::S3DVertex& operator [](u32 index) IRR_OVERRIDE
  176. {
  177. return (*Vertices)[index];
  178. }
  179. virtual video::S3DVertex& operator [](const u32 index) const IRR_OVERRIDE
  180. {
  181. return (*Vertices)[index];
  182. }
  183. virtual video::S3DVertex& getLast() IRR_OVERRIDE
  184. {
  185. return Vertices->getLast();
  186. }
  187. virtual void set_used(u32 usedNow) IRR_OVERRIDE
  188. {
  189. Vertices->set_used(usedNow);
  190. }
  191. virtual void reallocate(u32 new_size, bool canShrink=true) IRR_OVERRIDE
  192. {
  193. Vertices->reallocate(new_size, canShrink);
  194. }
  195. virtual u32 allocated_size() const IRR_OVERRIDE
  196. {
  197. return Vertices->allocated_size();
  198. }
  199. //! get the current hardware mapping hint
  200. virtual E_HARDWARE_MAPPING getHardwareMappingHint() const IRR_OVERRIDE
  201. {
  202. return MappingHint;
  203. }
  204. //! set the hardware mapping hint, for driver
  205. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint ) IRR_OVERRIDE
  206. {
  207. MappingHint=NewMappingHint;
  208. }
  209. //! flags the mesh as changed, reloads hardware buffers
  210. virtual void setDirty() IRR_OVERRIDE
  211. {
  212. ++ChangedID;
  213. }
  214. //! Get the currently used ID for identification of changes.
  215. /** This shouldn't be used for anything outside the VideoDriver. */
  216. virtual u32 getChangedID() const IRR_OVERRIDE {return ChangedID;}
  217. E_HARDWARE_MAPPING MappingHint;
  218. u32 ChangedID;
  219. };
  220. } // end namespace scene
  221. } // end namespace irr
  222. #endif