1
0

CCBundle3DData.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __CC_BUNDLE_3D_DATA_H__
  21. #define __CC_BUNDLE_3D_DATA_H__
  22. #include "base/CCRef.h"
  23. #include "base/ccTypes.h"
  24. #include "math/CCMath.h"
  25. #include "3d/CCAABB.h"
  26. #include <vector>
  27. #include <map>
  28. NS_CC_BEGIN
  29. /**mesh vertex attribute
  30. * @js NA
  31. * @lua NA
  32. */
  33. struct MeshVertexAttrib
  34. {
  35. //attribute size
  36. GLint size;
  37. //GL_FLOAT
  38. GLenum type;
  39. //VERTEX_ATTRIB_POSITION,VERTEX_ATTRIB_COLOR,VERTEX_ATTRIB_TEX_COORD,VERTEX_ATTRIB_NORMAL, VERTEX_ATTRIB_BLEND_WEIGHT, VERTEX_ATTRIB_BLEND_INDEX, GLProgram for detail
  40. int vertexAttrib;
  41. //size in bytes
  42. int attribSizeBytes;
  43. };
  44. /** model node data, since 3.3
  45. * @js NA
  46. * @lua NA
  47. */
  48. struct ModelData
  49. {
  50. std::string subMeshId;
  51. std::string materialId;
  52. std::vector<std::string> bones;
  53. std::vector<Mat4> invBindPose;
  54. virtual ~ModelData()
  55. {
  56. resetData();
  57. }
  58. virtual void resetData()
  59. {
  60. bones.clear();
  61. invBindPose.clear();
  62. }
  63. };
  64. /** Node data, since 3.3
  65. * @js NA
  66. * @lua NA
  67. */
  68. struct NodeData
  69. {
  70. std::string id;
  71. Mat4 transform;
  72. std::vector<ModelData*> modelNodeDatas;
  73. std::vector<NodeData*> children;
  74. virtual ~NodeData()
  75. {
  76. resetData();
  77. }
  78. virtual void resetData()
  79. {
  80. id.clear();
  81. transform.setIdentity();
  82. for (auto& it : children)
  83. {
  84. delete it;
  85. }
  86. children.clear();
  87. for(auto& modeldata : modelNodeDatas)
  88. {
  89. delete modeldata;
  90. }
  91. modelNodeDatas.clear();
  92. }
  93. };
  94. /** node datas, since 3.3
  95. * @js NA
  96. * @lua NA
  97. */
  98. struct NodeDatas
  99. {
  100. std::vector<NodeData*> skeleton; //skeleton
  101. std::vector<NodeData*> nodes; // nodes, CCNode, Sprite3D or part of Sprite3D
  102. virtual ~NodeDatas()
  103. {
  104. resetData();
  105. }
  106. void resetData()
  107. {
  108. for(auto& it : skeleton)
  109. {
  110. delete it;
  111. }
  112. skeleton.clear();
  113. for(auto& it : nodes)
  114. {
  115. delete it;
  116. }
  117. nodes.clear();
  118. }
  119. };
  120. /**mesh data
  121. * @js NA
  122. * @lua NA
  123. */
  124. struct MeshData
  125. {
  126. typedef std::vector<unsigned short> IndexArray;
  127. std::vector<float> vertex;
  128. int vertexSizeInFloat;
  129. std::vector<IndexArray> subMeshIndices;
  130. std::vector<std::string> subMeshIds; //subMesh Names (since 3.3)
  131. std::vector<AABB> subMeshAABB;
  132. int numIndex;
  133. std::vector<MeshVertexAttrib> attribs;
  134. int attribCount;
  135. public:
  136. /**
  137. * Get per vertex size
  138. * @return return the sum of each vertex's all attribute size.
  139. */
  140. int getPerVertexSize() const
  141. {
  142. int vertexsize = 0;
  143. for(const auto& attrib : attribs)
  144. {
  145. vertexsize += attrib.attribSizeBytes;
  146. }
  147. return vertexsize;
  148. }
  149. /**
  150. * Reset the data
  151. */
  152. void resetData()
  153. {
  154. vertex.clear();
  155. subMeshIndices.clear();
  156. subMeshAABB.clear();
  157. attribs.clear();
  158. vertexSizeInFloat = 0;
  159. numIndex = 0;
  160. attribCount = 0;
  161. }
  162. MeshData()
  163. : vertexSizeInFloat(0)
  164. , numIndex(0)
  165. , attribCount(0)
  166. {
  167. }
  168. ~MeshData()
  169. {
  170. resetData();
  171. }
  172. };
  173. /** mesh datas
  174. * @js NA
  175. * @lua NA
  176. */
  177. struct MeshDatas
  178. {
  179. std::vector<MeshData*> meshDatas;
  180. void resetData()
  181. {
  182. for(auto& it : meshDatas)
  183. {
  184. delete it;
  185. }
  186. meshDatas.clear();
  187. }
  188. ~MeshDatas()
  189. {
  190. resetData();
  191. }
  192. };
  193. /**skin data
  194. * @js NA
  195. * @lua NA
  196. */
  197. struct SkinData
  198. {
  199. std::vector<std::string> skinBoneNames; //skin bones affect skin
  200. std::vector<std::string> nodeBoneNames; //node bones don't affect skin, all bones [skinBone, nodeBone]
  201. std::vector<Mat4> inverseBindPoseMatrices; //bind pose of skin bone, only for skin bone
  202. std::vector<Mat4> skinBoneOriginMatrices; // original bone transform, for skin bone
  203. std::vector<Mat4> nodeBoneOriginMatrices; // original bone transform, for node bone
  204. //bone child info, both skinbone and node bone
  205. std::map<int, std::vector<int> > boneChild;//key parent, value child
  206. int rootBoneIndex;
  207. void resetData()
  208. {
  209. skinBoneNames.clear();
  210. nodeBoneNames.clear();
  211. inverseBindPoseMatrices.clear();
  212. skinBoneOriginMatrices.clear();
  213. nodeBoneOriginMatrices.clear();
  214. boneChild.clear();
  215. rootBoneIndex = -1;
  216. }
  217. void addSkinBoneNames(const std::string& name)
  218. {
  219. auto it = std::find(skinBoneNames.begin(), skinBoneNames.end(), name);
  220. if (it == skinBoneNames.end())
  221. skinBoneNames.push_back(name);
  222. }
  223. void addNodeBoneNames(const std::string& name)
  224. {
  225. auto it = std::find(nodeBoneNames.begin(), nodeBoneNames.end(), name);
  226. if (it == nodeBoneNames.end())
  227. nodeBoneNames.push_back(name);
  228. }
  229. int getSkinBoneNameIndex(const std::string& name)const
  230. {
  231. int i = 0;
  232. for (const auto& iter : skinBoneNames)
  233. {
  234. if ((iter) == name)
  235. return i;
  236. i++;
  237. }
  238. return -1;
  239. }
  240. int getBoneNameIndex(const std::string& name)const
  241. {
  242. int i = 0;
  243. for (const auto& iter : skinBoneNames)
  244. {
  245. if ((iter) == name)
  246. return i;
  247. i++;
  248. }
  249. for(const auto& iter : nodeBoneNames)
  250. {
  251. if (iter == name)
  252. return i;
  253. i++;
  254. }
  255. return -1;
  256. }
  257. };
  258. /**material data,
  259. * @js NA
  260. * @lua NA
  261. */
  262. struct MaterialData
  263. {
  264. std::map<int, std::string> texturePaths; //submesh id, texture path
  265. void resetData()
  266. {
  267. texturePaths.clear();
  268. }
  269. };
  270. /**new material, since 3.3
  271. * @js NA
  272. * @lua NA
  273. */
  274. struct NTextureData
  275. {
  276. enum class Usage {
  277. Unknown = 0,
  278. None = 1,
  279. Diffuse = 2,
  280. Emissive = 3,
  281. Ambient = 4,
  282. Specular = 5,
  283. Shininess = 6,
  284. Normal = 7,
  285. Bump = 8,
  286. Transparency = 9,
  287. Reflection = 10
  288. };
  289. std::string id;
  290. std::string filename;
  291. Usage type;
  292. GLenum wrapS;
  293. GLenum wrapT;
  294. } ;
  295. struct NMaterialData
  296. {
  297. std::vector<NTextureData> textures;
  298. std::string id;
  299. const NTextureData* getTextureData(const NTextureData::Usage& type) const
  300. {
  301. for(const auto& it : textures)
  302. {
  303. if (it.type == type)
  304. return &it;
  305. }
  306. return nullptr;
  307. }
  308. };
  309. /** material datas, since 3.3
  310. * @js NA
  311. * @lua NA
  312. */
  313. struct MaterialDatas
  314. {
  315. std::vector<NMaterialData> materials;
  316. void resetData()
  317. {
  318. materials.clear();
  319. }
  320. const NMaterialData* getMaterialData(const std::string& materialid) const
  321. {
  322. for(const auto& it : materials)
  323. {
  324. if (it.id == materialid)
  325. return &it;
  326. }
  327. return nullptr;
  328. }
  329. };
  330. /**animation data
  331. * @js NA
  332. * @lua NA
  333. */
  334. struct Animation3DData
  335. {
  336. public:
  337. struct Vec3Key
  338. {
  339. Vec3Key()
  340. : _time(0)
  341. {
  342. }
  343. Vec3Key(float time, const Vec3& v)
  344. : _time(time)
  345. , _key(v)
  346. {
  347. }
  348. float _time;
  349. Vec3 _key;
  350. };
  351. struct QuatKey
  352. {
  353. QuatKey()
  354. : _time(0)
  355. , _key(Quaternion::identity())
  356. {
  357. }
  358. QuatKey(float time, const Quaternion& quat)
  359. : _time(time)
  360. , _key(quat)
  361. {
  362. }
  363. float _time;
  364. Quaternion _key;
  365. };
  366. public:
  367. std::map<std::string, std::vector<Vec3Key>> _translationKeys;
  368. std::map<std::string, std::vector<QuatKey>> _rotationKeys;
  369. std::map<std::string, std::vector<Vec3Key>> _scaleKeys;
  370. float _totalTime;
  371. public:
  372. Animation3DData()
  373. :_totalTime(0)
  374. {
  375. }
  376. Animation3DData(const Animation3DData& other)
  377. : _translationKeys(other._translationKeys)
  378. , _rotationKeys(other._rotationKeys)
  379. , _scaleKeys(other._scaleKeys)
  380. , _totalTime(other._totalTime)
  381. {
  382. }
  383. void resetData()
  384. {
  385. _totalTime = 0;
  386. _translationKeys.clear();
  387. _rotationKeys.clear();
  388. _scaleKeys.clear();
  389. }
  390. };
  391. /**reference data
  392. * @js NA
  393. * @lua NA
  394. */
  395. struct Reference
  396. {
  397. public:
  398. std::string id;
  399. unsigned int type;
  400. unsigned int offset;
  401. Reference(){}
  402. ~Reference(){}
  403. };
  404. NS_CC_END
  405. #endif //__CC_BUNDLE_3D_DATA_H__