SSkinMeshBuffer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "IMeshBuffer.h"
  6. #include "CVertexBuffer.h"
  7. #include "CIndexBuffer.h"
  8. #include "S3DVertex.h"
  9. #include <cassert>
  10. namespace irr
  11. {
  12. namespace scene
  13. {
  14. //! A mesh buffer able to choose between S3DVertex2TCoords, S3DVertex and S3DVertexTangents at runtime
  15. struct SSkinMeshBuffer final : public IMeshBuffer
  16. {
  17. //! Default constructor
  18. SSkinMeshBuffer(video::E_VERTEX_TYPE vt = video::EVT_STANDARD) :
  19. VertexType(vt), PrimitiveType(EPT_TRIANGLES),
  20. BoundingBoxNeedsRecalculated(true)
  21. {
  22. Vertices_Tangents = new SVertexBufferTangents();
  23. Vertices_2TCoords = new SVertexBufferLightMap();
  24. Vertices_Standard = new SVertexBuffer();
  25. Indices = new SIndexBuffer();
  26. }
  27. //! Constructor for standard vertices
  28. SSkinMeshBuffer(std::vector<video::S3DVertex> &&vertices, std::vector<u16> &&indices) :
  29. SSkinMeshBuffer()
  30. {
  31. Vertices_Standard->Data = std::move(vertices);
  32. Indices->Data = std::move(indices);
  33. }
  34. ~SSkinMeshBuffer()
  35. {
  36. Vertices_Tangents->drop();
  37. Vertices_2TCoords->drop();
  38. Vertices_Standard->drop();
  39. Indices->drop();
  40. }
  41. //! Get Material of this buffer.
  42. const video::SMaterial &getMaterial() const override
  43. {
  44. return Material;
  45. }
  46. //! Get Material of this buffer.
  47. video::SMaterial &getMaterial() override
  48. {
  49. return Material;
  50. }
  51. const scene::IVertexBuffer *getVertexBuffer() const override
  52. {
  53. switch (VertexType) {
  54. case video::EVT_2TCOORDS:
  55. return Vertices_2TCoords;
  56. case video::EVT_TANGENTS:
  57. return Vertices_Tangents;
  58. default:
  59. return Vertices_Standard;
  60. }
  61. }
  62. scene::IVertexBuffer *getVertexBuffer() override
  63. {
  64. switch (VertexType) {
  65. case video::EVT_2TCOORDS:
  66. return Vertices_2TCoords;
  67. case video::EVT_TANGENTS:
  68. return Vertices_Tangents;
  69. default:
  70. return Vertices_Standard;
  71. }
  72. }
  73. const scene::IIndexBuffer *getIndexBuffer() const override
  74. {
  75. return Indices;
  76. }
  77. scene::IIndexBuffer *getIndexBuffer() override
  78. {
  79. return Indices;
  80. }
  81. //! Get standard vertex at given index
  82. virtual video::S3DVertex *getVertex(u32 index)
  83. {
  84. switch (VertexType) {
  85. case video::EVT_2TCOORDS:
  86. return &Vertices_2TCoords->Data[index];
  87. case video::EVT_TANGENTS:
  88. return &Vertices_Tangents->Data[index];
  89. default:
  90. return &Vertices_Standard->Data[index];
  91. }
  92. }
  93. //! Get bounding box
  94. const core::aabbox3d<f32> &getBoundingBox() const override
  95. {
  96. return BoundingBox;
  97. }
  98. //! Set bounding box
  99. void setBoundingBox(const core::aabbox3df &box) override
  100. {
  101. BoundingBox = box;
  102. }
  103. private:
  104. template <typename T> void recalculateBoundingBox(const CVertexBuffer<T> *buf)
  105. {
  106. if (!buf->getCount()) {
  107. BoundingBox.reset(0, 0, 0);
  108. } else {
  109. auto &vertices = buf->Data;
  110. BoundingBox.reset(vertices[0].Pos);
  111. for (size_t i = 1; i < vertices.size(); ++i)
  112. BoundingBox.addInternalPoint(vertices[i].Pos);
  113. }
  114. }
  115. template <typename T1, typename T2> static void copyVertex(const T1 &src, T2 &dst)
  116. {
  117. dst.Pos = src.Pos;
  118. dst.Normal = src.Normal;
  119. dst.Color = src.Color;
  120. dst.TCoords = src.TCoords;
  121. }
  122. public:
  123. //! Recalculate bounding box
  124. void recalculateBoundingBox() override
  125. {
  126. if (!BoundingBoxNeedsRecalculated)
  127. return;
  128. BoundingBoxNeedsRecalculated = false;
  129. switch (VertexType) {
  130. case video::EVT_STANDARD: {
  131. recalculateBoundingBox(Vertices_Standard);
  132. break;
  133. }
  134. case video::EVT_2TCOORDS: {
  135. recalculateBoundingBox(Vertices_2TCoords);
  136. break;
  137. }
  138. case video::EVT_TANGENTS: {
  139. recalculateBoundingBox(Vertices_Tangents);
  140. break;
  141. }
  142. }
  143. }
  144. //! Convert to 2tcoords vertex type
  145. void convertTo2TCoords()
  146. {
  147. if (VertexType == video::EVT_STANDARD) {
  148. video::S3DVertex2TCoords Vertex;
  149. for (const auto &Vertex_Standard : Vertices_Standard->Data) {
  150. copyVertex(Vertex_Standard, Vertex);
  151. Vertices_2TCoords->Data.push_back(Vertex);
  152. }
  153. Vertices_Standard->Data.clear();
  154. VertexType = video::EVT_2TCOORDS;
  155. }
  156. }
  157. //! Convert to tangents vertex type
  158. void convertToTangents()
  159. {
  160. if (VertexType == video::EVT_STANDARD) {
  161. video::S3DVertexTangents Vertex;
  162. for (const auto &Vertex_Standard : Vertices_Standard->Data) {
  163. copyVertex(Vertex_Standard, Vertex);
  164. Vertices_Tangents->Data.push_back(Vertex);
  165. }
  166. Vertices_Standard->Data.clear();
  167. VertexType = video::EVT_TANGENTS;
  168. } else if (VertexType == video::EVT_2TCOORDS) {
  169. video::S3DVertexTangents Vertex;
  170. for (const auto &Vertex_2TCoords : Vertices_2TCoords->Data) {
  171. copyVertex(Vertex_2TCoords, Vertex);
  172. Vertices_Tangents->Data.push_back(Vertex);
  173. }
  174. Vertices_2TCoords->Data.clear();
  175. VertexType = video::EVT_TANGENTS;
  176. }
  177. }
  178. //! append the vertices and indices to the current buffer
  179. void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override
  180. {
  181. assert(false);
  182. }
  183. //! Describe what kind of primitive geometry is used by the meshbuffer
  184. void setPrimitiveType(E_PRIMITIVE_TYPE type) override
  185. {
  186. PrimitiveType = type;
  187. }
  188. //! Get the kind of primitive geometry which is used by the meshbuffer
  189. E_PRIMITIVE_TYPE getPrimitiveType() const override
  190. {
  191. return PrimitiveType;
  192. }
  193. //! Call this after changing the positions of any vertex.
  194. void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; }
  195. SVertexBufferTangents *Vertices_Tangents;
  196. SVertexBufferLightMap *Vertices_2TCoords;
  197. SVertexBuffer *Vertices_Standard;
  198. SIndexBuffer *Indices;
  199. core::matrix4 Transformation;
  200. video::SMaterial Material;
  201. video::E_VERTEX_TYPE VertexType;
  202. core::aabbox3d<f32> BoundingBox{{0, 0, 0}};
  203. //! Primitive type used for rendering (triangles, lines, ...)
  204. E_PRIMITIVE_TYPE PrimitiveType;
  205. bool BoundingBoxNeedsRecalculated;
  206. };
  207. } // end namespace scene
  208. } // end namespace irr