CCMesh.h 8.7 KB

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