CMeshBuffer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <vector>
  6. #include "IMeshBuffer.h"
  7. #include "CVertexBuffer.h"
  8. #include "CIndexBuffer.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. //! Template implementation of the IMeshBuffer interface
  14. template <class T>
  15. class CMeshBuffer final : public IMeshBuffer
  16. {
  17. public:
  18. //! Default constructor for empty meshbuffer
  19. CMeshBuffer() :
  20. PrimitiveType(EPT_TRIANGLES)
  21. {
  22. #ifdef _DEBUG
  23. setDebugName("CMeshBuffer");
  24. #endif
  25. Vertices = new CVertexBuffer<T>();
  26. Indices = new SIndexBuffer();
  27. }
  28. ~CMeshBuffer()
  29. {
  30. Vertices->drop();
  31. Indices->drop();
  32. }
  33. //! Get material of this meshbuffer
  34. /** \return Material of this buffer */
  35. const video::SMaterial &getMaterial() const override
  36. {
  37. return Material;
  38. }
  39. //! Get material of this meshbuffer
  40. /** \return Material of this buffer */
  41. video::SMaterial &getMaterial() override
  42. {
  43. return Material;
  44. }
  45. const scene::IVertexBuffer *getVertexBuffer() const override
  46. {
  47. return Vertices;
  48. }
  49. scene::IVertexBuffer *getVertexBuffer() override
  50. {
  51. return Vertices;
  52. }
  53. const scene::IIndexBuffer *getIndexBuffer() const override
  54. {
  55. return Indices;
  56. }
  57. scene::IIndexBuffer *getIndexBuffer() override
  58. {
  59. return Indices;
  60. }
  61. //! Get the axis aligned bounding box
  62. /** \return Axis aligned bounding box of this buffer. */
  63. const core::aabbox3d<f32> &getBoundingBox() const override
  64. {
  65. return BoundingBox;
  66. }
  67. //! Set the axis aligned bounding box
  68. /** \param box New axis aligned bounding box for this buffer. */
  69. //! set user axis aligned bounding box
  70. void setBoundingBox(const core::aabbox3df &box) override
  71. {
  72. BoundingBox = box;
  73. }
  74. //! Recalculate the bounding box.
  75. /** should be called if the mesh changed. */
  76. void recalculateBoundingBox() override
  77. {
  78. if (Vertices->getCount()) {
  79. BoundingBox.reset(Vertices->getPosition(0));
  80. const irr::u32 vsize = Vertices->getCount();
  81. for (u32 i = 1; i < vsize; ++i)
  82. BoundingBox.addInternalPoint(Vertices->getPosition(i));
  83. } else
  84. BoundingBox.reset(0, 0, 0);
  85. }
  86. //! Append the vertices and indices to the current buffer
  87. void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override
  88. {
  89. if (vertices == getVertices())
  90. return;
  91. const u32 vertexCount = getVertexCount();
  92. const u32 indexCount = getIndexCount();
  93. auto *vt = static_cast<const T *>(vertices);
  94. Vertices->Data.insert(Vertices->Data.end(), vt, vt + numVertices);
  95. for (u32 i = vertexCount; i < getVertexCount(); i++)
  96. BoundingBox.addInternalPoint(Vertices->getPosition(i));
  97. Indices->Data.insert(Indices->Data.end(), indices, indices + numIndices);
  98. if (vertexCount != 0) {
  99. for (u32 i = indexCount; i < getIndexCount(); i++)
  100. Indices->Data[i] += vertexCount;
  101. }
  102. }
  103. //! Describe what kind of primitive geometry is used by the meshbuffer
  104. void setPrimitiveType(E_PRIMITIVE_TYPE type) override
  105. {
  106. PrimitiveType = type;
  107. }
  108. //! Get the kind of primitive geometry which is used by the meshbuffer
  109. E_PRIMITIVE_TYPE getPrimitiveType() const override
  110. {
  111. return PrimitiveType;
  112. }
  113. //! Material for this meshbuffer.
  114. video::SMaterial Material;
  115. //! Vertex buffer
  116. CVertexBuffer<T> *Vertices;
  117. //! Index buffer
  118. SIndexBuffer *Indices;
  119. //! Bounding box of this meshbuffer.
  120. core::aabbox3d<f32> BoundingBox;
  121. //! Primitive type used for rendering (triangles, lines, ...)
  122. E_PRIMITIVE_TYPE PrimitiveType;
  123. };
  124. //! Standard meshbuffer
  125. typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
  126. //! Meshbuffer with two texture coords per vertex, e.g. for lightmaps
  127. typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
  128. //! Meshbuffer with vertices having tangents stored, e.g. for normal mapping
  129. typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
  130. } // end namespace scene
  131. } // end namespace irr