CMeshBuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 IRR_T_MESH_BUFFER_H_INCLUDED
  5. #define IRR_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 for 16-bit buffers
  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. //! returns color of vertex i
  145. virtual video::SColor& getColor(u32 i) IRR_OVERRIDE
  146. {
  147. return Vertices[i].Color;
  148. }
  149. //! returns color of vertex i
  150. virtual const video::SColor& getColor(u32 i) const IRR_OVERRIDE
  151. {
  152. return Vertices[i].Color;
  153. }
  154. //! Append the vertices and indices to the current buffer
  155. /** Only works for compatible types, i.e. either the same type
  156. or the main buffer is of standard type. Otherwise, behavior is
  157. undefined. Also can't append it's own vertices/indices to itself.
  158. */
  159. virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices, bool updateBoundingBox=true) IRR_OVERRIDE
  160. {
  161. if (vertices == getVertices() || indices == getIndices()) // can't do that because we're doing reallocations on those blocks
  162. return;
  163. const u32 vertexCount = getVertexCount();
  164. u32 i;
  165. Vertices.reallocate(vertexCount+numVertices, false);
  166. for (i=0; i<numVertices; ++i)
  167. {
  168. Vertices.push_back(static_cast<const T*>(vertices)[i]);
  169. }
  170. if ( updateBoundingBox && numVertices > 0)
  171. {
  172. if ( vertexCount == 0 )
  173. BoundingBox.reset(static_cast<const T*>(vertices)[0].Pos);
  174. for (i=0; i<numVertices; ++i)
  175. BoundingBox.addInternalPoint(static_cast<const T*>(vertices)[i].Pos);
  176. }
  177. Indices.reallocate(getIndexCount()+numIndices, false);
  178. for (i=0; i<numIndices; ++i)
  179. {
  180. Indices.push_back(indices[i]+vertexCount);
  181. }
  182. setDirty();
  183. }
  184. //! Append the meshbuffer to the current buffer
  185. virtual void append(const IMeshBuffer* const other, bool updateBoundingBox=true) IRR_OVERRIDE
  186. {
  187. if ( getVertexType() != other->getVertexType() )
  188. return;
  189. append(other->getVertices(), other->getVertexCount(), other->getIndices(), other->getIndexCount(), updateBoundingBox);
  190. }
  191. //! get the current hardware mapping hint
  192. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const IRR_OVERRIDE
  193. {
  194. return MappingHint_Vertex;
  195. }
  196. //! get the current hardware mapping hint
  197. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const IRR_OVERRIDE
  198. {
  199. return MappingHint_Index;
  200. }
  201. //! set the hardware mapping hint, for driver
  202. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX ) IRR_OVERRIDE
  203. {
  204. if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
  205. MappingHint_Vertex=NewMappingHint;
  206. if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
  207. MappingHint_Index=NewMappingHint;
  208. }
  209. //! Describe what kind of primitive geometry is used by the meshbuffer
  210. virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) IRR_OVERRIDE
  211. {
  212. PrimitiveType = type;
  213. }
  214. //! Get the kind of primitive geometry which is used by the meshbuffer
  215. virtual E_PRIMITIVE_TYPE getPrimitiveType() const IRR_OVERRIDE
  216. {
  217. return PrimitiveType;
  218. }
  219. //! flags the mesh as changed, reloads hardware buffers
  220. virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) IRR_OVERRIDE
  221. {
  222. if (Buffer==EBT_VERTEX_AND_INDEX ||Buffer==EBT_VERTEX)
  223. ++ChangedID_Vertex;
  224. if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
  225. ++ChangedID_Index;
  226. }
  227. //! Get the currently used ID for identification of changes.
  228. /** This shouldn't be used for anything outside the VideoDriver. */
  229. virtual u32 getChangedID_Vertex() const IRR_OVERRIDE {return ChangedID_Vertex;}
  230. //! Get the currently used ID for identification of changes.
  231. /** This shouldn't be used for anything outside the VideoDriver. */
  232. virtual u32 getChangedID_Index() const IRR_OVERRIDE {return ChangedID_Index;}
  233. //! Returns type of the class implementing the IMeshBuffer
  234. virtual EMESH_BUFFER_TYPE getType() const IRR_OVERRIDE
  235. {
  236. return getTypeT();
  237. }
  238. //! Create copy of the meshbuffer
  239. virtual IMeshBuffer* createClone(int cloneFlags) const IRR_OVERRIDE
  240. {
  241. CMeshBuffer<T> * clone = new CMeshBuffer<T>();
  242. if (cloneFlags & ECF_VERTICES)
  243. {
  244. clone->Vertices = Vertices;
  245. clone->BoundingBox = BoundingBox;
  246. }
  247. if (cloneFlags & ECF_INDICES)
  248. {
  249. clone->Indices = Indices;
  250. }
  251. clone->PrimitiveType = PrimitiveType;
  252. clone->Material = getMaterial();
  253. clone->MappingHint_Vertex = MappingHint_Vertex;
  254. clone->MappingHint_Index = MappingHint_Index;
  255. return clone;
  256. }
  257. //! Returns type of the class implementing the IMeshBuffer for template specialization
  258. // Minor note: Some compilers (VS) allow directly specializing the virtual function,
  259. // but this will fail on other compilers (GCC). So using a helper function.
  260. EMESH_BUFFER_TYPE getTypeT() const;
  261. u32 ChangedID_Vertex;
  262. u32 ChangedID_Index;
  263. //! hardware mapping hint
  264. E_HARDWARE_MAPPING MappingHint_Vertex;
  265. E_HARDWARE_MAPPING MappingHint_Index;
  266. //! Material for this meshbuffer.
  267. video::SMaterial Material;
  268. //! Vertices of this buffer
  269. core::array<T> Vertices;
  270. //! Indices into the vertices of this buffer.
  271. core::array<u16> Indices;
  272. //! Bounding box of this meshbuffer.
  273. core::aabbox3d<f32> BoundingBox;
  274. //! Primitive type used for rendering (triangles, lines, ...)
  275. E_PRIMITIVE_TYPE PrimitiveType;
  276. };
  277. //! Standard meshbuffer
  278. typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
  279. //! Meshbuffer with two texture coords per vertex, e.g. for lightmaps
  280. typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
  281. //! Meshbuffer with vertices having tangents stored, e.g. for normal mapping
  282. typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
  283. //! partial specialization to return types
  284. template <>
  285. inline EMESH_BUFFER_TYPE CMeshBuffer<video::S3DVertex>::getTypeT() const
  286. {
  287. return EMBT_STANDARD;
  288. }
  289. template <>
  290. inline EMESH_BUFFER_TYPE CMeshBuffer<video::S3DVertex2TCoords>::getTypeT() const
  291. {
  292. return EMBT_LIGHTMAP;
  293. }
  294. template <>
  295. inline EMESH_BUFFER_TYPE CMeshBuffer<video::S3DVertexTangents>::getTypeT() const
  296. {
  297. return EMBT_TANGENTS;
  298. }
  299. } // end namespace scene
  300. } // end namespace irr
  301. #endif