CVertexBuffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 __C_VERTEX_BUFFER_H_INCLUDED__
  5. #define __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 video::S3DVertex& operator [](const u32 index) const =0;
  21. virtual video::S3DVertex& getLast() =0;
  22. virtual void set_used(u32 usedNow) =0;
  23. virtual void reallocate(u32 new_size) =0;
  24. virtual u32 allocated_size() const =0;
  25. virtual video::S3DVertex* pointer() =0;
  26. virtual video::E_VERTEX_TYPE getType() const =0;
  27. };
  28. template <class T>
  29. class CSpecificVertexList : public IVertexList
  30. {
  31. public:
  32. core::array<T> Vertices;
  33. virtual u32 stride() const _IRR_OVERRIDE_ {return sizeof(T);}
  34. virtual u32 size() const _IRR_OVERRIDE_ {return Vertices.size();}
  35. virtual void push_back (const video::S3DVertex &element) _IRR_OVERRIDE_
  36. {Vertices.push_back((T&)element);}
  37. virtual video::S3DVertex& operator [](const u32 index) const _IRR_OVERRIDE_
  38. {return (video::S3DVertex&)Vertices[index];}
  39. virtual video::S3DVertex& getLast() _IRR_OVERRIDE_
  40. {return (video::S3DVertex&)Vertices.getLast();}
  41. virtual void set_used(u32 usedNow) _IRR_OVERRIDE_
  42. {Vertices.set_used(usedNow);}
  43. virtual void reallocate(u32 new_size) _IRR_OVERRIDE_
  44. {Vertices.reallocate(new_size);}
  45. virtual u32 allocated_size() const _IRR_OVERRIDE_
  46. {
  47. return Vertices.allocated_size();
  48. }
  49. virtual video::S3DVertex* pointer() _IRR_OVERRIDE_ {return Vertices.pointer();}
  50. virtual video::E_VERTEX_TYPE getType() const _IRR_OVERRIDE_ {return T::getType();}
  51. };
  52. public:
  53. IVertexList *Vertices;
  54. CVertexBuffer(video::E_VERTEX_TYPE vertexType) : Vertices(0),
  55. MappingHint(EHM_NEVER), ChangedID(1)
  56. {
  57. setType(vertexType);
  58. }
  59. CVertexBuffer(const IVertexBuffer &VertexBufferCopy) :
  60. Vertices(0), MappingHint(EHM_NEVER),
  61. ChangedID(1)
  62. {
  63. setType(VertexBufferCopy.getType());
  64. reallocate(VertexBufferCopy.size());
  65. for (u32 n=0;n<VertexBufferCopy.size();++n)
  66. push_back(VertexBufferCopy[n]);
  67. }
  68. virtual ~CVertexBuffer()
  69. {
  70. delete Vertices;
  71. }
  72. virtual void setType(video::E_VERTEX_TYPE vertexType) _IRR_OVERRIDE_
  73. {
  74. IVertexList *NewVertices=0;
  75. switch (vertexType)
  76. {
  77. case video::EVT_STANDARD:
  78. {
  79. NewVertices=new CSpecificVertexList<video::S3DVertex>;
  80. break;
  81. }
  82. case video::EVT_2TCOORDS:
  83. {
  84. NewVertices=new CSpecificVertexList<video::S3DVertex2TCoords>;
  85. break;
  86. }
  87. case video::EVT_TANGENTS:
  88. {
  89. NewVertices=new CSpecificVertexList<video::S3DVertexTangents>;
  90. break;
  91. }
  92. }
  93. if (Vertices)
  94. {
  95. NewVertices->reallocate( Vertices->size() );
  96. for(u32 n=0;n<Vertices->size();++n)
  97. NewVertices->push_back((*Vertices)[n]);
  98. delete Vertices;
  99. }
  100. Vertices=NewVertices;
  101. }
  102. virtual void* getData() _IRR_OVERRIDE_ {return Vertices->pointer();}
  103. virtual video::E_VERTEX_TYPE getType() const _IRR_OVERRIDE_ {return Vertices->getType();}
  104. virtual u32 stride() const _IRR_OVERRIDE_ {return Vertices->stride();}
  105. virtual u32 size() const _IRR_OVERRIDE_
  106. {
  107. return Vertices->size();
  108. }
  109. virtual void push_back (const video::S3DVertex &element) _IRR_OVERRIDE_
  110. {
  111. Vertices->push_back(element);
  112. }
  113. virtual video::S3DVertex& operator [](const u32 index) const _IRR_OVERRIDE_
  114. {
  115. return (*Vertices)[index];
  116. }
  117. virtual video::S3DVertex& getLast() _IRR_OVERRIDE_
  118. {
  119. return Vertices->getLast();
  120. }
  121. virtual void set_used(u32 usedNow) _IRR_OVERRIDE_
  122. {
  123. Vertices->set_used(usedNow);
  124. }
  125. virtual void reallocate(u32 new_size) _IRR_OVERRIDE_
  126. {
  127. Vertices->reallocate(new_size);
  128. }
  129. virtual u32 allocated_size() const _IRR_OVERRIDE_
  130. {
  131. return Vertices->allocated_size();
  132. }
  133. virtual video::S3DVertex* pointer() _IRR_OVERRIDE_
  134. {
  135. return Vertices->pointer();
  136. }
  137. //! get the current hardware mapping hint
  138. virtual E_HARDWARE_MAPPING getHardwareMappingHint() const _IRR_OVERRIDE_
  139. {
  140. return MappingHint;
  141. }
  142. //! set the hardware mapping hint, for driver
  143. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint ) _IRR_OVERRIDE_
  144. {
  145. MappingHint=NewMappingHint;
  146. }
  147. //! flags the mesh as changed, reloads hardware buffers
  148. virtual void setDirty() _IRR_OVERRIDE_
  149. {
  150. ++ChangedID;
  151. }
  152. //! Get the currently used ID for identification of changes.
  153. /** This shouldn't be used for anything outside the VideoDriver. */
  154. virtual u32 getChangedID() const _IRR_OVERRIDE_ {return ChangedID;}
  155. E_HARDWARE_MAPPING MappingHint;
  156. u32 ChangedID;
  157. };
  158. } // end namespace scene
  159. } // end namespace irr
  160. #endif