IMeshBuffer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 __I_MESH_BUFFER_H_INCLUDED__
  5. #define __I_MESH_BUFFER_H_INCLUDED__
  6. #include "IReferenceCounted.h"
  7. #include "SMaterial.h"
  8. #include "aabbox3d.h"
  9. #include "S3DVertex.h"
  10. #include "SVertexIndex.h"
  11. #include "EHardwareBufferFlags.h"
  12. #include "EPrimitiveTypes.h"
  13. namespace irr
  14. {
  15. namespace scene
  16. {
  17. //! Struct for holding a mesh with a single material.
  18. /** A part of an IMesh which has the same material on each face of that
  19. group. Logical groups of an IMesh need not be put into separate mesh
  20. buffers, but can be. Separately animated parts of the mesh must be put
  21. into separate mesh buffers.
  22. Some mesh buffer implementations have limitations on the number of
  23. vertices the buffer can hold. In that case, logical grouping can help.
  24. Moreover, the number of vertices should be optimized for the GPU upload,
  25. which often depends on the type of gfx card. Typical figures are
  26. 1000-10000 vertices per buffer.
  27. SMeshBuffer is a simple implementation of a MeshBuffer, which supports
  28. up to 65535 vertices.
  29. Since meshbuffers are used for drawing, and hence will be exposed
  30. to the driver, chances are high that they are grab()'ed from somewhere.
  31. It's therefore required to dynamically allocate meshbuffers which are
  32. passed to a video driver and only drop the buffer once it's not used in
  33. the current code block anymore.
  34. */
  35. class IMeshBuffer : public virtual IReferenceCounted
  36. {
  37. public:
  38. //! Get the material of this meshbuffer
  39. /** \return Material of this buffer. */
  40. virtual video::SMaterial& getMaterial() = 0;
  41. //! Get the material of this meshbuffer
  42. /** \return Material of this buffer. */
  43. virtual const video::SMaterial& getMaterial() const = 0;
  44. //! Get type of vertex data which is stored in this meshbuffer.
  45. /** \return Vertex type of this buffer. */
  46. virtual video::E_VERTEX_TYPE getVertexType() const = 0;
  47. //! Get access to vertex data. The data is an array of vertices.
  48. /** Which vertex type is used can be determined by getVertexType().
  49. \return Pointer to array of vertices. */
  50. virtual const void* getVertices() const = 0;
  51. //! Get access to vertex data. The data is an array of vertices.
  52. /** Which vertex type is used can be determined by getVertexType().
  53. \return Pointer to array of vertices. */
  54. virtual void* getVertices() = 0;
  55. //! Get amount of vertices in meshbuffer.
  56. /** \return Number of vertices in this buffer. */
  57. virtual u32 getVertexCount() const = 0;
  58. //! Get type of index data which is stored in this meshbuffer.
  59. /** \return Index type of this buffer. */
  60. virtual video::E_INDEX_TYPE getIndexType() const =0;
  61. //! Get access to indices.
  62. /** \return Pointer to indices array. */
  63. virtual const u16* getIndices() const = 0;
  64. //! Get access to indices.
  65. /** \return Pointer to indices array. */
  66. virtual u16* getIndices() = 0;
  67. //! Get amount of indices in this meshbuffer.
  68. /** \return Number of indices in this buffer. */
  69. virtual u32 getIndexCount() const = 0;
  70. //! Get the axis aligned bounding box of this meshbuffer.
  71. /** \return Axis aligned bounding box of this buffer. */
  72. virtual const core::aabbox3df& getBoundingBox() const = 0;
  73. //! Set axis aligned bounding box
  74. /** \param box User defined axis aligned bounding box to use
  75. for this buffer. */
  76. virtual void setBoundingBox(const core::aabbox3df& box) = 0;
  77. //! Recalculates the bounding box. Should be called if the mesh changed.
  78. virtual void recalculateBoundingBox() = 0;
  79. //! returns position of vertex i
  80. virtual const core::vector3df& getPosition(u32 i) const = 0;
  81. //! returns position of vertex i
  82. virtual core::vector3df& getPosition(u32 i) = 0;
  83. //! returns normal of vertex i
  84. virtual const core::vector3df& getNormal(u32 i) const = 0;
  85. //! returns normal of vertex i
  86. virtual core::vector3df& getNormal(u32 i) = 0;
  87. //! returns texture coord of vertex i
  88. virtual const core::vector2df& getTCoords(u32 i) const = 0;
  89. //! returns texture coord of vertex i
  90. virtual core::vector2df& getTCoords(u32 i) = 0;
  91. //! Append the vertices and indices to the current buffer
  92. /** Only works for compatible vertex types.
  93. \param vertices Pointer to a vertex array.
  94. \param numVertices Number of vertices in the array.
  95. \param indices Pointer to index array.
  96. \param numIndices Number of indices in array. */
  97. virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices) = 0;
  98. //! Append the meshbuffer to the current buffer
  99. /** Only works for compatible vertex types
  100. \param other Buffer to append to this one. */
  101. virtual void append(const IMeshBuffer* const other) = 0;
  102. //! get the current hardware mapping hint
  103. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const = 0;
  104. //! get the current hardware mapping hint
  105. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const = 0;
  106. //! set the hardware mapping hint, for driver
  107. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) = 0;
  108. //! flags the meshbuffer as changed, reloads hardware buffers
  109. virtual void setDirty(E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX) = 0;
  110. //! Get the currently used ID for identification of changes.
  111. /** This shouldn't be used for anything outside the VideoDriver. */
  112. virtual u32 getChangedID_Vertex() const = 0;
  113. //! Get the currently used ID for identification of changes.
  114. /** This shouldn't be used for anything outside the VideoDriver. */
  115. virtual u32 getChangedID_Index() const = 0;
  116. //! Describe what kind of primitive geometry is used by the meshbuffer
  117. /** Note: Default is EPT_TRIANGLES. Using other types is fine for rendering.
  118. But meshbuffer manipulation functions might expect type EPT_TRIANGLES
  119. to work correctly. Also mesh writers will generally fail (badly!) with other
  120. types than EPT_TRIANGLES. */
  121. virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) = 0;
  122. //! Get the kind of primitive geometry which is used by the meshbuffer
  123. virtual E_PRIMITIVE_TYPE getPrimitiveType() const = 0;
  124. //! Calculate how many geometric primitives are used by this meshbuffer
  125. virtual u32 getPrimitiveCount() const
  126. {
  127. const u32 indexCount = getIndexCount();
  128. switch (getPrimitiveType())
  129. {
  130. case scene::EPT_POINTS: return indexCount;
  131. case scene::EPT_LINE_STRIP: return indexCount-1;
  132. case scene::EPT_LINE_LOOP: return indexCount;
  133. case scene::EPT_LINES: return indexCount/2;
  134. case scene::EPT_TRIANGLE_STRIP: return (indexCount-2);
  135. case scene::EPT_TRIANGLE_FAN: return (indexCount-2);
  136. case scene::EPT_TRIANGLES: return indexCount/3;
  137. case scene::EPT_QUAD_STRIP: return (indexCount-2)/2;
  138. case scene::EPT_QUADS: return indexCount/4;
  139. case scene::EPT_POLYGON: return indexCount; // (not really primitives, that would be 1, works like line_strip)
  140. case scene::EPT_POINT_SPRITES: return indexCount;
  141. }
  142. return 0;
  143. }
  144. };
  145. } // end namespace scene
  146. } // end namespace irr
  147. #endif