CMeshBuffer.h 7.4 KB

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