SSkinMeshBuffer.h 5.7 KB

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