CMeshBuffer.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #pragma once
  5. #include "irrArray.h"
  6. #include "IMeshBuffer.h"
  7. namespace irr
  8. {
  9. namespace scene
  10. {
  11. //! Template implementation of the IMeshBuffer interface
  12. template <class T>
  13. class CMeshBuffer : public IMeshBuffer
  14. {
  15. public:
  16. //! Default constructor for empty meshbuffer
  17. CMeshBuffer() :
  18. ChangedID_Vertex(1), ChangedID_Index(1), MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER), HWBuffer(NULL), PrimitiveType(EPT_TRIANGLES)
  19. {
  20. #ifdef _DEBUG
  21. setDebugName("CMeshBuffer");
  22. #endif
  23. }
  24. //! Get material of this meshbuffer
  25. /** \return Material of this buffer */
  26. const video::SMaterial &getMaterial() const override
  27. {
  28. return Material;
  29. }
  30. //! Get material of this meshbuffer
  31. /** \return Material of this buffer */
  32. video::SMaterial &getMaterial() override
  33. {
  34. return Material;
  35. }
  36. //! Get pointer to vertices
  37. /** \return Pointer to vertices. */
  38. const void *getVertices() const override
  39. {
  40. return Vertices.const_pointer();
  41. }
  42. //! Get pointer to vertices
  43. /** \return Pointer to vertices. */
  44. void *getVertices() override
  45. {
  46. return Vertices.pointer();
  47. }
  48. //! Get number of vertices
  49. /** \return Number of vertices. */
  50. u32 getVertexCount() const override
  51. {
  52. return Vertices.size();
  53. }
  54. //! Get type of index data which is stored in this meshbuffer.
  55. /** \return Index type of this buffer. */
  56. video::E_INDEX_TYPE getIndexType() const override
  57. {
  58. return video::EIT_16BIT;
  59. }
  60. //! Get pointer to indices
  61. /** \return Pointer to indices. */
  62. const u16 *getIndices() const override
  63. {
  64. return Indices.const_pointer();
  65. }
  66. //! Get pointer to indices
  67. /** \return Pointer to indices. */
  68. u16 *getIndices() override
  69. {
  70. return Indices.pointer();
  71. }
  72. //! Get number of indices
  73. /** \return Number of indices. */
  74. u32 getIndexCount() const override
  75. {
  76. return Indices.size();
  77. }
  78. //! Get the axis aligned bounding box
  79. /** \return Axis aligned bounding box of this buffer. */
  80. const core::aabbox3d<f32> &getBoundingBox() const override
  81. {
  82. return BoundingBox;
  83. }
  84. //! Set the axis aligned bounding box
  85. /** \param box New axis aligned bounding box for this buffer. */
  86. //! set user axis aligned bounding box
  87. void setBoundingBox(const core::aabbox3df &box) override
  88. {
  89. BoundingBox = box;
  90. }
  91. //! Recalculate the bounding box.
  92. /** should be called if the mesh changed. */
  93. void recalculateBoundingBox() override
  94. {
  95. if (!Vertices.empty()) {
  96. BoundingBox.reset(Vertices[0].Pos);
  97. const irr::u32 vsize = Vertices.size();
  98. for (u32 i = 1; i < vsize; ++i)
  99. BoundingBox.addInternalPoint(Vertices[i].Pos);
  100. } else
  101. BoundingBox.reset(0, 0, 0);
  102. }
  103. //! Get type of vertex data stored in this buffer.
  104. /** \return Type of vertex data. */
  105. video::E_VERTEX_TYPE getVertexType() const override
  106. {
  107. return T::getType();
  108. }
  109. //! returns position of vertex i
  110. const core::vector3df &getPosition(u32 i) const override
  111. {
  112. return Vertices[i].Pos;
  113. }
  114. //! returns position of vertex i
  115. core::vector3df &getPosition(u32 i) override
  116. {
  117. return Vertices[i].Pos;
  118. }
  119. //! returns normal of vertex i
  120. const core::vector3df &getNormal(u32 i) const override
  121. {
  122. return Vertices[i].Normal;
  123. }
  124. //! returns normal of vertex i
  125. core::vector3df &getNormal(u32 i) override
  126. {
  127. return Vertices[i].Normal;
  128. }
  129. //! returns texture coord of vertex i
  130. const core::vector2df &getTCoords(u32 i) const override
  131. {
  132. return Vertices[i].TCoords;
  133. }
  134. //! returns texture coord of vertex i
  135. core::vector2df &getTCoords(u32 i) override
  136. {
  137. return Vertices[i].TCoords;
  138. }
  139. //! Append the vertices and indices to the current buffer
  140. /** Only works for compatible types, i.e. either the same type
  141. or the main buffer is of standard type. Otherwise, behavior is
  142. undefined.
  143. */
  144. void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override
  145. {
  146. if (vertices == getVertices())
  147. return;
  148. const u32 vertexCount = getVertexCount();
  149. u32 i;
  150. Vertices.reallocate(vertexCount + numVertices);
  151. for (i = 0; i < numVertices; ++i) {
  152. Vertices.push_back(static_cast<const T *>(vertices)[i]);
  153. BoundingBox.addInternalPoint(static_cast<const T *>(vertices)[i].Pos);
  154. }
  155. Indices.reallocate(getIndexCount() + numIndices);
  156. for (i = 0; i < numIndices; ++i) {
  157. Indices.push_back(indices[i] + vertexCount);
  158. }
  159. }
  160. //! get the current hardware mapping hint
  161. E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const override
  162. {
  163. return MappingHint_Vertex;
  164. }
  165. //! get the current hardware mapping hint
  166. E_HARDWARE_MAPPING getHardwareMappingHint_Index() const override
  167. {
  168. return MappingHint_Index;
  169. }
  170. //! set the hardware mapping hint, for driver
  171. void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer = EBT_VERTEX_AND_INDEX) override
  172. {
  173. if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
  174. MappingHint_Vertex = NewMappingHint;
  175. if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
  176. MappingHint_Index = NewMappingHint;
  177. }
  178. //! Describe what kind of primitive geometry is used by the meshbuffer
  179. void setPrimitiveType(E_PRIMITIVE_TYPE type) override
  180. {
  181. PrimitiveType = type;
  182. }
  183. //! Get the kind of primitive geometry which is used by the meshbuffer
  184. E_PRIMITIVE_TYPE getPrimitiveType() const override
  185. {
  186. return PrimitiveType;
  187. }
  188. //! flags the mesh as changed, reloads hardware buffers
  189. void setDirty(E_BUFFER_TYPE Buffer = EBT_VERTEX_AND_INDEX) override
  190. {
  191. if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
  192. ++ChangedID_Vertex;
  193. if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
  194. ++ChangedID_Index;
  195. }
  196. //! Get the currently used ID for identification of changes.
  197. /** This shouldn't be used for anything outside the VideoDriver. */
  198. u32 getChangedID_Vertex() const override { return ChangedID_Vertex; }
  199. //! Get the currently used ID for identification of changes.
  200. /** This shouldn't be used for anything outside the VideoDriver. */
  201. u32 getChangedID_Index() const override { return ChangedID_Index; }
  202. void setHWBuffer(void *ptr) const override
  203. {
  204. HWBuffer = ptr;
  205. }
  206. void *getHWBuffer() const override
  207. {
  208. return HWBuffer;
  209. }
  210. u32 ChangedID_Vertex;
  211. u32 ChangedID_Index;
  212. //! hardware mapping hint
  213. E_HARDWARE_MAPPING MappingHint_Vertex;
  214. E_HARDWARE_MAPPING MappingHint_Index;
  215. mutable void *HWBuffer;
  216. //! Material for this meshbuffer.
  217. video::SMaterial Material;
  218. //! Vertices of this buffer
  219. core::array<T> Vertices;
  220. //! Indices into the vertices of this buffer.
  221. core::array<u16> Indices;
  222. //! Bounding box of this meshbuffer.
  223. core::aabbox3d<f32> BoundingBox;
  224. //! Primitive type used for rendering (triangles, lines, ...)
  225. E_PRIMITIVE_TYPE PrimitiveType;
  226. };
  227. //! Standard meshbuffer
  228. typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
  229. //! Meshbuffer with two texture coords per vertex, e.g. for lightmaps
  230. typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
  231. //! Meshbuffer with vertices having tangents stored, e.g. for normal mapping
  232. typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
  233. } // end namespace scene
  234. } // end namespace irr