SSkinMeshBuffer.h 9.5 KB

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