2
0

IMeshBuffer.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "IReferenceCounted.h"
  6. #include "SMaterial.h"
  7. #include "aabbox3d.h"
  8. #include "IVertexBuffer.h"
  9. #include "IIndexBuffer.h"
  10. #include "EHardwareBufferFlags.h"
  11. #include "EPrimitiveTypes.h"
  12. namespace irr
  13. {
  14. namespace scene
  15. {
  16. //! Struct for holding a mesh with a single material.
  17. /** A part of an IMesh which has the same material on each face of that
  18. group. Logical groups of an IMesh need not be put into separate mesh
  19. buffers, but can be. Separately animated parts of the mesh must be put
  20. into separate mesh buffers.
  21. Some mesh buffer implementations have limitations on the number of
  22. vertices the buffer can hold. In that case, logical grouping can help.
  23. Moreover, the number of vertices should be optimized for the GPU upload,
  24. which often depends on the type of gfx card. Typical figures are
  25. 1000-10000 vertices per buffer.
  26. SMeshBuffer is a simple implementation of a MeshBuffer, which supports
  27. up to 65535 vertices.
  28. Since meshbuffers are used for drawing, and hence will be exposed
  29. to the driver, chances are high that they are grab()'ed from somewhere.
  30. It's therefore required to dynamically allocate meshbuffers which are
  31. passed to a video driver and only drop the buffer once it's not used in
  32. the current code block anymore.
  33. */
  34. class IMeshBuffer : public virtual IReferenceCounted
  35. {
  36. public:
  37. //! Get the material of this meshbuffer
  38. /** \return Material of this buffer. */
  39. virtual video::SMaterial &getMaterial() = 0;
  40. //! Get the material of this meshbuffer
  41. /** \return Material of this buffer. */
  42. virtual const video::SMaterial &getMaterial() const = 0;
  43. /// Get the vertex buffer
  44. virtual const scene::IVertexBuffer *getVertexBuffer() const = 0;
  45. /// Get the vertex buffer
  46. virtual scene::IVertexBuffer *getVertexBuffer() = 0;
  47. /// Get the index buffer
  48. virtual const scene::IIndexBuffer *getIndexBuffer() const = 0;
  49. /// Get the index buffer
  50. virtual scene::IIndexBuffer *getIndexBuffer() = 0;
  51. //! Get the axis aligned bounding box of this meshbuffer.
  52. /** \return Axis aligned bounding box of this buffer. */
  53. virtual const core::aabbox3df &getBoundingBox() const = 0;
  54. //! Set axis aligned bounding box
  55. /** \param box User defined axis aligned bounding box to use
  56. for this buffer. */
  57. virtual void setBoundingBox(const core::aabbox3df &box) = 0;
  58. //! Recalculates the bounding box. Should be called if the mesh changed.
  59. virtual void recalculateBoundingBox() = 0;
  60. //! Append the vertices and indices to the current buffer
  61. /** Only works for compatible vertex types.
  62. \param vertices Pointer to a vertex array.
  63. \param numVertices Number of vertices in the array.
  64. \param indices Pointer to index array.
  65. \param numIndices Number of indices in array. */
  66. virtual void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) = 0;
  67. /* Leftover functions that are now just helpers for accessing the respective buffer. */
  68. //! Get type of vertex data which is stored in this meshbuffer.
  69. /** \return Vertex type of this buffer. */
  70. inline video::E_VERTEX_TYPE getVertexType() const
  71. {
  72. return getVertexBuffer()->getType();
  73. }
  74. //! Get access to vertex data. The data is an array of vertices.
  75. /** Which vertex type is used can be determined by getVertexType().
  76. \return Pointer to array of vertices. */
  77. inline const void *getVertices() const
  78. {
  79. return getVertexBuffer()->getData();
  80. }
  81. //! Get access to vertex data. The data is an array of vertices.
  82. /** Which vertex type is used can be determined by getVertexType().
  83. \return Pointer to array of vertices. */
  84. inline void *getVertices()
  85. {
  86. return getVertexBuffer()->getData();
  87. }
  88. //! Get amount of vertices in meshbuffer.
  89. /** \return Number of vertices in this buffer. */
  90. inline u32 getVertexCount() const
  91. {
  92. return getVertexBuffer()->getCount();
  93. }
  94. //! Get type of index data which is stored in this meshbuffer.
  95. /** \return Index type of this buffer. */
  96. inline video::E_INDEX_TYPE getIndexType() const
  97. {
  98. return getIndexBuffer()->getType();
  99. }
  100. //! Get access to indices.
  101. /** \return Pointer to indices array. */
  102. inline const u16 *getIndices() const
  103. {
  104. _IRR_DEBUG_BREAK_IF(getIndexBuffer()->getType() != video::EIT_16BIT);
  105. return static_cast<const u16*>(getIndexBuffer()->getData());
  106. }
  107. //! Get access to indices.
  108. /** \return Pointer to indices array. */
  109. inline u16 *getIndices()
  110. {
  111. _IRR_DEBUG_BREAK_IF(getIndexBuffer()->getType() != video::EIT_16BIT);
  112. return static_cast<u16*>(getIndexBuffer()->getData());
  113. }
  114. //! Get amount of indices in this meshbuffer.
  115. /** \return Number of indices in this buffer. */
  116. inline u32 getIndexCount() const
  117. {
  118. return getIndexBuffer()->getCount();
  119. }
  120. //! returns position of vertex i
  121. inline const core::vector3df &getPosition(u32 i) const
  122. {
  123. return getVertexBuffer()->getPosition(i);
  124. }
  125. //! returns position of vertex i
  126. inline core::vector3df &getPosition(u32 i)
  127. {
  128. return getVertexBuffer()->getPosition(i);
  129. }
  130. //! returns normal of vertex i
  131. inline const core::vector3df &getNormal(u32 i) const
  132. {
  133. return getVertexBuffer()->getNormal(i);
  134. }
  135. //! returns normal of vertex i
  136. inline core::vector3df &getNormal(u32 i)
  137. {
  138. return getVertexBuffer()->getNormal(i);
  139. }
  140. //! returns texture coord of vertex i
  141. inline const core::vector2df &getTCoords(u32 i) const
  142. {
  143. return getVertexBuffer()->getTCoords(i);
  144. }
  145. //! returns texture coord of vertex i
  146. inline core::vector2df &getTCoords(u32 i)
  147. {
  148. return getVertexBuffer()->getTCoords(i);
  149. }
  150. //! set the hardware mapping hint, for driver
  151. inline void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX)
  152. {
  153. if (buffer == EBT_VERTEX_AND_INDEX || buffer == EBT_VERTEX)
  154. getVertexBuffer()->setHardwareMappingHint(newMappingHint);
  155. if (buffer == EBT_VERTEX_AND_INDEX || buffer == EBT_INDEX)
  156. getIndexBuffer()->setHardwareMappingHint(newMappingHint);
  157. }
  158. //! flags the meshbuffer as changed, reloads hardware buffers
  159. inline void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX)
  160. {
  161. if (buffer == EBT_VERTEX_AND_INDEX || buffer == EBT_VERTEX)
  162. getVertexBuffer()->setDirty();
  163. if (buffer == EBT_VERTEX_AND_INDEX || buffer == EBT_INDEX)
  164. getIndexBuffer()->setDirty();
  165. }
  166. /* End helpers */
  167. //! Describe what kind of primitive geometry is used by the meshbuffer
  168. /** Note: Default is EPT_TRIANGLES. Using other types is fine for rendering.
  169. But meshbuffer manipulation functions might expect type EPT_TRIANGLES
  170. to work correctly. Also mesh writers will generally fail (badly!) with other
  171. types than EPT_TRIANGLES. */
  172. virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) = 0;
  173. //! Get the kind of primitive geometry which is used by the meshbuffer
  174. virtual E_PRIMITIVE_TYPE getPrimitiveType() const = 0;
  175. //! Calculate how many geometric primitives are used by this meshbuffer
  176. u32 getPrimitiveCount() const
  177. {
  178. return getIndexBuffer()->getPrimitiveCount(getPrimitiveType());
  179. }
  180. //! Calculate size of vertices and indices in memory
  181. size_t getSize() const
  182. {
  183. size_t ret = 0;
  184. switch (getVertexType()) {
  185. case video::EVT_STANDARD:
  186. ret += sizeof(video::S3DVertex) * getVertexCount();
  187. break;
  188. case video::EVT_2TCOORDS:
  189. ret += sizeof(video::S3DVertex2TCoords) * getVertexCount();
  190. break;
  191. case video::EVT_TANGENTS:
  192. ret += sizeof(video::S3DVertexTangents) * getVertexCount();
  193. break;
  194. }
  195. switch (getIndexType()) {
  196. case video::EIT_16BIT:
  197. ret += sizeof(u16) * getIndexCount();
  198. break;
  199. case video::EIT_32BIT:
  200. ret += sizeof(u32) * getIndexCount();
  201. break;
  202. }
  203. return ret;
  204. }
  205. };
  206. } // end namespace scene
  207. } // end namespace irr