SSkinMeshBuffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 S_SKIN_MESH_BUFFER_H_INCLUDED
  5. #define S_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. PrimitiveType(EPT_TRIANGLES),
  19. MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER),
  20. BoundingBoxNeedsRecalculated(true)
  21. {
  22. #ifdef _DEBUG
  23. setDebugName("SSkinMeshBuffer");
  24. #endif
  25. }
  26. //! Get Material of this buffer.
  27. virtual const video::SMaterial& getMaterial() const IRR_OVERRIDE
  28. {
  29. return Material;
  30. }
  31. //! Get Material of this buffer.
  32. virtual video::SMaterial& getMaterial() IRR_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. {
  41. case video::EVT_2TCOORDS:
  42. return (video::S3DVertex*)&Vertices_2TCoords[index];
  43. case video::EVT_TANGENTS:
  44. return (video::S3DVertex*)&Vertices_Tangents[index];
  45. default:
  46. return &Vertices_Standard[index];
  47. }
  48. }
  49. //! Get pointer to vertex array
  50. virtual const void* getVertices() const IRR_OVERRIDE
  51. {
  52. switch (VertexType)
  53. {
  54. case video::EVT_2TCOORDS:
  55. return Vertices_2TCoords.const_pointer();
  56. case video::EVT_TANGENTS:
  57. return Vertices_Tangents.const_pointer();
  58. default:
  59. return Vertices_Standard.const_pointer();
  60. }
  61. }
  62. //! Get pointer to vertex array
  63. virtual void* getVertices() IRR_OVERRIDE
  64. {
  65. switch (VertexType)
  66. {
  67. case video::EVT_2TCOORDS:
  68. return Vertices_2TCoords.pointer();
  69. case video::EVT_TANGENTS:
  70. return Vertices_Tangents.pointer();
  71. default:
  72. return Vertices_Standard.pointer();
  73. }
  74. }
  75. //! Get vertex count
  76. virtual u32 getVertexCount() const IRR_OVERRIDE
  77. {
  78. switch (VertexType)
  79. {
  80. case video::EVT_2TCOORDS:
  81. return Vertices_2TCoords.size();
  82. case video::EVT_TANGENTS:
  83. return Vertices_Tangents.size();
  84. default:
  85. return Vertices_Standard.size();
  86. }
  87. }
  88. //! Get type of index data which is stored in this meshbuffer.
  89. /** \return Index type of this buffer. */
  90. virtual video::E_INDEX_TYPE getIndexType() const IRR_OVERRIDE
  91. {
  92. return video::EIT_16BIT;
  93. }
  94. //! Get pointer to index array
  95. virtual const u16* getIndices() const IRR_OVERRIDE
  96. {
  97. return Indices.const_pointer();
  98. }
  99. //! Get pointer to index array
  100. virtual u16* getIndices() IRR_OVERRIDE
  101. {
  102. return Indices.pointer();
  103. }
  104. //! Get index count
  105. virtual u32 getIndexCount() const IRR_OVERRIDE
  106. {
  107. return Indices.size();
  108. }
  109. //! Get bounding box
  110. virtual const core::aabbox3d<f32>& getBoundingBox() const IRR_OVERRIDE
  111. {
  112. return BoundingBox;
  113. }
  114. //! Set bounding box
  115. virtual void setBoundingBox( const core::aabbox3df& box) IRR_OVERRIDE
  116. {
  117. BoundingBox = box;
  118. }
  119. //! Recalculate bounding box
  120. virtual void recalculateBoundingBox() IRR_OVERRIDE
  121. {
  122. if(!BoundingBoxNeedsRecalculated)
  123. return;
  124. BoundingBoxNeedsRecalculated = false;
  125. switch (VertexType)
  126. {
  127. case video::EVT_STANDARD:
  128. {
  129. if (Vertices_Standard.empty())
  130. BoundingBox.reset(0,0,0);
  131. else
  132. {
  133. BoundingBox.reset(Vertices_Standard[0].Pos);
  134. for (u32 i=1; i<Vertices_Standard.size(); ++i)
  135. BoundingBox.addInternalPoint(Vertices_Standard[i].Pos);
  136. }
  137. break;
  138. }
  139. case video::EVT_2TCOORDS:
  140. {
  141. if (Vertices_2TCoords.empty())
  142. BoundingBox.reset(0,0,0);
  143. else
  144. {
  145. BoundingBox.reset(Vertices_2TCoords[0].Pos);
  146. for (u32 i=1; i<Vertices_2TCoords.size(); ++i)
  147. BoundingBox.addInternalPoint(Vertices_2TCoords[i].Pos);
  148. }
  149. break;
  150. }
  151. case video::EVT_TANGENTS:
  152. {
  153. if (Vertices_Tangents.empty())
  154. BoundingBox.reset(0,0,0);
  155. else
  156. {
  157. BoundingBox.reset(Vertices_Tangents[0].Pos);
  158. for (u32 i=1; i<Vertices_Tangents.size(); ++i)
  159. BoundingBox.addInternalPoint(Vertices_Tangents[i].Pos);
  160. }
  161. break;
  162. }
  163. }
  164. }
  165. //! Get vertex type
  166. virtual video::E_VERTEX_TYPE getVertexType() const IRR_OVERRIDE
  167. {
  168. return VertexType;
  169. }
  170. //! Convert to 2tcoords vertex type
  171. void convertTo2TCoords()
  172. {
  173. if (VertexType==video::EVT_STANDARD)
  174. {
  175. for(u32 n=0;n<Vertices_Standard.size();++n)
  176. {
  177. video::S3DVertex2TCoords Vertex;
  178. Vertex.Color=Vertices_Standard[n].Color;
  179. Vertex.Pos=Vertices_Standard[n].Pos;
  180. Vertex.Normal=Vertices_Standard[n].Normal;
  181. Vertex.TCoords=Vertices_Standard[n].TCoords;
  182. Vertices_2TCoords.push_back(Vertex);
  183. }
  184. Vertices_Standard.clear();
  185. VertexType=video::EVT_2TCOORDS;
  186. }
  187. }
  188. //! Convert to tangents vertex type
  189. void convertToTangents()
  190. {
  191. if (VertexType==video::EVT_STANDARD)
  192. {
  193. for(u32 n=0;n<Vertices_Standard.size();++n)
  194. {
  195. video::S3DVertexTangents Vertex;
  196. Vertex.Color=Vertices_Standard[n].Color;
  197. Vertex.Pos=Vertices_Standard[n].Pos;
  198. Vertex.Normal=Vertices_Standard[n].Normal;
  199. Vertex.TCoords=Vertices_Standard[n].TCoords;
  200. Vertices_Tangents.push_back(Vertex);
  201. }
  202. Vertices_Standard.clear();
  203. VertexType=video::EVT_TANGENTS;
  204. }
  205. else if (VertexType==video::EVT_2TCOORDS)
  206. {
  207. for(u32 n=0;n<Vertices_2TCoords.size();++n)
  208. {
  209. video::S3DVertexTangents Vertex;
  210. Vertex.Color=Vertices_2TCoords[n].Color;
  211. Vertex.Pos=Vertices_2TCoords[n].Pos;
  212. Vertex.Normal=Vertices_2TCoords[n].Normal;
  213. Vertex.TCoords=Vertices_2TCoords[n].TCoords;
  214. Vertices_Tangents.push_back(Vertex);
  215. }
  216. Vertices_2TCoords.clear();
  217. VertexType=video::EVT_TANGENTS;
  218. }
  219. }
  220. //! returns position of vertex i
  221. virtual const core::vector3df& getPosition(u32 i) const IRR_OVERRIDE
  222. {
  223. switch (VertexType)
  224. {
  225. case video::EVT_2TCOORDS:
  226. return Vertices_2TCoords[i].Pos;
  227. case video::EVT_TANGENTS:
  228. return Vertices_Tangents[i].Pos;
  229. default:
  230. return Vertices_Standard[i].Pos;
  231. }
  232. }
  233. //! returns position of vertex i
  234. virtual core::vector3df& getPosition(u32 i) IRR_OVERRIDE
  235. {
  236. switch (VertexType)
  237. {
  238. case video::EVT_2TCOORDS:
  239. return Vertices_2TCoords[i].Pos;
  240. case video::EVT_TANGENTS:
  241. return Vertices_Tangents[i].Pos;
  242. default:
  243. return Vertices_Standard[i].Pos;
  244. }
  245. }
  246. //! returns normal of vertex i
  247. virtual const core::vector3df& getNormal(u32 i) const IRR_OVERRIDE
  248. {
  249. switch (VertexType)
  250. {
  251. case video::EVT_2TCOORDS:
  252. return Vertices_2TCoords[i].Normal;
  253. case video::EVT_TANGENTS:
  254. return Vertices_Tangents[i].Normal;
  255. default:
  256. return Vertices_Standard[i].Normal;
  257. }
  258. }
  259. //! returns normal of vertex i
  260. virtual core::vector3df& getNormal(u32 i) IRR_OVERRIDE
  261. {
  262. switch (VertexType)
  263. {
  264. case video::EVT_2TCOORDS:
  265. return Vertices_2TCoords[i].Normal;
  266. case video::EVT_TANGENTS:
  267. return Vertices_Tangents[i].Normal;
  268. default:
  269. return Vertices_Standard[i].Normal;
  270. }
  271. }
  272. //! returns texture coords of vertex i
  273. virtual const core::vector2df& getTCoords(u32 i) const IRR_OVERRIDE
  274. {
  275. switch (VertexType)
  276. {
  277. case video::EVT_2TCOORDS:
  278. return Vertices_2TCoords[i].TCoords;
  279. case video::EVT_TANGENTS:
  280. return Vertices_Tangents[i].TCoords;
  281. default:
  282. return Vertices_Standard[i].TCoords;
  283. }
  284. }
  285. //! returns texture coords of vertex i
  286. virtual core::vector2df& getTCoords(u32 i) IRR_OVERRIDE
  287. {
  288. switch (VertexType)
  289. {
  290. case video::EVT_2TCOORDS:
  291. return Vertices_2TCoords[i].TCoords;
  292. case video::EVT_TANGENTS:
  293. return Vertices_Tangents[i].TCoords;
  294. default:
  295. return Vertices_Standard[i].TCoords;
  296. }
  297. }
  298. //! returns color of vertex i
  299. virtual video::SColor& getColor(u32 i) IRR_OVERRIDE
  300. {
  301. switch (VertexType)
  302. {
  303. case video::EVT_2TCOORDS:
  304. return Vertices_2TCoords[i].Color;
  305. case video::EVT_TANGENTS:
  306. return Vertices_Tangents[i].Color;
  307. default:
  308. return Vertices_Standard[i].Color;
  309. }
  310. }
  311. //! returns color of vertex i
  312. virtual const video::SColor& getColor(u32 i) const IRR_OVERRIDE
  313. {
  314. switch (VertexType)
  315. {
  316. case video::EVT_2TCOORDS:
  317. return Vertices_2TCoords[i].Color;
  318. case video::EVT_TANGENTS:
  319. return Vertices_Tangents[i].Color;
  320. default:
  321. return Vertices_Standard[i].Color;
  322. }
  323. }
  324. //! append the vertices and indices to the current buffer
  325. virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices, bool updateBoundingBox) IRR_OVERRIDE {}
  326. //! append the meshbuffer to the current buffer
  327. virtual void append(const IMeshBuffer* const other, bool updateBoundingBox) IRR_OVERRIDE {}
  328. //! get the current hardware mapping hint for vertex buffers
  329. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const IRR_OVERRIDE
  330. {
  331. return MappingHint_Vertex;
  332. }
  333. //! get the current hardware mapping hint for index buffers
  334. virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const IRR_OVERRIDE
  335. {
  336. return MappingHint_Index;
  337. }
  338. //! set the hardware mapping hint, for driver
  339. virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX ) IRR_OVERRIDE
  340. {
  341. if (Buffer==EBT_VERTEX)
  342. MappingHint_Vertex=NewMappingHint;
  343. else if (Buffer==EBT_INDEX)
  344. MappingHint_Index=NewMappingHint;
  345. else if (Buffer==EBT_VERTEX_AND_INDEX)
  346. {
  347. MappingHint_Vertex=NewMappingHint;
  348. MappingHint_Index=NewMappingHint;
  349. }
  350. }
  351. //! Describe what kind of primitive geometry is used by the meshbuffer
  352. virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) IRR_OVERRIDE
  353. {
  354. PrimitiveType = type;
  355. }
  356. //! Get the kind of primitive geometry which is used by the meshbuffer
  357. virtual E_PRIMITIVE_TYPE getPrimitiveType() const IRR_OVERRIDE
  358. {
  359. return PrimitiveType;
  360. }
  361. //! flags the mesh as changed, reloads hardware buffers
  362. virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) IRR_OVERRIDE
  363. {
  364. if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
  365. ++ChangedID_Vertex;
  366. if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
  367. ++ChangedID_Index;
  368. }
  369. virtual u32 getChangedID_Vertex() const IRR_OVERRIDE {return ChangedID_Vertex;}
  370. virtual u32 getChangedID_Index() const IRR_OVERRIDE {return ChangedID_Index;}
  371. //! Returns type of the class implementing the IMeshBuffer
  372. virtual EMESH_BUFFER_TYPE getType() const IRR_OVERRIDE
  373. {
  374. return EMBT_SKIN;
  375. }
  376. //! Create copy of the meshbuffer
  377. virtual IMeshBuffer* createClone(int cloneFlags) const IRR_OVERRIDE
  378. {
  379. SSkinMeshBuffer* clone = new SSkinMeshBuffer(VertexType);
  380. if (cloneFlags & ECF_VERTICES)
  381. {
  382. clone->Vertices_Tangents = Vertices_Tangents;
  383. clone->Vertices_2TCoords = Vertices_2TCoords;
  384. clone->Vertices_Standard = Vertices_Standard;
  385. clone->BoundingBox = BoundingBox;
  386. clone->BoundingBoxNeedsRecalculated = BoundingBoxNeedsRecalculated;
  387. }
  388. if (cloneFlags & ECF_INDICES)
  389. {
  390. clone->Indices = Indices;
  391. }
  392. clone->Transformation = Transformation;
  393. clone->Material = getMaterial();
  394. clone->PrimitiveType = PrimitiveType;
  395. clone->MappingHint_Vertex = MappingHint_Vertex;
  396. clone->MappingHint_Index = MappingHint_Index;
  397. return clone;
  398. }
  399. //! Call this after changing the positions of any vertex.
  400. void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; }
  401. core::array<video::S3DVertexTangents> Vertices_Tangents;
  402. core::array<video::S3DVertex2TCoords> Vertices_2TCoords;
  403. core::array<video::S3DVertex> Vertices_Standard;
  404. core::array<u16> Indices;
  405. u32 ChangedID_Vertex;
  406. u32 ChangedID_Index;
  407. //ISkinnedMesh::SJoint *AttachedJoint;
  408. core::matrix4 Transformation;
  409. video::SMaterial Material;
  410. video::E_VERTEX_TYPE VertexType;
  411. core::aabbox3d<f32> BoundingBox;
  412. //! Primitive type used for rendering (triangles, lines, ...)
  413. E_PRIMITIVE_TYPE PrimitiveType;
  414. // hardware mapping hint
  415. E_HARDWARE_MAPPING MappingHint_Vertex:3;
  416. E_HARDWARE_MAPPING MappingHint_Index:3;
  417. bool BoundingBoxNeedsRecalculated:1;
  418. };
  419. } // end namespace scene
  420. } // end namespace irr
  421. #endif