CMeshBuffer.h 3.8 KB

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