CCMesh.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 __CCMESH_H__
  21. #define __CCMESH_H__
  22. #include <string>
  23. #include <map>
  24. #include "3d/CCBundle3DData.h"
  25. #include "3d/CCAABB.h"
  26. #include "base/CCRef.h"
  27. #include "math/CCMath.h"
  28. #include "renderer/CCMeshCommand.h"
  29. NS_CC_BEGIN
  30. /**
  31. * @addtogroup _3d
  32. * @{
  33. */
  34. class Texture2D;
  35. class MeshSkin;
  36. class MeshIndexData;
  37. class GLProgramState;
  38. class GLProgram;
  39. class Material;
  40. class Renderer;
  41. class Scene;
  42. class Pass;
  43. /**
  44. * @brief Mesh: contains ref to index buffer, GLProgramState, texture, skin, blend function, aabb and so on
  45. */
  46. class CC_DLL Mesh : public Ref
  47. {
  48. friend class Sprite3D;
  49. public:
  50. typedef std::vector<unsigned short> IndexArray;
  51. /**create mesh from positions, normals, and so on, single SubMesh*/
  52. static Mesh* create(const std::vector<float>& positions, const std::vector<float>& normals, const std::vector<float>& texs, const IndexArray& indices);
  53. //高频操作时的创建方式,解决内存分配导致的卡顿问题
  54. static Mesh* create(std::vector<float>* outputVertices,const std::vector<float>& positions, const std::vector<float>& normals, const std::vector<float>& tangent, const std::vector<float>& texs, const IndexArray& indices,MeshData* reusedData);
  55. /**create mesh with vertex attributes*/
  56. CC_DEPRECATED_ATTRIBUTE static Mesh* create(const std::vector<float>& vertices, int perVertexSizeInFloat, const IndexArray& indices, int /*numIndex*/, const std::vector<MeshVertexAttrib>& attribs, int /*attribCount*/){ return create(vertices, perVertexSizeInFloat, indices, attribs); }
  57. /**
  58. * @lua NA
  59. */
  60. static Mesh* create(const std::vector<float>& vertices, int perVertexSizeInFloat, const IndexArray& indices, const std::vector<MeshVertexAttrib>& attribs);
  61. /**
  62. * @lua 缓存meshdata,解决高频操作时内存导致的卡顿问题
  63. */
  64. static Mesh* create(const std::vector<float>& vertices, int perVertexSizeInFloat, const IndexArray& indices, const std::vector<MeshVertexAttrib>& attribs,MeshData* reusedData);
  65. /**
  66. * create mesh
  67. * @lua NA
  68. */
  69. static Mesh* create(const std::string& name, MeshIndexData* indexData, MeshSkin* skin = nullptr);
  70. void updateMesh(std::vector<float>* outputVertices,const std::vector<float>& positions, const std::vector<float>& normals, const std::vector<float>& tangent, const std::vector<float>& texs, const IndexArray& indices);
  71. /**
  72. * get vertex buffer
  73. *
  74. * @lua NA
  75. */
  76. GLuint getVertexBuffer() const;
  77. /**
  78. * has vertex attribute?
  79. *
  80. * @lua NA
  81. */
  82. bool hasVertexAttrib(int attrib) const;
  83. /**get mesh vertex attribute count*/
  84. ssize_t getMeshVertexAttribCount() const;
  85. /**get MeshVertexAttribute by index*/
  86. const MeshVertexAttrib& getMeshVertexAttribute(int idx);
  87. /**get per vertex size in bytes*/
  88. int getVertexSizeInBytes() const;
  89. /**
  90. * set texture (diffuse), which is responsible for the main appearance. It is also means main texture, you can also call setTexture(texPath, NTextureData::Usage::Diffuse)
  91. * @param texPath texture path
  92. */
  93. void setTexture(const std::string& texPath);
  94. /**
  95. * set texture (diffuse), which is responsible for the main appearance. It is also means main texture, you can also call setTexture(texPath, NTextureData::Usage::Diffuse)
  96. * @param tex texture to be set
  97. */
  98. void setTexture(Texture2D* tex);
  99. /**
  100. * set texture
  101. * @param tex texture to be set
  102. * @param usage Usage of this texture
  103. * @param whether refresh the cache file name
  104. */
  105. void setTexture(Texture2D* tex, NTextureData::Usage usage,bool cacheFileName = true);
  106. /**
  107. * set texture
  108. * @param texPath texture path
  109. * @param usage Usage of this texture
  110. */
  111. void setTexture(const std::string& texPath, NTextureData::Usage usage);
  112. /**
  113. * Get texture (diffuse), which is responsible for the main appearance. It is also means main texture, you can also call getTexture(NTextureData::Usage::Diffuse)
  114. * @return Texture used, return the texture of first mesh if multiple meshes exist
  115. */
  116. Texture2D* getTexture() const;
  117. /**
  118. * Get texture
  119. * @param usage Usage of returned texture
  120. * @return The texture of this usage, return the texture of first mesh if multiple meshes exist
  121. */
  122. Texture2D* getTexture(NTextureData::Usage usage);
  123. /**visible getter and setter*/
  124. void setVisible(bool visible);
  125. bool isVisible() const;
  126. /**
  127. * skin getter
  128. *
  129. * @lua NA
  130. */
  131. MeshSkin* getSkin() const { return _skin; }
  132. /**
  133. * mesh index data getter
  134. *
  135. * @lua NA
  136. */
  137. MeshIndexData* getMeshIndexData() const { return _meshIndexData; }
  138. /**
  139. * get GLProgramState
  140. *
  141. * @lua NA
  142. */
  143. GLProgramState* getGLProgramState() const;
  144. /**name getter */
  145. const std::string& getName() const { return _name; }
  146. void setBlendFunc(const BlendFunc &blendFunc);
  147. const BlendFunc &getBlendFunc() const;
  148. /**
  149. * get primitive type
  150. *
  151. * @lua NA
  152. */
  153. GLenum getPrimitiveType() const;
  154. /**
  155. * get index count
  156. *
  157. * @lua NA
  158. */
  159. ssize_t getIndexCount() const;
  160. /**
  161. * get index format
  162. *
  163. * @lua NA
  164. */
  165. GLenum getIndexFormat() const;
  166. /**
  167. * get index buffer
  168. *
  169. * @lua NA
  170. */
  171. GLuint getIndexBuffer() const;
  172. /**get AABB*/
  173. const AABB& getAABB() const { return _aabb; }
  174. /** Sets a new GLProgramState for the Mesh
  175. * A new Material will be created for it
  176. */
  177. void setGLProgramState(GLProgramState* glProgramState);
  178. /** Sets a new Material to the Mesh */
  179. void setMaterial(Material* material);
  180. /** Returns the Material being used by the Mesh */
  181. Material* getMaterial() const;
  182. void draw(Renderer* renderer, float globalZ, const Mat4& transform, uint32_t flags, unsigned int lightMask, const Vec4& color, bool forceDepthWrite);
  183. /**
  184. * Get the MeshCommand.
  185. */
  186. MeshCommand& getMeshCommand() { return _meshCommand; }
  187. /**skin setter*/
  188. void setSkin(MeshSkin* skin);
  189. /**Mesh index data setter*/
  190. void setMeshIndexData(MeshIndexData* indexdata);
  191. /**name setter*/
  192. void setName(const std::string& name) { _name = name; }
  193. /**
  194. * calculate the AABB of the mesh
  195. * @note the AABB is in the local space, not the world space
  196. */
  197. void calculateAABB();
  198. /**
  199. * force set this Sprite3D to 2D render queue
  200. */
  201. void setForce2DQueue(bool force2D) { _force2DQueue = force2D; }
  202. std::string getTextureFileName(){ return _texFile; }
  203. CC_CONSTRUCTOR_ACCESS:
  204. Mesh();
  205. virtual ~Mesh();
  206. protected:
  207. void resetLightUniformValues();
  208. void setLightUniforms(Pass* pass, Scene* scene, const Vec4& color, unsigned int lightmask);
  209. void bindMeshCommand();
  210. std::map<NTextureData::Usage, Texture2D*> _textures; //textures that submesh is using
  211. MeshSkin* _skin; //skin
  212. bool _visible; // is the submesh visible
  213. bool _isTransparent; // is this mesh transparent, it is a property of material in fact
  214. bool _force2DQueue; // add this mesh to 2D render queue
  215. std::string _name;
  216. MeshCommand _meshCommand;
  217. MeshIndexData* _meshIndexData;
  218. GLProgramState* _glProgramState;
  219. BlendFunc _blend;
  220. bool _blendDirty;
  221. Material* _material;
  222. AABB _aabb;
  223. std::function<void()> _visibleChanged;
  224. ///light parameters
  225. std::vector<Vec3> _dirLightUniformColorValues;
  226. std::vector<Vec3> _dirLightUniformDirValues;
  227. std::vector<Vec3> _pointLightUniformColorValues;
  228. std::vector<Vec3> _pointLightUniformPositionValues;
  229. std::vector<float> _pointLightUniformRangeInverseValues;
  230. std::vector<Vec3> _spotLightUniformColorValues;
  231. std::vector<Vec3> _spotLightUniformPositionValues;
  232. std::vector<Vec3> _spotLightUniformDirValues;
  233. std::vector<float> _spotLightUniformInnerAngleCosValues;
  234. std::vector<float> _spotLightUniformOuterAngleCosValues;
  235. std::vector<float> _spotLightUniformRangeInverseValues;
  236. std::string _texFile;
  237. };
  238. // end of 3d group
  239. /// @}
  240. /// @cond
  241. extern std::string CC_DLL s_uniformSamplerName[];//uniform sampler names array
  242. /// @endcond
  243. NS_CC_END
  244. #endif // __CCMESH_H__