2012-04-19 14:35:52 +08:00
/****************************************************************************
Copyright ( c ) 2009 Jason Booth
2014-01-07 11:25:07 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
2018-01-29 16:25:32 +08:00
Copyright ( c ) 2013 - 2016 Chukong Technologies Inc .
Copyright ( c ) 2017 - 2018 Xiamen Yaji Software Co . , Ltd .
2012-04-19 14:35:52 +08:00
http : //www.cocos2d-x.org
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# ifndef __CCRENDER_TEXTURE_H__
# define __CCRENDER_TEXTURE_H__
2014-04-27 01:11:22 +08:00
# include "2d/CCNode.h"
# include "2d/CCSprite.h"
2014-05-17 05:36:00 +08:00
# include "platform/CCImage.h"
2014-04-30 08:37:36 +08:00
# include "renderer/CCGroupCommand.h"
# include "renderer/CCCustomCommand.h"
2012-04-19 14:35:52 +08:00
NS_CC_BEGIN
2013-12-31 10:54:37 +08:00
class EventCustom ;
2012-06-20 18:09:11 +08:00
/**
2015-03-21 17:06:26 +08:00
* @ addtogroup _2d
2012-06-20 18:09:11 +08:00
* @ {
*/
2012-04-19 14:35:52 +08:00
/**
2015-03-21 17:06:26 +08:00
* @ brief RenderTexture is a generic rendering target . To render things into it ,
* simply construct a render target , call begin on it , call visit on any cocos
* scenes or objects to render them , and call end . For convenience , render texture
* adds a sprite as it ' s display child with the results , so you can simply add
* the render texture to your scene and treat it like any other CocosNode .
* There are also functions for saving the render texture to disk in PNG or JPG format .
* @ since v0 .8 .1
*/
2013-06-20 14:13:12 +08:00
class CC_DLL RenderTexture : public Node
2012-04-19 14:35:52 +08:00
{
public :
2015-03-21 17:06:26 +08:00
/** Initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format.
*
* @ param w The RenderTexture object width .
* @ param h The RenderTexture object height .
* @ param format In Points and a pixel format ( only RGB and RGBA formats are valid ) .
* @ param depthStencilFormat The depthStencil format .
*/
2013-12-18 17:47:20 +08:00
static RenderTexture * create ( int w , int h , Texture2D : : PixelFormat format , GLuint depthStencilFormat ) ;
2012-06-08 14:30:55 +08:00
2015-03-21 17:06:26 +08:00
/** Creates a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid.
*
* @ param w The RenderTexture object width .
* @ param h The RenderTexture object height .
* @ param format In Points and a pixel format ( only RGB and RGBA formats are valid ) .
*/
2013-12-18 17:47:20 +08:00
static RenderTexture * create ( int w , int h , Texture2D : : PixelFormat format ) ;
2012-04-19 14:35:52 +08:00
2015-03-21 17:06:26 +08:00
/** Creates a RenderTexture object with width and height in Points, pixel format is RGBA8888.
*
* @ param w The RenderTexture object width .
* @ param h The RenderTexture object height .
*/
2013-06-20 14:13:12 +08:00
static RenderTexture * create ( int w , int h ) ;
2012-06-12 01:43:07 +08:00
2015-03-21 17:06:26 +08:00
/** Starts grabbing. */
2013-12-17 14:18:41 +08:00
virtual void begin ( ) ;
2012-04-19 14:35:52 +08:00
2015-03-21 17:06:26 +08:00
/** Starts rendering to the texture while clearing the texture first.
* This is more efficient then calling - clear first and then - begin .
*
* @ param r Red .
* @ param g Green .
* @ param b Blue .
* @ param a Alpha .
*/
2013-12-05 03:54:57 +08:00
virtual void beginWithClear ( float r , float g , float b , float a ) ;
2012-04-19 14:35:52 +08:00
2015-03-21 17:06:26 +08:00
/** Starts rendering to the texture while clearing the texture first.
* This is more efficient then calling - clear first and then - begin .
*
* @ param r Red .
* @ param g Green .
* @ param b Blue .
* @ param a Alpha .
2015-03-27 11:39:31 +08:00
* @ param depthValue The depth Value .
2015-03-21 17:06:26 +08:00
*/
2013-12-05 03:54:57 +08:00
virtual void beginWithClear ( float r , float g , float b , float a , float depthValue ) ;
2012-06-19 13:50:11 +08:00
2015-03-21 17:06:26 +08:00
/** Starts rendering to the texture while clearing the texture first.
* This is more efficient then calling - clear first and then - begin .
*
* @ param r Red .
* @ param g Green .
* @ param b Blue .
* @ param a Alpha .
* @ param depthValue A specified depth value .
* @ param stencilValue A specified stencil value .
*/
2013-12-05 03:54:57 +08:00
virtual void beginWithClear ( float r , float g , float b , float a , float depthValue , int stencilValue ) ;
2012-06-12 01:43:07 +08:00
2016-06-14 23:35:17 +08:00
/** Ends grabbing.
* @ lua endToLua
2015-03-28 14:28:16 +08:00
*/
2013-12-17 14:18:41 +08:00
virtual void end ( ) ;
2012-04-19 14:35:52 +08:00
2015-03-21 17:06:26 +08:00
/** Clears the texture with a color.
*
* @ param r Red .
* @ param g Green .
* @ param b Blue .
* @ param a Alpha .
*/
2012-04-19 14:35:52 +08:00
void clear ( float r , float g , float b , float a ) ;
2015-03-21 17:06:26 +08:00
/** Clears the texture with a specified depth value.
*
2015-03-27 11:39:31 +08:00
* @ param depthValue A specified depth value .
2015-03-21 17:06:26 +08:00
*/
2013-12-05 03:54:57 +08:00
virtual void clearDepth ( float depthValue ) ;
2012-06-19 13:50:11 +08:00
2015-03-21 17:06:26 +08:00
/** Clears the texture with a specified stencil value.
*
2015-03-27 11:39:31 +08:00
* @ param stencilValue A specified stencil value .
2012-04-19 14:35:52 +08:00
*/
2015-03-21 17:06:26 +08:00
virtual void clearStencil ( int stencilValue ) ;
2013-07-24 17:53:34 +08:00
2015-03-21 17:06:26 +08:00
/* Creates a new Image from with the texture's data.
* Caller is responsible for releasing it by calling delete .
*
* @ param flipImage Whether or not to flip image .
* @ return An image .
2015-03-28 14:28:16 +08:00
* @ js NA
2015-03-21 17:06:26 +08:00
*/
2013-06-20 14:13:12 +08:00
Image * newImage ( bool flipImage = true ) ;
2013-07-24 17:53:34 +08:00
CC_DEPRECATED_ATTRIBUTE Image * newCCImage ( bool flipImage = true ) { return newImage ( flipImage ) ; } ;
2012-04-19 14:35:52 +08:00
2015-03-21 17:06:26 +08:00
/** Saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
* Returns true if the operation is successful .
*
* @ param filename The file name .
* @ param isRGBA The file is RGBA or not .
* @ param callback When the file is save finished , it will callback this function .
* @ return Returns true if the operation is successful .
2012-04-19 14:35:52 +08:00
*/
2014-08-12 10:50:57 +08:00
bool saveToFile ( const std : : string & filename , bool isRGBA = true , std : : function < void ( RenderTexture * , const std : : string & ) > callback = nullptr ) ;
2012-04-19 14:35:52 +08:00
/** saves the texture into a file. The format could be JPG or PNG. The file will be saved in the Documents folder.
2013-08-01 16:39:42 +08:00
Returns true if the operation is successful .
2015-09-22 16:08:23 +08:00
* Notes : since v3 . x , saveToFile will generate a custom command , which will be called in the following render - > render ( ) .
2015-03-21 17:06:26 +08:00
* 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 .
* To solve this , add Director : : getInstance ( ) - > getRenderer ( ) - > render ( ) ; after this function .
*
* @ param filename The file name .
* @ param format The image format .
* @ param isRGBA The file is RGBA or not .
* @ param callback When the file is save finished , it will callback this function .
* @ return Returns true if the operation is successful .
2012-04-19 14:35:52 +08:00
*/
2014-08-12 10:50:57 +08:00
bool saveToFile ( const std : : string & filename , Image : : Format format , bool isRGBA = true , std : : function < void ( RenderTexture * , const std : : string & ) > callback = nullptr ) ;
2012-06-14 18:37:57 +08:00
/** Listen "come to background" message, and save render texture.
2015-03-21 17:06:26 +08:00
* It only has effect on Android .
*
* @ param event Event Custom .
2012-06-14 18:37:57 +08:00
*/
2013-12-31 10:54:37 +08:00
void listenToBackground ( EventCustom * event ) ;
2012-11-14 18:05:15 +08:00
2015-03-21 17:06:26 +08:00
/** Listen "come to foreground" message and restore the frame buffer object.
* It only has effect on Android .
*
* @ param event Event Custom .
2012-11-30 07:04:07 +08:00
*/
2013-12-31 10:54:37 +08:00
void listenToForeground ( EventCustom * event ) ;
2012-11-30 07:04:07 +08:00
2015-03-21 17:06:26 +08:00
/** Valid flags: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT. They can be OR'ed. Valid when "autoDraw" is true.
*
* @ return Clear flags .
*/
2016-09-12 09:44:21 +08:00
unsigned int getClearFlags ( ) const { return _clearFlags ; }
2015-03-21 17:06:26 +08:00
/** Set flags.
*
* @ param clearFlags Valid flags : GL_COLOR_BUFFER_BIT , GL_DEPTH_BUFFER_BIT , GL_STENCIL_BUFFER_BIT .
*/
2016-09-12 09:44:21 +08:00
void setClearFlags ( unsigned int clearFlags ) { _clearFlags = clearFlags ; }
2012-11-14 18:05:15 +08:00
2015-03-21 17:06:26 +08:00
/** Clear color value. Valid only when "autoDraw" is true.
*
* @ return Color value .
*/
2016-09-12 09:44:21 +08:00
const Color4F & getClearColor ( ) const { return _clearColor ; }
2015-03-21 17:06:26 +08:00
/** Set color value.
*
* @ param clearColor Color value .
*/
2016-09-12 09:44:21 +08:00
void setClearColor ( const Color4F & clearColor ) { _clearColor = clearColor ; }
2012-11-14 18:05:15 +08:00
2015-03-21 17:06:26 +08:00
/** Value for clearDepth. Valid only when "autoDraw" is true.
*
* @ return Value for clearDepth .
*/
2016-09-12 09:44:21 +08:00
float getClearDepth ( ) const { return _clearDepth ; }
2015-03-21 17:06:26 +08:00
/** Set Value for clearDepth.
*
* @ param clearDepth Value for clearDepth .
*/
2016-09-12 09:44:21 +08:00
void setClearDepth ( float clearDepth ) { _clearDepth = clearDepth ; }
2012-11-14 18:05:15 +08:00
2015-03-21 17:06:26 +08:00
/** Value for clear Stencil. Valid only when "autoDraw" is true.
*
* @ return Value for clear Stencil .
*/
2016-09-12 09:44:21 +08:00
int getClearStencil ( ) const { return _clearStencil ; }
2015-03-21 17:06:26 +08:00
/** Set Value for clear Stencil.
*
* @ param clearStencil Value for clear Stencil .
*/
2016-09-12 09:44:21 +08:00
void setClearStencil ( int clearStencil ) { _clearStencil = clearStencil ; }
2012-11-14 18:05:15 +08:00
2015-09-22 16:08:23 +08:00
/** When enabled, it will render its children into the texture automatically. Disabled by default for compatibility reasons.
2015-03-21 17:06:26 +08:00
* Will be enabled in the future .
*
* @ return Return the autoDraw value .
2012-11-14 18:05:15 +08:00
*/
2016-09-12 09:44:21 +08:00
bool isAutoDraw ( ) const { return _autoDraw ; }
2015-03-21 17:06:26 +08:00
/** Set a valve to control whether or not render its children into the texture automatically.
*
* @ param isAutoDraw Whether or not render its children into the texture automatically .
*/
2016-09-12 09:44:21 +08:00
void setAutoDraw ( bool isAutoDraw ) { _autoDraw = isAutoDraw ; }
2012-11-14 18:05:15 +08:00
2015-03-21 17:06:26 +08:00
/** Gets the Sprite being used.
*
* @ return A Sprite .
*/
2016-09-12 09:44:21 +08:00
Sprite * getSprite ( ) const { return _sprite ; }
2013-07-23 18:26:26 +08:00
2015-03-21 17:06:26 +08:00
/** Sets the Sprite being used.
*
2015-03-27 11:39:31 +08:00
* @ param sprite A Sprite .
2015-03-21 17:06:26 +08:00
*/
2015-12-17 21:55:47 +08:00
void setSprite ( Sprite * sprite ) ;
2013-07-23 18:26:26 +08:00
2013-07-18 07:56:19 +08:00
// Overrides
2014-05-31 07:42:05 +08:00
virtual void visit ( Renderer * renderer , const Mat4 & parentTransform , uint32_t parentFlags ) override ;
virtual void draw ( Renderer * renderer , const Mat4 & transform , uint32_t flags ) override ;
2018-01-18 14:28:40 +08:00
virtual void setGlobalZOrder ( float globalZOrder ) override ;
2013-07-18 07:56:19 +08:00
2015-03-21 17:06:26 +08:00
/** Flag: Use stack matrix computed from scene hierarchy or generate new modelView and projection matrix.
*
2015-09-22 16:08:23 +08:00
* @ param keepMatrix Whether or not use stack matrix computed from scene hierarchy or generate new modelView and projection matrix .
2015-03-28 14:28:16 +08:00
* @ js NA
2015-03-21 17:06:26 +08:00
*/
2014-03-04 10:41:03 +08:00
void setKeepMatrix ( bool keepMatrix ) ;
2014-03-04 17:34:48 +08:00
/**Used for grab part of screen to a texture.
2015-03-21 17:06:26 +08:00
* @ param rtBegin The position of renderTexture on the fullRect .
* @ param fullRect The total size of screen .
* @ param fullViewport The total viewportSize .
*/
2014-05-15 01:07:09 +08:00
void setVirtualViewport ( const Vec2 & rtBegin , const Rect & fullRect , const Rect & fullViewport ) ;
2014-03-04 10:41:03 +08:00
2013-11-23 08:14:05 +08:00
public :
2015-09-22 16:08:23 +08:00
/** FIXME: should be protected.
2015-03-21 17:06:26 +08:00
* but due to a bug in PowerVR + Android ,
* the constructor is public again .
2015-03-28 14:28:16 +08:00
* @ js ctor
2015-03-21 17:06:26 +08:00
*/
2013-11-15 08:33:43 +08:00
RenderTexture ( ) ;
2015-03-28 14:28:16 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-11-15 08:33:43 +08:00
virtual ~ RenderTexture ( ) ;
2015-03-21 17:06:26 +08:00
/** Initializes a RenderTexture object with width and height in Points and a pixel format, only RGB and RGBA formats are valid.
*
* @ param w The RenderTexture object width .
* @ param h The RenderTexture object height .
* @ param format In Points and a pixel format ( only RGB and RGBA formats are valid ) .
2015-09-22 16:08:23 +08:00
* @ return If succeed , it will return true .
2015-03-21 17:06:26 +08:00
*/
2013-12-18 17:47:20 +08:00
bool initWithWidthAndHeight ( int w , int h , Texture2D : : PixelFormat format ) ;
2015-03-21 17:06:26 +08:00
/** Initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format.
*
* @ param w The RenderTexture object width .
* @ param h The RenderTexture object height .
* @ param format In Points and a pixel format ( only RGB and RGBA formats are valid ) .
* @ param depthStencilFormat The depthStencil format .
2015-09-22 16:08:23 +08:00
* @ return If succeed , it will return true .
2015-03-21 17:06:26 +08:00
*/
2013-12-18 17:47:20 +08:00
bool initWithWidthAndHeight ( int w , int h , Texture2D : : PixelFormat format , GLuint depthStencilFormat ) ;
2013-11-15 08:33:43 +08:00
2013-11-23 08:14:05 +08:00
protected :
2013-12-23 19:59:09 +08:00
virtual void beginWithClear ( float r , float g , float b , float a , float depthValue , int stencilValue , GLbitfield flags ) ;
2014-03-04 15:30:05 +08:00
//flags: whether generate new modelView and projection matrix or not
2014-03-04 10:41:03 +08:00
bool _keepMatrix ;
Rect _rtTextureRect ;
Rect _fullRect ;
Rect _fullviewPort ;
2013-06-15 14:03:30 +08:00
GLuint _FBO ;
2017-01-20 09:59:02 +08:00
GLuint _depthRenderBuffer ;
GLuint _stencilRenderBuffer ;
2013-06-15 14:03:30 +08:00
GLint _oldFBO ;
2013-06-20 14:13:12 +08:00
Texture2D * _texture ;
Texture2D * _textureCopy ; // a copy of _texture
Image * _UITextureImage ;
2013-07-26 04:36:19 +08:00
Texture2D : : PixelFormat _pixelFormat ;
2018-01-21 19:07:49 +08:00
GLuint _depthAndStencilFormat ;
2012-11-14 18:05:15 +08:00
// code for "auto" update
2013-06-15 14:03:30 +08:00
GLbitfield _clearFlags ;
2013-07-05 16:49:22 +08:00
Color4F _clearColor ;
2013-06-15 14:03:30 +08:00
GLclampf _clearDepth ;
GLint _clearStencil ;
bool _autoDraw ;
2013-07-18 07:56:19 +08:00
/** The Sprite being used.
The sprite , by default , will use the following blending function : GL_ONE , GL_ONE_MINUS_SRC_ALPHA .
The blending function can be changed in runtime by calling :
2014-05-07 16:37:21 +08:00
- renderTexture - > getSprite ( ) - > setBlendFunc ( ( BlendFunc ) { GL_ONE , GL_ONE_MINUS_SRC_ALPHA } ) ;
2013-07-18 07:56:19 +08:00
*/
2013-07-23 18:26:26 +08:00
Sprite * _sprite ;
2013-12-26 21:19:12 +08:00
2013-12-26 18:11:23 +08:00
GroupCommand _groupCommand ;
2013-12-26 21:19:12 +08:00
CustomCommand _beginWithClearCommand ;
CustomCommand _clearDepthCommand ;
CustomCommand _clearCommand ;
CustomCommand _beginCommand ;
CustomCommand _endCommand ;
2014-08-13 10:14:26 +08:00
/*this command is used to encapsulate saveToFile,
call saveToFile twice will overwrite this command and callback
and the command and callback will be executed twice .
*/
2014-03-07 17:41:51 +08:00
CustomCommand _saveToFileCommand ;
2014-08-12 10:50:57 +08:00
std : : function < void ( RenderTexture * , const std : : string & ) > _saveFileCallback ;
2013-12-23 15:59:47 +08:00
protected :
//renderer caches and callbacks
void onBegin ( ) ;
void onEnd ( ) ;
2013-11-15 08:33:43 +08:00
2013-12-23 15:59:47 +08:00
void onClear ( ) ;
void onClearDepth ( ) ;
2014-03-07 17:41:51 +08:00
2014-06-13 13:35:18 +08:00
void onSaveToFile ( const std : : string & fileName , bool isRGBA = true ) ;
2018-01-21 19:07:49 +08:00
void setupDepthAndStencil ( int powW , int powH ) ;
2013-12-23 15:59:47 +08:00
2014-05-15 01:07:09 +08:00
Mat4 _oldTransMatrix , _oldProjMatrix ;
Mat4 _transformMatrix , _projectionMatrix ;
2013-11-15 08:33:43 +08:00
private :
CC_DISALLOW_COPY_AND_ASSIGN ( RenderTexture ) ;
2012-04-19 14:35:52 +08:00
} ;
2012-06-20 18:09:11 +08:00
// end of textures group
/// @}
2012-04-19 14:35:52 +08:00
NS_CC_END
2012-06-14 05:26:28 +08:00
# endif //__CCRENDER_TEXTURE_H__