大佬教程收集整理的这篇文章主要介绍了Cocos2d-x 3.x RenderTexture渲染纹理源码分析,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
最近在学习3.x的源码,3.x的渲染机制改动比较大,改变了之前在每帧遍历所有的Node树节点时通过draw()方法中的直接渲染,而是通过生成渲染指令并将渲染指令发送到渲染队列,在每一帧结束时进行延迟渲染,这样就把游戏循环和渲染分离开来,2dx官方说要把渲染放在一个独立的线程来处理,按照3.x现在的架构可能不久就会实现。
Cocos2d-x提供了一个RenderTexture类来支持把帧缓冲中的数据渲染到一块纹理之上,通过它可以来实现游戏截图,获取纹理上的像素信息等效果,在3.x的RenderTexutrue则是使用了GroupCommand组渲染指令,将其内部一系列的渲染指令放到一个渲染队列中,这样这个组内的渲染指令将单独使用一个渲染队列,不受其他渲染指令的影响(如渲染的优先级)。
我使用的是Cocos2d-x3.2的版本,RenderTexture类在2d/misc-nodes/CCRenderTexture.h/.cpp下。
把源码都加了注释,可以通过RenderTexture整个类的构造->初始化->绘制过程->析构,来大致了解一下RenderTextrue是如何将这缓冲的数据渲染到纹理的工作流程。
CCRenderTexture.h文件
CCRenderTexture.cpp文件
本文转载自:http://www.voidcn.com/article/p-gbobhtdc-g.html
#ifndef __CCRENDER_TEXTURE_H__
#define __CCRENDER_TEXTURE_H__
#include "2d/CCNode.h"
#include "2d/CCSprite.h"
#include "platform/CCImage.h"
#include "renderer/CCGroupCommand.h"
#include "renderer/CCCustomcatommand.h"
NS_CC_BEGIN
class
EventCustom;
/**
* @addtogroup textures
* @{
*/
/**
@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
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
*/
class
CC_DLL RenderTexture :
public
Node
public
:
/** initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format*/
static
RenderTexture * create(
int
w,
int
h,Texture2D::PixelFormat format,GLuint depthStencilFormat);
/** creates a RenderTexture object with width and height in Points and a pixel format,only RGB and RGBA formats are valid */
/** starts grabbing */
/** starts rendering to the texture while clearing the texture first.
This is more efficient then calling -clear first and then -begin */
/** starts rendering to the texture while clearing the texture first.
This is more efficient then calling -clear first and then -begin */
/** starts rendering to the texture while clearing the texture first.
This is more efficient then calling -clear first and then -begin */
/** end is key word of lua,use other name to export to lua. */
inline
void
endToLua(){
end();
};
/** ends grabbing*/
/** clears the texture with a color */
/** clears the texture with a specified depth value */
/** clears the texture with a specified stencil value */
/* creates a new Image from with the texture's data.
Caller is responsible for releasing it by calling delete.
*/
};
/** saves the texturE into a file using JPEG format. The file will be saved in the Documents folder.
*/
/** saves the texturE into a file. The format could be JPG or PNG. The file will be saved in the Documents folder.
*/
It only has effect on Android.
*/
It only has effect on Android.
*/
/** 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
_clearFlags;
};
_clearFlags = clearFlags;
};
/** Clear color value. Valid only when "autoDraw" is true. */
return
_clearColor;
};
_clearColor = clearColor;
};
/** Value for clearDepth. Valid only when "autoDraw" is true. */
return
_clearDepth;
};
_clearDepth = clearDepth;
};
return
_clearStencil;
};
_clearStencil = clearStencil;
};
/** When enabled,it will render its children into the texture automatically. Disabled by default for compatiblity reasons.
Will be enabled in the future.
*/
return
_autoDraw;
};
_autoDraw = isAutoDraw;
};
/** Gets the Sprite being used. */
return
_sprite;
};
/** Sets the Sprite being used. */
inline
void
setSprite(Sprite* spritE) {
CC_SAFE_RETAIN(spritE);
CC_SAFE_RELEASE(_spritE);
_sprite = sprite;
};
// Overrides
virtual
void
visit(Renderer *renderer,
const
@H_612_3534@mat4 &parentTransform,uint32_t parentFlags) override;
//flag: use stack matrix computed from scene hierarchy or generate new modelView and projection matrix
/**Used for grab part of screen to a texture.
//rtBegin: the position of renderTexture on the fullRect
//fullRect: the @R_527_10586@l size of screen
//fullViewport: the @R_527_10586@l viewportSize
*/
public
:
// XXX should be procted.
// but due to a bug in PowerVR + Android,
// the constructor is public again
RenderTexture();
/** initializes a RenderTexture object with width and height in Points and a pixel format,only RGB and RGBA formats are valid */
/** initializes a RenderTexture object with width and height in Points and a pixel format( only RGB and RGBA formats are valid ) and depthStencil format*/
protected
:
//flags: whether generate new modelView and projection matrix or not
bool
_keepMatrix;
Rect _rtTextureRect;
Rect _fullRect;
Rect _fullviewPort;
GLuint _FBO;
GLuint _depthRenderBufffer;
GLint _oldFBO;
Texture2D* _texture;
Texture2D* _textureCopy;
// a copy of _texture
Image* _UITextureImage;
Texture2D::PixelFormat _pixelFormat;
// code for "auto" update
GLbitfield _clearFlags;
Color4F _clearColor;
GLclampf _clearDepth;
GLint _clearStencil;
bool
_autoDraw;
/** 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:
@H_369_4899@
- renderTexture->getSprite()->setBlendFunc((BlendFunC){
GL_ONE,GL_ONE_MINUS_SRC_ALPHA
});
*/
Sprite* _sprite;
GroupCommand _groupCommand;
Customcatommand _beginWithClearCommand;
Customcatommand _clearDepthCommand;
Customcatommand _clearCommand;
Customcatommand _beginCommand;
Customcatommand _endCommand;
Customcatommand _saveToFileCommand;
protected
:
//renderer caches and callBACks
private
:
CC_DISALLOW_COPY_AND_ASSIGN(RenderTexturE);
};
// end of textures group
/// @
}
NS_CC_END
#endif //__CCRENDER_TEXTURE_H__
以上是大佬教程为你收集整理的Cocos2d-x 3.x RenderTexture渲染纹理源码分析全部内容,希望文章能够帮你解决Cocos2d-x 3.x RenderTexture渲染纹理源码分析所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。