CCRenderTexture.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /****************************************************************************
  2. Copyright (c) 2009 Jason Booth
  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 __CCRENDER_TEXTURE_H__
  24. #define __CCRENDER_TEXTURE_H__
  25. #include "2d/CCNode.h"
  26. #include "2d/CCSprite.h"
  27. #include "platform/CCImage.h"
  28. #include "renderer/CCGroupCommand.h"
  29. #include "renderer/CCCustomCommand.h"
  30. NS_CC_BEGIN
  31. class EventCustom;
  32. /**
  33. * @addtogroup _2d
  34. * @{
  35. */
  36. /**
  37. * @brief RenderTexture is a generic rendering target. To render things into it,
  38. * simply construct a render target, call begin on it, call visit on any cocos
  39. * scenes or objects to render them, and call end. For convenience, render texture
  40. * adds a sprite as it's display child with the results, so you can simply add
  41. * the render texture to your scene and treat it like any other CocosNode.
  42. * There are also functions for saving the render texture to disk in PNG or JPG format.
  43. * @since v0.8.1
  44. */
  45. class CC_DLL RenderTexture : public Node
  46. {
  47. public:
  48. /** Initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format.
  49. *
  50. * @param w The RenderTexture object width.
  51. * @param h The RenderTexture object height.
  52. * @param format In Points and a pixel format( only RGB and RGBA formats are valid ).
  53. * @param depthStencilFormat The depthStencil format.
  54. */
  55. static RenderTexture * create(int w ,int h, Texture2D::PixelFormat format, GLuint depthStencilFormat);
  56. /** Creates a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid.
  57. *
  58. * @param w The RenderTexture object width.
  59. * @param h The RenderTexture object height.
  60. * @param format In Points and a pixel format( only RGB and RGBA formats are valid ).
  61. */
  62. static RenderTexture * create(int w, int h, Texture2D::PixelFormat format);
  63. /** Creates a RenderTexture object with width and height in Points, pixel format is RGBA8888.
  64. *
  65. * @param w The RenderTexture object width.
  66. * @param h The RenderTexture object height.
  67. */
  68. static RenderTexture * create(int w, int h);
  69. /** Starts grabbing. */
  70. virtual void begin();
  71. /** Starts rendering to the texture while clearing the texture first.
  72. * This is more efficient then calling -clear first and then -begin.
  73. *
  74. * @param r Red.
  75. * @param g Green.
  76. * @param b Blue.
  77. * @param a Alpha.
  78. */
  79. virtual void beginWithClear(float r, float g, float b, float a);
  80. /** Starts rendering to the texture while clearing the texture first.
  81. * This is more efficient then calling -clear first and then -begin.
  82. *
  83. * @param r Red.
  84. * @param g Green.
  85. * @param b Blue.
  86. * @param a Alpha.
  87. * @param depthValue The depth Value.
  88. */
  89. virtual void beginWithClear(float r, float g, float b, float a, float depthValue);
  90. /** Starts rendering to the texture while clearing the texture first.
  91. * This is more efficient then calling -clear first and then -begin.
  92. *
  93. * @param r Red.
  94. * @param g Green.
  95. * @param b Blue.
  96. * @param a Alpha.
  97. * @param depthValue A specified depth value.
  98. * @param stencilValue A specified stencil value.
  99. */
  100. virtual void beginWithClear(float r, float g, float b, float a, float depthValue, int stencilValue);
  101. /** Ends grabbing.
  102. * @lua endToLua
  103. */
  104. virtual void end();
  105. /** Clears the texture with a color.
  106. *
  107. * @param r Red.
  108. * @param g Green.
  109. * @param b Blue.
  110. * @param a Alpha.
  111. */
  112. void clear(float r, float g, float b, float a);
  113. /** Clears the texture with a specified depth value.
  114. *
  115. * @param depthValue A specified depth value.
  116. */
  117. virtual void clearDepth(float depthValue);
  118. /** Clears the texture with a specified stencil value.
  119. *
  120. * @param stencilValue A specified stencil value.
  121. */
  122. virtual void clearStencil(int stencilValue);
  123. /* Creates a new Image from with the texture's data.
  124. * Caller is responsible for releasing it by calling delete.
  125. *
  126. * @param flipImage Whether or not to flip image.
  127. * @return An image.
  128. * @js NA
  129. */
  130. Image* newImage(bool flipImage = true);
  131. CC_DEPRECATED_ATTRIBUTE Image* newCCImage(bool flipImage = true) { return newImage(flipImage); };
  132. /** Saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
  133. * Returns true if the operation is successful.
  134. *
  135. * @param filename The file name.
  136. * @param isRGBA The file is RGBA or not.
  137. * @param callback When the file is save finished,it will callback this function.
  138. * @return Returns true if the operation is successful.
  139. */
  140. bool saveToFile(const std::string& filename, bool isRGBA = true, std::function<void (RenderTexture*, const std::string&)> callback = nullptr);
  141. /** saves the texture into a file. The format could be JPG or PNG. The file will be saved in the Documents folder.
  142. Returns true if the operation is successful.
  143. * Notes: since v3.x, saveToFile will generate a custom command, which will be called in the following render->render().
  144. * So if this function is called in a event handler, the actual save file will be called in the next frame. If we switch to a different scene, the game will crash.
  145. * To solve this, add Director::getInstance()->getRenderer()->render(); after this function.
  146. *
  147. * @param filename The file name.
  148. * @param format The image format.
  149. * @param isRGBA The file is RGBA or not.
  150. * @param callback When the file is save finished,it will callback this function.
  151. * @return Returns true if the operation is successful.
  152. */
  153. bool saveToFile(const std::string& filename, Image::Format format, bool isRGBA = true, std::function<void (RenderTexture*, const std::string&)> callback = nullptr);
  154. /** Listen "come to background" message, and save render texture.
  155. * It only has effect on Android.
  156. *
  157. * @param event Event Custom.
  158. */
  159. void listenToBackground(EventCustom *event);
  160. /** Listen "come to foreground" message and restore the frame buffer object.
  161. * It only has effect on Android.
  162. *
  163. * @param event Event Custom.
  164. */
  165. void listenToForeground(EventCustom *event);
  166. /** Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT. They can be OR'ed. Valid when "autoDraw" is true.
  167. *
  168. * @return Clear flags.
  169. */
  170. unsigned int getClearFlags() const { return _clearFlags; }
  171. /** Set flags.
  172. *
  173. * @param clearFlags Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
  174. */
  175. void setClearFlags(unsigned int clearFlags) { _clearFlags = clearFlags; }
  176. /** Clear color value. Valid only when "autoDraw" is true.
  177. *
  178. * @return Color value.
  179. */
  180. const Color4F& getClearColor() const { return _clearColor; }
  181. /** Set color value.
  182. *
  183. * @param clearColor Color value.
  184. */
  185. void setClearColor(const Color4F &clearColor) { _clearColor = clearColor; }
  186. /** Value for clearDepth. Valid only when "autoDraw" is true.
  187. *
  188. * @return Value for clearDepth.
  189. */
  190. float getClearDepth() const { return _clearDepth; }
  191. /** Set Value for clearDepth.
  192. *
  193. * @param clearDepth Value for clearDepth.
  194. */
  195. void setClearDepth(float clearDepth) { _clearDepth = clearDepth; }
  196. /** Value for clear Stencil. Valid only when "autoDraw" is true.
  197. *
  198. * @return Value for clear Stencil.
  199. */
  200. int getClearStencil() const { return _clearStencil; }
  201. /** Set Value for clear Stencil.
  202. *
  203. * @param clearStencil Value for clear Stencil.
  204. */
  205. void setClearStencil(int clearStencil) { _clearStencil = clearStencil; }
  206. /** When enabled, it will render its children into the texture automatically. Disabled by default for compatibility reasons.
  207. * Will be enabled in the future.
  208. *
  209. * @return Return the autoDraw value.
  210. */
  211. bool isAutoDraw() const { return _autoDraw; }
  212. /** Set a valve to control whether or not render its children into the texture automatically.
  213. *
  214. * @param isAutoDraw Whether or not render its children into the texture automatically.
  215. */
  216. void setAutoDraw(bool isAutoDraw) { _autoDraw = isAutoDraw; }
  217. /** Gets the Sprite being used.
  218. *
  219. * @return A Sprite.
  220. */
  221. Sprite* getSprite() const { return _sprite; }
  222. /** Sets the Sprite being used.
  223. *
  224. * @param sprite A Sprite.
  225. */
  226. void setSprite(Sprite* sprite);
  227. // Overrides
  228. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  229. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  230. /** Flag: Use stack matrix computed from scene hierarchy or generate new modelView and projection matrix.
  231. *
  232. * @param keepMatrix Whether or not use stack matrix computed from scene hierarchy or generate new modelView and projection matrix.
  233. * @js NA
  234. */
  235. void setKeepMatrix(bool keepMatrix);
  236. /**Used for grab part of screen to a texture.
  237. * @param rtBegin The position of renderTexture on the fullRect.
  238. * @param fullRect The total size of screen.
  239. * @param fullViewport The total viewportSize.
  240. */
  241. void setVirtualViewport(const Vec2& rtBegin, const Rect& fullRect, const Rect& fullViewport);
  242. public:
  243. /** FIXME: should be protected.
  244. * but due to a bug in PowerVR + Android,
  245. * the constructor is public again.
  246. * @js ctor
  247. */
  248. RenderTexture();
  249. /**
  250. * @js NA
  251. * @lua NA
  252. */
  253. virtual ~RenderTexture();
  254. /** Initializes a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid.
  255. *
  256. * @param w The RenderTexture object width.
  257. * @param h The RenderTexture object height.
  258. * @param format In Points and a pixel format( only RGB and RGBA formats are valid ).
  259. * @return If succeed, it will return true.
  260. */
  261. bool initWithWidthAndHeight(int w, int h, Texture2D::PixelFormat format);
  262. /** Initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format.
  263. *
  264. * @param w The RenderTexture object width.
  265. * @param h The RenderTexture object height.
  266. * @param format In Points and a pixel format( only RGB and RGBA formats are valid ).
  267. * @param depthStencilFormat The depthStencil format.
  268. * @return If succeed, it will return true.
  269. */
  270. bool initWithWidthAndHeight(int w, int h, Texture2D::PixelFormat format, GLuint depthStencilFormat);
  271. protected:
  272. virtual void beginWithClear(float r, float g, float b, float a, float depthValue, int stencilValue, GLbitfield flags);
  273. //flags: whether generate new modelView and projection matrix or not
  274. bool _keepMatrix;
  275. Rect _rtTextureRect;
  276. Rect _fullRect;
  277. Rect _fullviewPort;
  278. GLuint _FBO;
  279. GLuint _depthRenderBuffer;
  280. GLuint _stencilRenderBuffer;
  281. GLint _oldFBO;
  282. Texture2D* _texture;
  283. Texture2D* _textureCopy; // a copy of _texture
  284. Image* _UITextureImage;
  285. Texture2D::PixelFormat _pixelFormat;
  286. GLuint _depthAndStencilFormat;
  287. // code for "auto" update
  288. GLbitfield _clearFlags;
  289. Color4F _clearColor;
  290. GLclampf _clearDepth;
  291. GLint _clearStencil;
  292. bool _autoDraw;
  293. /** The Sprite being used.
  294. The sprite, by default, will use the following blending function: GL_ONE, GL_ONE_MINUS_SRC_ALPHA.
  295. The blending function can be changed in runtime by calling:
  296. - renderTexture->getSprite()->setBlendFunc((BlendFunc){GL_ONE, GL_ONE_MINUS_SRC_ALPHA});
  297. */
  298. Sprite* _sprite;
  299. GroupCommand _groupCommand;
  300. CustomCommand _beginWithClearCommand;
  301. CustomCommand _clearDepthCommand;
  302. CustomCommand _clearCommand;
  303. CustomCommand _beginCommand;
  304. CustomCommand _endCommand;
  305. /*this command is used to encapsulate saveToFile,
  306. call saveToFile twice will overwrite this command and callback
  307. and the command and callback will be executed twice.
  308. */
  309. CustomCommand _saveToFileCommand;
  310. std::function<void (RenderTexture*, const std::string&)> _saveFileCallback;
  311. protected:
  312. //renderer caches and callbacks
  313. void onBegin();
  314. void onEnd();
  315. void onClear();
  316. void onClearDepth();
  317. void onSaveToFile(const std::string& fileName, bool isRGBA = true);
  318. void setupDepthAndStencil(int powW, int powH);
  319. Mat4 _oldTransMatrix, _oldProjMatrix;
  320. Mat4 _transformMatrix, _projectionMatrix;
  321. private:
  322. CC_DISALLOW_COPY_AND_ASSIGN(RenderTexture);
  323. };
  324. // end of textures group
  325. /// @}
  326. NS_CC_END
  327. #endif //__CCRENDER_TEXTURE_H__