CCVertexIndexBuffer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CC_VERTEX_INDEX_BUFFER_H__
  22. #define __CC_VERTEX_INDEX_BUFFER_H__
  23. #include <vector>
  24. #include "base/CCRef.h"
  25. #include "platform/CCGL.h"
  26. /**
  27. * @addtogroup renderer
  28. * @{
  29. */
  30. NS_CC_BEGIN
  31. class EventListenerCustom;
  32. /**
  33. VertexBuffer is an abstraction of low level openGL Vertex Buffer Object.
  34. It is used to save an array of vertices.
  35. *@js NA
  36. */
  37. class CC_DLL VertexBuffer : public Ref
  38. {
  39. public:
  40. /**
  41. Create an instance of VertexBuffer.
  42. @param sizePerVertex Size in bytes of one vertex.
  43. @param vertexNumber The number of vertex.
  44. @param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
  45. */
  46. static VertexBuffer* create(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
  47. /**Get the size in bytes of one vertex.*/
  48. int getSizePerVertex() const;
  49. /**Get the number of vertices.*/
  50. int getVertexNumber() const;
  51. /**
  52. Update all or part of vertices data, if the range specified exceeds the vertex buffer, it will be clipped.
  53. @param verts The pointer of the vertex data.
  54. @param count The number of vertices to update.
  55. @param begin The first vertex to update.
  56. */
  57. bool updateVertices(const void* verts, int count, int begin);
  58. /**
  59. Get the size of the vertex array in bytes, equals getSizePerVertex() * getVertexNumber().
  60. */
  61. int getSize() const;
  62. /**
  63. Get the internal openGL handle.
  64. */
  65. GLuint getVBO() const;
  66. protected:
  67. /**
  68. Constructor.
  69. */
  70. VertexBuffer();
  71. /**
  72. Destructor.
  73. */
  74. virtual ~VertexBuffer();
  75. /**
  76. Init the storage of vertex buffer.
  77. @param sizePerVertex Size in bytes of one vertex.
  78. @param vertexNumber The number of vertex.
  79. @param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
  80. */
  81. bool init(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
  82. protected:
  83. /**
  84. Event handler for foreground.
  85. */
  86. void recreateVBO() const;
  87. /**
  88. Event listener for foreground.
  89. */
  90. EventListenerCustom* _recreateVBOEventListener;
  91. protected:
  92. /**
  93. Internal handle for openGL.
  94. */
  95. mutable GLuint _vbo;
  96. /**
  97. Size in bytes for one vertex.
  98. */
  99. int _sizePerVertex;
  100. /**
  101. Number of vertices.
  102. */
  103. int _vertexNumber;
  104. /**
  105. Buffer used for shadow copy.
  106. */
  107. std::vector<unsigned char> _shadowCopy;
  108. /**
  109. Hint for optimisation in GL.
  110. */
  111. GLenum _usage;
  112. protected:
  113. /**
  114. Static member to indicate that use _shadowCopy or not.
  115. */
  116. static bool _enableShadowCopy;
  117. public:
  118. /**
  119. Static getter for shadowCopy.
  120. */
  121. static bool isShadowCopyEnabled() { return _enableShadowCopy; }
  122. /**
  123. Static setter for shadowCopy.
  124. */
  125. static void enableShadowCopy(bool enabled) { _enableShadowCopy = enabled; }
  126. };
  127. /**
  128. IndexBuffer is an abstraction of low level openGL Buffer Object.
  129. It used to save an array of indices.
  130. @js NA
  131. */
  132. class CC_DLL IndexBuffer : public Ref
  133. {
  134. public:
  135. /**
  136. Enum for the type of index, short indices and int indices could be used.
  137. */
  138. enum class IndexType
  139. {
  140. /**Short index will be used.*/
  141. INDEX_TYPE_SHORT_16,
  142. /**Int index will be used.*/
  143. INDEX_TYPE_UINT_32
  144. };
  145. public:
  146. /**
  147. Create an instance of IndexBuffer.
  148. @param type type of index.
  149. @param number The number of indices.
  150. @param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
  151. */
  152. static IndexBuffer* create(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
  153. /**
  154. Getter for type of indices.
  155. */
  156. IndexType getType() const;
  157. /**
  158. Get the size in bytes for one index, will be 2 for INDEX_TYPE_SHORT_16 and 4 for INDEX_TYPE_UINT_32.
  159. */
  160. int getSizePerIndex() const;
  161. /**
  162. Get the number of indices.
  163. */
  164. int getIndexNumber() const;
  165. /**
  166. Update all or part of indices data, if the range specified exceeds the vertex buffer, it will be clipped.
  167. @param indices The pointer of the index data.
  168. @param count The number of indices to update.
  169. @param begin The start index to update.
  170. */
  171. bool updateIndices(const void* indices, int count, int begin);
  172. /**
  173. Get the size in bytes of the array of indices.
  174. */
  175. int getSize() const;
  176. /**
  177. Get the openGL handle for index buffer.
  178. */
  179. GLuint getVBO() const;
  180. protected:
  181. /**
  182. Constructor.
  183. */
  184. IndexBuffer();
  185. /**
  186. Destructor.
  187. */
  188. virtual ~IndexBuffer();
  189. /**
  190. Init the storageof IndexBuffer.
  191. @param type type of index.
  192. @param number The number of indices.
  193. @param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
  194. */
  195. bool init(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
  196. protected:
  197. /**
  198. Handle for openGL.
  199. */
  200. mutable GLuint _vbo;
  201. /**
  202. Type for index.
  203. */
  204. IndexType _type;
  205. /**
  206. Number of indices.
  207. */
  208. int _indexNumber;
  209. protected:
  210. /**
  211. Event handler for foreground.
  212. */
  213. void recreateVBO() const;
  214. /**
  215. Event listener for foreground.
  216. */
  217. EventListenerCustom* _recreateVBOEventListener;
  218. /**
  219. Buffer used for shadow copy.
  220. */
  221. std::vector<unsigned char> _shadowCopy;
  222. /**
  223. Hint for optimisation in GL.
  224. */
  225. GLenum _usage;
  226. protected:
  227. /**
  228. Static member to indicate that use _shadowCopy or not.
  229. */
  230. static bool _enableShadowCopy;
  231. public:
  232. /**
  233. Static getter for shadowCopy.
  234. */
  235. static bool isShadowCopyEnabled() { return _enableShadowCopy; }
  236. /**
  237. Static setter for shadowCopy.
  238. */
  239. static void enableShadowCopy(bool enabled) { _enableShadowCopy = enabled; }
  240. };
  241. NS_CC_END
  242. /**
  243. end of support group
  244. @}
  245. */
  246. #endif /* __CC_VERTEX_INDEX_BUFFER_H__*/