CMeshBuffer.h 8.7 KB

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