CCGrid.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /****************************************************************************
  2. Copyright (c) 2009 On-Core
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __EFFECTS_CCGRID_H__
  24. #define __EFFECTS_CCGRID_H__
  25. #include "base/CCRef.h"
  26. #include "base/ccTypes.h"
  27. #include "base/CCDirector.h"
  28. NS_CC_BEGIN
  29. class Texture2D;
  30. class Grabber;
  31. class GLProgram;
  32. class Node;
  33. /**
  34. * @addtogroup _2d
  35. * @{
  36. */
  37. /** Base class for Other grid.
  38. */
  39. class CC_DLL GridBase : public Ref
  40. {
  41. public:
  42. /** create one Grid */
  43. static GridBase* create(const Size& gridSize, Texture2D *texture, bool flipped);
  44. /** create one Grid */
  45. static GridBase* create(const Size& gridSize);
  46. /**
  47. Destructor.
  48. * @js NA
  49. * @lua NA
  50. */
  51. virtual ~GridBase(void);
  52. /**@{
  53. Init the Grid base.
  54. @param gridSize the size of the grid.
  55. @param texture The texture used for grab.
  56. @param flipped whether or not the grab texture should be flip by Y or not.
  57. @param rect The effective grid rect.
  58. */
  59. bool initWithSize(const Size& gridSize);
  60. bool initWithSize(const Size& gridSize, const Rect& rect);
  61. bool initWithSize(const Size& gridSize, Texture2D *texture, bool flipped);
  62. bool initWithSize(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect);
  63. /**@}*/
  64. /** @{
  65. Getter and setter of the active state of the grid.
  66. */
  67. bool isActive() const { return _active; }
  68. void setActive(bool active);
  69. /**@}*/
  70. /** Get number of times that the grid will be reused. */
  71. int getReuseGrid() const { return _reuseGrid; }
  72. /** Set number of times that the grid will be reused. */
  73. void setReuseGrid(int reuseGrid) { _reuseGrid = reuseGrid; }
  74. /** Size of the grid. */
  75. const Size& getGridSize() const { return _gridSize; }
  76. /**Set the size of the grid.*/
  77. void setGridSize(const Size& gridSize) { _gridSize = gridSize; }
  78. /** Pixels between the grids. */
  79. const Vec2& getStep() const { return _step; }
  80. /**Get the pixels between the grids.*/
  81. void setStep(const Vec2& step) { _step = step; }
  82. /** is texture flipped. */
  83. bool isTextureFlipped() const { return _isTextureFlipped; }
  84. /**Set the texture flipped or not.*/
  85. void setTextureFlipped(bool flipped);
  86. /**@{
  87. Init and reset the status when render effects by using the grid.
  88. */
  89. void beforeDraw(void);
  90. void afterDraw(Node *target);
  91. /**@}*/
  92. /**@{
  93. Interface for custom action when before or after draw.
  94. @js NA
  95. */
  96. virtual void beforeBlit() {}
  97. virtual void afterBlit() {}
  98. /**@}*/
  99. /**Interface used to blit the texture with grid to screen.*/
  100. virtual void blit(void);
  101. /**Interface, Reuse the grid vertices.*/
  102. virtual void reuse(void);
  103. /**Interface, Calculate the vertices used for the blit.*/
  104. virtual void calculateVertexPoints(void);
  105. /**Change projection to 2D for grabbing.*/
  106. void set2DProjection(void);
  107. /**
  108. * @brief Set the effect grid rect.
  109. * @param rect The effect grid rect.
  110. */
  111. void setGridRect(const Rect& rect);
  112. /**
  113. * @brief Get the effect grid rect.
  114. * @return Return the effect grid rect.
  115. */
  116. const Rect& getGridRect() const { return _gridRect; }
  117. protected:
  118. bool _active;
  119. int _reuseGrid;
  120. Size _gridSize;
  121. Texture2D *_texture;
  122. Vec2 _step;
  123. Grabber *_grabber;
  124. bool _isTextureFlipped;
  125. GLProgram* _shaderProgram;
  126. Director::Projection _directorProjection;
  127. Rect _gridRect;
  128. };
  129. /**
  130. Grid3D is a 3D grid implementation. Each vertex has 3 dimensions: x,y,z
  131. */
  132. class CC_DLL Grid3D : public GridBase
  133. {
  134. public:
  135. /** create one Grid. */
  136. static Grid3D* create(const Size& gridSize);
  137. /** create one Grid. */
  138. static Grid3D* create(const Size& gridSize, const Rect& rect);
  139. /** create one Grid. */
  140. static Grid3D* create(const Size& gridSize, Texture2D *texture, bool flipped);
  141. /** create one Grid. */
  142. static Grid3D* create(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect);
  143. /**
  144. Constructor.
  145. * @js ctor
  146. */
  147. Grid3D();
  148. /**
  149. Destructor.
  150. * @js NA
  151. * @lua NA
  152. */
  153. ~Grid3D(void);
  154. /** Returns the vertex at a given position.
  155. * @js NA
  156. * @lua NA
  157. */
  158. Vec3 getVertex(const Vec2& pos) const;
  159. /** @deprecated Use getVertex() instead
  160. * @lua NA
  161. */
  162. CC_DEPRECATED_ATTRIBUTE Vec3 vertex(const Vec2& pos) const { return getVertex(pos); }
  163. /** Returns the original (non-transformed) vertex at a given position.
  164. * @js NA
  165. * @lua NA
  166. */
  167. Vec3 getOriginalVertex(const Vec2& pos) const;
  168. /** @deprecated Use getOriginalVertex() instead
  169. * @lua NA
  170. */
  171. CC_DEPRECATED_ATTRIBUTE Vec3 originalVertex(const Vec2& pos) const { return getOriginalVertex(pos); }
  172. /** Sets a new vertex at a given position.
  173. * @lua NA
  174. */
  175. void setVertex(const Vec2& pos, const Vec3& vertex);
  176. /**@{
  177. Implementations for interfaces in base class.
  178. */
  179. virtual void beforeBlit() override;
  180. virtual void afterBlit() override;
  181. virtual void blit() override;
  182. virtual void reuse() override;
  183. virtual void calculateVertexPoints() override;
  184. /**@}*/
  185. /**@{
  186. Getter and Setter for depth test state when blit.
  187. @js NA
  188. */
  189. void setNeedDepthTestForBlit( bool neededDepthTest) { _needDepthTestForBlit = neededDepthTest; }
  190. bool getNeedDepthTestForBlit() const { return _needDepthTestForBlit; }
  191. /**@}*/
  192. protected:
  193. GLvoid *_texCoordinates;
  194. GLvoid *_vertices;
  195. GLvoid *_originalVertices;
  196. GLushort *_indices;
  197. bool _needDepthTestForBlit;
  198. bool _oldDepthTestValue;
  199. bool _oldDepthWriteValue;
  200. };
  201. /**
  202. TiledGrid3D is a 3D grid implementation. It differs from Grid3D in that
  203. the tiles can be separated from the grid.
  204. */
  205. class CC_DLL TiledGrid3D : public GridBase
  206. {
  207. public:
  208. /** Create one Grid. */
  209. static TiledGrid3D* create(const Size& gridSize);
  210. /** Create one Grid. */
  211. static TiledGrid3D* create(const Size& gridSize, const Rect& rect);
  212. /** Create one Grid. */
  213. static TiledGrid3D* create(const Size& gridSize, Texture2D *texture, bool flipped);
  214. /** Create one Grid. */
  215. static TiledGrid3D* create(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect);
  216. /**
  217. Constructor.
  218. * @js ctor
  219. */
  220. TiledGrid3D();
  221. /**
  222. Destructor.
  223. * @js NA
  224. * @lua NA
  225. */
  226. ~TiledGrid3D();
  227. /** Returns the tile at the given position.
  228. * @js NA
  229. * @lua NA
  230. */
  231. Quad3 getTile(const Vec2& pos) const;
  232. /** returns the tile at the given position
  233. * @lua NA
  234. */
  235. CC_DEPRECATED_ATTRIBUTE Quad3 tile(const Vec2& pos) const { return getTile(pos); }
  236. /** Returns the original tile (untransformed) at the given position.
  237. * @js NA
  238. * @lua NA
  239. */
  240. Quad3 getOriginalTile(const Vec2& pos) const;
  241. /** returns the original tile (untransformed) at the given position
  242. * @lua NA
  243. */
  244. CC_DEPRECATED_ATTRIBUTE Quad3 originalTile(const Vec2& pos) const { return getOriginalTile(pos); }
  245. /** Sets a new tile.
  246. * @lua NA
  247. */
  248. void setTile(const Vec2& pos, const Quad3& coords);
  249. /**@{
  250. Implementations for interfaces in base class.
  251. */
  252. virtual void blit() override;
  253. virtual void reuse() override;
  254. virtual void calculateVertexPoints() override;
  255. /**@}*/
  256. protected:
  257. GLvoid *_texCoordinates;
  258. GLvoid *_vertices;
  259. GLvoid *_originalVertices;
  260. GLushort *_indices;
  261. };
  262. // end of effects group
  263. /// @}
  264. NS_CC_END
  265. #endif // __EFFECTS_CCGRID_H__