SSkinMeshBuffer.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 "S3DVertex.h"
  7. namespace irr
  8. {
  9. namespace scene
  10. {
  11. //! A mesh buffer able to choose between S3DVertex2TCoords, S3DVertex and S3DVertexTangents at runtime
  12. struct SSkinMeshBuffer : public IMeshBuffer
  13. {
  14. //! Default constructor
  15. SSkinMeshBuffer(video::E_VERTEX_TYPE vt = video::EVT_STANDARD) :
  16. ChangedID_Vertex(1), ChangedID_Index(1), VertexType(vt),
  17. PrimitiveType(EPT_TRIANGLES),
  18. MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER),
  19. HWBuffer(NULL),
  20. BoundingBoxNeedsRecalculated(true)
  21. {
  22. #ifdef _DEBUG
  23. setDebugName("SSkinMeshBuffer");
  24. #endif
  25. }
  26. //! Get Material of this buffer.
  27. const video::SMaterial &getMaterial() const override
  28. {
  29. return Material;
  30. }
  31. //! Get Material of this buffer.
  32. video::SMaterial &getMaterial() override
  33. {
  34. return Material;
  35. }
  36. //! Get standard vertex at given index
  37. virtual video::S3DVertex *getVertex(u32 index)
  38. {
  39. switch (VertexType) {
  40. case video::EVT_2TCOORDS:
  41. return (video::S3DVertex *)&Vertices_2TCoords[index];
  42. case video::EVT_TANGENTS:
  43. return (video::S3DVertex *)&Vertices_Tangents[index];
  44. default:
  45. return &Vertices_Standard[index];
  46. }
  47. }
  48. //! Get pointer to vertex array
  49. const void *getVertices() const override
  50. {
  51. switch (VertexType) {
  52. case video::EVT_2TCOORDS:
  53. return Vertices_2TCoords.const_pointer();
  54. case video::EVT_TANGENTS:
  55. return Vertices_Tangents.const_pointer();
  56. default:
  57. return Vertices_Standard.const_pointer();
  58. }
  59. }
  60. //! Get pointer to vertex array
  61. void *getVertices() override
  62. {
  63. switch (VertexType) {
  64. case video::EVT_2TCOORDS:
  65. return Vertices_2TCoords.pointer();
  66. case video::EVT_TANGENTS:
  67. return Vertices_Tangents.pointer();
  68. default:
  69. return Vertices_Standard.pointer();
  70. }
  71. }
  72. //! Get vertex count
  73. u32 getVertexCount() const override
  74. {
  75. switch (VertexType) {
  76. case video::EVT_2TCOORDS:
  77. return Vertices_2TCoords.size();
  78. case video::EVT_TANGENTS:
  79. return Vertices_Tangents.size();
  80. default:
  81. return Vertices_Standard.size();
  82. }
  83. }
  84. //! Get type of index data which is stored in this meshbuffer.
  85. /** \return Index type of this buffer. */
  86. video::E_INDEX_TYPE getIndexType() const override
  87. {
  88. return video::EIT_16BIT;
  89. }
  90. //! Get pointer to index array
  91. const u16 *getIndices() const override
  92. {
  93. return Indices.const_pointer();
  94. }
  95. //! Get pointer to index array
  96. u16 *getIndices() override
  97. {
  98. return Indices.pointer();
  99. }
  100. //! Get index count
  101. u32 getIndexCount() const override
  102. {
  103. return Indices.size();
  104. }
  105. //! Get bounding box
  106. const core::aabbox3d<f32> &getBoundingBox() const override
  107. {
  108. return BoundingBox;
  109. }
  110. //! Set bounding box
  111. void setBoundingBox(const core::aabbox3df &box) override
  112. {
  113. BoundingBox = box;
  114. }
  115. //! Recalculate bounding box
  116. void recalculateBoundingBox() override
  117. {
  118. if (!BoundingBoxNeedsRecalculated)
  119. return;
  120. BoundingBoxNeedsRecalculated = false;
  121. switch (VertexType) {
  122. case video::EVT_STANDARD: {
  123. if (Vertices_Standard.empty())
  124. BoundingBox.reset(0, 0, 0);
  125. else {
  126. BoundingBox.reset(Vertices_Standard[0].Pos);
  127. for (u32 i = 1; i < Vertices_Standard.size(); ++i)
  128. BoundingBox.addInternalPoint(Vertices_Standard[i].Pos);
  129. }
  130. break;
  131. }
  132. case video::EVT_2TCOORDS: {
  133. if (Vertices_2TCoords.empty())
  134. BoundingBox.reset(0, 0, 0);
  135. else {
  136. BoundingBox.reset(Vertices_2TCoords[0].Pos);
  137. for (u32 i = 1; i < Vertices_2TCoords.size(); ++i)
  138. BoundingBox.addInternalPoint(Vertices_2TCoords[i].Pos);
  139. }
  140. break;
  141. }
  142. case video::EVT_TANGENTS: {
  143. if (Vertices_Tangents.empty())
  144. BoundingBox.reset(0, 0, 0);
  145. else {
  146. BoundingBox.reset(Vertices_Tangents[0].Pos);
  147. for (u32 i = 1; i < Vertices_Tangents.size(); ++i)
  148. BoundingBox.addInternalPoint(Vertices_Tangents[i].Pos);
  149. }
  150. break;
  151. }
  152. }
  153. }
  154. //! Get vertex type
  155. video::E_VERTEX_TYPE getVertexType() const override
  156. {
  157. return VertexType;
  158. }
  159. //! Convert to 2tcoords vertex type
  160. void convertTo2TCoords()
  161. {
  162. if (VertexType == video::EVT_STANDARD) {
  163. for (u32 n = 0; n < Vertices_Standard.size(); ++n) {
  164. video::S3DVertex2TCoords Vertex;
  165. Vertex.Color = Vertices_Standard[n].Color;
  166. Vertex.Pos = Vertices_Standard[n].Pos;
  167. Vertex.Normal = Vertices_Standard[n].Normal;
  168. Vertex.TCoords = Vertices_Standard[n].TCoords;
  169. Vertices_2TCoords.push_back(Vertex);
  170. }
  171. Vertices_Standard.clear();
  172. VertexType = video::EVT_2TCOORDS;
  173. }
  174. }
  175. //! Convert to tangents vertex type
  176. void convertToTangents()
  177. {
  178. if (VertexType == video::EVT_STANDARD) {
  179. for (u32 n = 0; n < Vertices_Standard.size(); ++n) {
  180. video::S3DVertexTangents Vertex;
  181. Vertex.Color = Vertices_Standard[n].Color;
  182. Vertex.Pos = Vertices_Standard[n].Pos;
  183. Vertex.Normal = Vertices_Standard[n].Normal;
  184. Vertex.TCoords = Vertices_Standard[n].TCoords;
  185. Vertices_Tangents.push_back(Vertex);
  186. }
  187. Vertices_Standard.clear();
  188. VertexType = video::EVT_TANGENTS;
  189. } else if (VertexType == video::EVT_2TCOORDS) {
  190. for (u32 n = 0; n < Vertices_2TCoords.size(); ++n) {
  191. video::S3DVertexTangents Vertex;
  192. Vertex.Color = Vertices_2TCoords[n].Color;
  193. Vertex.Pos = Vertices_2TCoords[n].Pos;
  194. Vertex.Normal = Vertices_2TCoords[n].Normal;
  195. Vertex.TCoords = Vertices_2TCoords[n].TCoords;
  196. Vertices_Tangents.push_back(Vertex);
  197. }
  198. Vertices_2TCoords.clear();
  199. VertexType = video::EVT_TANGENTS;
  200. }
  201. }
  202. //! returns position of vertex i
  203. const core::vector3df &getPosition(u32 i) const override
  204. {
  205. switch (VertexType) {
  206. case video::EVT_2TCOORDS:
  207. return Vertices_2TCoords[i].Pos;
  208. case video::EVT_TANGENTS:
  209. return Vertices_Tangents[i].Pos;
  210. default:
  211. return Vertices_Standard[i].Pos;
  212. }
  213. }
  214. //! returns position of vertex i
  215. core::vector3df &getPosition(u32 i) override
  216. {
  217. switch (VertexType) {
  218. case video::EVT_2TCOORDS:
  219. return Vertices_2TCoords[i].Pos;
  220. case video::EVT_TANGENTS:
  221. return Vertices_Tangents[i].Pos;
  222. default:
  223. return Vertices_Standard[i].Pos;
  224. }
  225. }
  226. //! returns normal of vertex i
  227. const core::vector3df &getNormal(u32 i) const override
  228. {
  229. switch (VertexType) {
  230. case video::EVT_2TCOORDS:
  231. return Vertices_2TCoords[i].Normal;
  232. case video::EVT_TANGENTS:
  233. return Vertices_Tangents[i].Normal;
  234. default:
  235. return Vertices_Standard[i].Normal;
  236. }
  237. }
  238. //! returns normal of vertex i
  239. core::vector3df &getNormal(u32 i) override
  240. {
  241. switch (VertexType) {
  242. case video::EVT_2TCOORDS:
  243. return Vertices_2TCoords[i].Normal;
  244. case video::EVT_TANGENTS:
  245. return Vertices_Tangents[i].Normal;
  246. default:
  247. return Vertices_Standard[i].Normal;
  248. }
  249. }
  250. //! returns texture coords of vertex i
  251. const core::vector2df &getTCoords(u32 i) const override
  252. {
  253. switch (VertexType) {
  254. case video::EVT_2TCOORDS:
  255. return Vertices_2TCoords[i].TCoords;
  256. case video::EVT_TANGENTS:
  257. return Vertices_Tangents[i].TCoords;
  258. default:
  259. return Vertices_Standard[i].TCoords;
  260. }
  261. }
  262. //! returns texture coords of vertex i
  263. core::vector2df &getTCoords(u32 i) override
  264. {
  265. switch (VertexType) {
  266. case video::EVT_2TCOORDS:
  267. return Vertices_2TCoords[i].TCoords;
  268. case video::EVT_TANGENTS:
  269. return Vertices_Tangents[i].TCoords;
  270. default:
  271. return Vertices_Standard[i].TCoords;
  272. }
  273. }
  274. //! append the vertices and indices to the current buffer
  275. void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override {}
  276. //! get the current hardware mapping hint for vertex buffers
  277. E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const override
  278. {
  279. return MappingHint_Vertex;
  280. }
  281. //! get the current hardware mapping hint for index buffers
  282. E_HARDWARE_MAPPING getHardwareMappingHint_Index() const override
  283. {
  284. return MappingHint_Index;
  285. }
  286. //! set the hardware mapping hint, for driver
  287. void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer = EBT_VERTEX_AND_INDEX) override
  288. {
  289. if (Buffer == EBT_VERTEX)
  290. MappingHint_Vertex = NewMappingHint;
  291. else if (Buffer == EBT_INDEX)
  292. MappingHint_Index = NewMappingHint;
  293. else if (Buffer == EBT_VERTEX_AND_INDEX) {
  294. MappingHint_Vertex = NewMappingHint;
  295. MappingHint_Index = NewMappingHint;
  296. }
  297. }
  298. //! Describe what kind of primitive geometry is used by the meshbuffer
  299. void setPrimitiveType(E_PRIMITIVE_TYPE type) override
  300. {
  301. PrimitiveType = type;
  302. }
  303. //! Get the kind of primitive geometry which is used by the meshbuffer
  304. E_PRIMITIVE_TYPE getPrimitiveType() const override
  305. {
  306. return PrimitiveType;
  307. }
  308. //! flags the mesh as changed, reloads hardware buffers
  309. void setDirty(E_BUFFER_TYPE Buffer = EBT_VERTEX_AND_INDEX) override
  310. {
  311. if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
  312. ++ChangedID_Vertex;
  313. if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
  314. ++ChangedID_Index;
  315. }
  316. u32 getChangedID_Vertex() const override { return ChangedID_Vertex; }
  317. u32 getChangedID_Index() const override { return ChangedID_Index; }
  318. void setHWBuffer(void *ptr) const override
  319. {
  320. HWBuffer = ptr;
  321. }
  322. void *getHWBuffer() const override
  323. {
  324. return HWBuffer;
  325. }
  326. //! Call this after changing the positions of any vertex.
  327. void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; }
  328. core::array<video::S3DVertexTangents> Vertices_Tangents;
  329. core::array<video::S3DVertex2TCoords> Vertices_2TCoords;
  330. core::array<video::S3DVertex> Vertices_Standard;
  331. core::array<u16> Indices;
  332. u32 ChangedID_Vertex;
  333. u32 ChangedID_Index;
  334. // ISkinnedMesh::SJoint *AttachedJoint;
  335. core::matrix4 Transformation;
  336. video::SMaterial Material;
  337. video::E_VERTEX_TYPE VertexType;
  338. core::aabbox3d<f32> BoundingBox;
  339. //! Primitive type used for rendering (triangles, lines, ...)
  340. E_PRIMITIVE_TYPE PrimitiveType;
  341. // hardware mapping hint
  342. E_HARDWARE_MAPPING MappingHint_Vertex : 3;
  343. E_HARDWARE_MAPPING MappingHint_Index : 3;
  344. mutable void *HWBuffer;
  345. bool BoundingBoxNeedsRecalculated : 1;
  346. };
  347. } // end namespace scene
  348. } // end namespace irr