1
0

CCBundle3D.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 __CCBUNDLE3D_H__
  21. #define __CCBUNDLE3D_H__
  22. #include "base/CCData.h"
  23. #include "3d/CCBundle3DData.h"
  24. #include "3d/CCBundleReader.h"
  25. #include "json/document-wrapper.h"
  26. NS_CC_BEGIN
  27. /**
  28. * @addtogroup _3d
  29. * @{
  30. */
  31. class Animation3D;
  32. /**
  33. * @brief Defines a bundle file that contains a collection of assets. Mesh, Material, MeshSkin, Animation
  34. * There are two types of bundle files, c3t and c3b.
  35. * c3t text file
  36. * c3b binary file
  37. * @js NA
  38. * @lua NA
  39. */
  40. class CC_DLL Bundle3D
  41. {
  42. public:
  43. /**
  44. * create a new bundle, destroy it when finish using it
  45. */
  46. static Bundle3D* createBundle();
  47. static void destroyBundle(Bundle3D* bundle);
  48. virtual void clear();
  49. /**
  50. * load a file. You must load a file first, then call loadMeshData, loadSkinData, and so on
  51. * @param path File to be loaded
  52. * @return result of load
  53. */
  54. virtual bool load(const std::string& path);
  55. /**
  56. * load skin data from bundle
  57. * @param id The ID of the skin, load the first Skin in the bundle if it is empty
  58. */
  59. virtual bool loadSkinData(const std::string& id, SkinData* skindata);
  60. /**
  61. * load material data from bundle
  62. * @param id The ID of the animation, load the first animation in the bundle if it is empty
  63. */
  64. virtual bool loadAnimationData(const std::string& id, Animation3DData* animationdata);
  65. //since 3.3, to support reskin
  66. virtual bool loadMeshDatas(MeshDatas& meshdatas);
  67. //since 3.3, to support reskin
  68. virtual bool loadNodes(NodeDatas& nodedatas);
  69. //since 3.3, to support reskin
  70. virtual bool loadMaterials(MaterialDatas& materialdatas);
  71. /**
  72. * load triangle list
  73. * @param path the file path to load
  74. */
  75. static std::vector<Vec3> getTrianglesList(const std::string& path);
  76. //load .obj file
  77. static bool loadObj(MeshDatas& meshdatas, MaterialDatas& materialdatas, NodeDatas& nodedatas, const std::string& fullPath, const char* mtl_basepath = nullptr);
  78. //calculate aabb
  79. static AABB calculateAABB(const std::vector<float>& vertex, int stride, const std::vector<unsigned short>& index);
  80. protected:
  81. bool loadJson(const std::string& path);
  82. bool loadBinary(const std::string& path);
  83. bool loadMeshDatasJson(MeshDatas& meshdatas);
  84. bool loadMeshDataJson_0_1(MeshDatas& meshdatas);
  85. bool loadMeshDataJson_0_2(MeshDatas& meshdatas);
  86. bool loadMeshDatasBinary(MeshDatas& meshdatas);
  87. bool loadMeshDatasBinary_0_1(MeshDatas& meshdatas);
  88. bool loadMeshDatasBinary_0_2(MeshDatas& meshdatas);
  89. bool loadMaterialsJson(MaterialDatas& materialdatas);
  90. bool loadMaterialDataJson_0_1(MaterialDatas& materialdatas);
  91. bool loadMaterialDataJson_0_2(MaterialDatas& materialdatas);
  92. bool loadMaterialsBinary(MaterialDatas& materialdatas);
  93. bool loadMaterialsBinary_0_1(MaterialDatas& materialdatas);
  94. bool loadMaterialsBinary_0_2(MaterialDatas& materialdatas);
  95. bool loadMeshDataJson(MeshData* meshdata);
  96. bool loadMeshDataJson_0_1(MeshData* meshdata);
  97. bool loadMeshDataJson_0_2(MeshData* meshdata);
  98. bool loadSkinDataJson(SkinData* skindata);
  99. bool loadSkinDataBinary(SkinData* skindata);
  100. bool loadMaterialDataJson(MaterialData* materialdata);
  101. bool loadMaterialDataJson_0_1(MaterialData* materialdata);
  102. bool loadMaterialDataJson_0_2(MaterialData* materialdata);
  103. bool loadAnimationDataJson(const std::string& id,Animation3DData* animationdata);
  104. bool loadAnimationDataBinary(const std::string& id,Animation3DData* animationdata);
  105. /**
  106. * load nodes of json
  107. */
  108. bool loadNodesJson(NodeDatas& nodedatas);
  109. NodeData* parseNodesRecursivelyJson(const rapidjson::Value& jvalue, bool singleSprite);
  110. /**
  111. * load nodes of binary
  112. */
  113. bool loadNodesBinary(NodeDatas& nodedatas);
  114. NodeData* parseNodesRecursivelyBinary(bool& skeleton, bool singleSprite);
  115. /**
  116. * get define data type
  117. * @param str The type in string
  118. */
  119. GLenum parseGLType(const std::string& str);
  120. /**
  121. * get define data type
  122. * @param str The type in string
  123. */
  124. NTextureData::Usage parseGLTextureType(const std::string& str);
  125. /**
  126. * get vertex attribute type
  127. * @param str The type in string
  128. */
  129. unsigned int parseGLProgramAttribute(const std::string& str);
  130. /*
  131. * get model path
  132. * @param str Full path of model file
  133. */
  134. void getModelRelativePath(const std::string& path);
  135. /*
  136. * set the read position in buffer to the target type
  137. * @param The data type
  138. * @param The data id
  139. */
  140. Reference* seekToFirstType(unsigned int type, const std::string& id = "");
  141. CC_CONSTRUCTOR_ACCESS:
  142. Bundle3D();
  143. virtual ~Bundle3D();
  144. protected:
  145. std::string _modelPath;
  146. std::string _path;
  147. std::string _version;// the c3b or c3t version
  148. // for json reading
  149. std::string _jsonBuffer;
  150. rapidjson::Document _jsonReader;
  151. // for binary reading
  152. Data _binaryBuffer;
  153. BundleReader _binaryReader;
  154. unsigned int _referenceCount;
  155. Reference* _references;
  156. bool _isBinary;
  157. };
  158. // end of 3d group
  159. /// @}
  160. NS_CC_END
  161. #endif // __CCBUNDLE3D_H__