IMeshBuffer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "S3DVertex.h"
  9. #include "SVertexIndex.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 type of vertex data which is stored in this meshbuffer.
  44. /** \return Vertex type of this buffer. */
  45. virtual video::E_VERTEX_TYPE getVertexType() const = 0;
  46. //! Get access to vertex data. The data is an array of vertices.
  47. /** Which vertex type is used can be determined by getVertexType().
  48. \return Pointer to array of vertices. */
  49. virtual const void *getVertices() const = 0;
  50. //! Get access to vertex data. The data is an array of vertices.
  51. /** Which vertex type is used can be determined by getVertexType().
  52. \return Pointer to array of vertices. */
  53. virtual void *getVertices() = 0;
  54. //! Get amount of vertices in meshbuffer.
  55. /** \return Number of vertices in this buffer. */
  56. virtual u32 getVertexCount() const = 0;
  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 = 0;
  60. //! Get access to indices.
  61. /** \return Pointer to indices array. */
  62. virtual const u16 *getIndices() const = 0;
  63. //! Get access to indices.
  64. /** \return Pointer to indices array. */
  65. virtual u16 *getIndices() = 0;
  66. //! Get amount of indices in this meshbuffer.
  67. /** \return Number of indices in this buffer. */
  68. virtual u32 getIndexCount() const = 0;
  69. //! Get the axis aligned bounding box of this meshbuffer.
  70. /** \return Axis aligned bounding box of this buffer. */
  71. virtual const core::aabbox3df &getBoundingBox() const = 0;
  72. //! Set axis aligned bounding box
  73. /** \param box User defined axis aligned bounding box to use
  74. for this buffer. */
  75. virtual void setBoundingBox(const core::aabbox3df &box) = 0;
  76. //! Recalculates the bounding box. Should be called if the mesh changed.
  77. virtual void recalculateBoundingBox() = 0;
  78. //! returns position of vertex i
  79. virtual const core::vector3df &getPosition(u32 i) const = 0;
  80. //! returns position of vertex i
  81. virtual core::vector3df &getPosition(u32 i) = 0;
  82. //! returns normal of vertex i
  83. virtual const core::vector3df &getNormal(u32 i) const = 0;
  84. //! returns normal of vertex i
  85. virtual core::vector3df &getNormal(u32 i) = 0;
  86. //! returns texture coord of vertex i
  87. virtual const core::vector2df &getTCoords(u32 i) const = 0;
  88. //! returns texture coord of vertex i
  89. virtual core::vector2df &getTCoords(u32 i) = 0;
  90. //! Append the vertices and indices to the current buffer
  91. /** Only works for compatible vertex types.
  92. \param vertices Pointer to a vertex array.
  93. \param numVertices Number of vertices in the array.
  94. \param indices Pointer to index array.
  95. \param numIndices Number of indices in array. */
  96. virtual void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) = 0;
  97. //! get the current hardware mapping hint
  98. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const = 0;
  99. //! get the current hardware mapping hint
  100. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const = 0;
  101. //! set the hardware mapping hint, for driver
  102. virtual void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) = 0;
  103. //! flags the meshbuffer as changed, reloads hardware buffers
  104. virtual void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) = 0;
  105. //! Get the currently used ID for identification of changes.
  106. /** This shouldn't be used for anything outside the VideoDriver. */
  107. virtual u32 getChangedID_Vertex() const = 0;
  108. //! Get the currently used ID for identification of changes.
  109. /** This shouldn't be used for anything outside the VideoDriver. */
  110. virtual u32 getChangedID_Index() const = 0;
  111. //! Used by the VideoDriver to remember the buffer link.
  112. virtual void setHWBuffer(void *ptr) const = 0;
  113. virtual void *getHWBuffer() const = 0;
  114. //! Describe what kind of primitive geometry is used by the meshbuffer
  115. /** Note: Default is EPT_TRIANGLES. Using other types is fine for rendering.
  116. But meshbuffer manipulation functions might expect type EPT_TRIANGLES
  117. to work correctly. Also mesh writers will generally fail (badly!) with other
  118. types than EPT_TRIANGLES. */
  119. virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) = 0;
  120. //! Get the kind of primitive geometry which is used by the meshbuffer
  121. virtual E_PRIMITIVE_TYPE getPrimitiveType() const = 0;
  122. //! Calculate how many geometric primitives are used by this meshbuffer
  123. virtual u32 getPrimitiveCount() const
  124. {
  125. const u32 indexCount = getIndexCount();
  126. switch (getPrimitiveType()) {
  127. case scene::EPT_POINTS:
  128. return indexCount;
  129. case scene::EPT_LINE_STRIP:
  130. return indexCount - 1;
  131. case scene::EPT_LINE_LOOP:
  132. return indexCount;
  133. case scene::EPT_LINES:
  134. return indexCount / 2;
  135. case scene::EPT_TRIANGLE_STRIP:
  136. return (indexCount - 2);
  137. case scene::EPT_TRIANGLE_FAN:
  138. return (indexCount - 2);
  139. case scene::EPT_TRIANGLES:
  140. return indexCount / 3;
  141. case scene::EPT_POINT_SPRITES:
  142. return indexCount;
  143. }
  144. return 0;
  145. }
  146. //! Calculate size of vertices and indices in memory
  147. virtual size_t getSize() const
  148. {
  149. size_t ret = 0;
  150. switch (getVertexType()) {
  151. case video::EVT_STANDARD:
  152. ret += sizeof(video::S3DVertex) * getVertexCount();
  153. break;
  154. case video::EVT_2TCOORDS:
  155. ret += sizeof(video::S3DVertex2TCoords) * getVertexCount();
  156. break;
  157. case video::EVT_TANGENTS:
  158. ret += sizeof(video::S3DVertexTangents) * getVertexCount();
  159. break;
  160. }
  161. switch (getIndexType()) {
  162. case video::EIT_16BIT:
  163. ret += sizeof(u16) * getIndexCount();
  164. break;
  165. case video::EIT_32BIT:
  166. ret += sizeof(u32) * getIndexCount();
  167. break;
  168. }
  169. return ret;
  170. }
  171. };
  172. } // end namespace scene
  173. } // end namespace irr