SSkinMeshBuffer.h 5.8 KB

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