1.渲染流程的区别
- 1.cocos2d-x 2.x的Sprite的draw方法
找个2.x的CCSprite,你看看它的draw(void)方法,你就会发现这个方法里面做了所有的openGl绘制。
- 2.cocos2d-x 3.x的Sprite的draw方法
找个2.x的CCSprite,你看看它的draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)方法,你就会发现这个方法里面只有很少的几行代码。只是初始化了一个QuadCommand,然后把这个对象添加到render中,然后就在队列中等待绘制,这个改进会让游戏更加流畅
2.3.x中Shader的使用。
- 1.声明一个自定义的CustomCommand对象,然后再draw里面进行初始化,记得指定_globalZOrder。
_customCommand.func = CC_CALLBACK_0(BBSpriteEmboos::onDraw, this, transform, flags);
renderer->addCommand(&_customCommand);
bool BBSpriteEmboos::initWithTexture(CCTexture2D *pTexture, const CCRect& rect) {
do{
// CCLog("override initWithTexture!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, rect));
GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
CCFileUtils::sharedFileUtils()->fullPathForFilename("res/plg/effect/BBSpriteEmboos.fsh").c_str())->getCString();
// 加载顶点着色器和片元着色器
m_pShaderProgram = new CCGLProgram();
m_pShaderProgram ->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, fragSource);
this->setShaderProgram(m_pShaderProgram);
CHECK_GL_ERROR_DEBUG();
// 启用顶点着色器的attribute变量,坐标、纹理坐标、颜色
m_pShaderProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
m_pShaderProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
m_pShaderProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
CHECK_GL_ERROR_DEBUG();
// 自定义着色器链接
m_pShaderProgram->link();
CHECK_GL_ERROR_DEBUG();
// 设置移动、缩放、旋转矩阵
m_pShaderProgram->updateUniforms();
CHECK_GL_ERROR_DEBUG();
return true;
}while(0);
return false;
}
// 获取GL程序
auto glProgram = getGLProgram();
glProgram->use();
// 设置变换矩阵,旋转平移等
glProgram->setUniformsForBuiltins(transform);
// 开启顶点属性
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
ccBlendFunc blend = getBlendFunc();
ccGLBlendFunc(blend.src, blend.dst);
ccGLBindTexture2D( getTexture()->getName());
//
// Attributes
//
#define kQuadSize sizeof(_quad.bl)
long offset = (long)&_quad;
// vertex
int diff = offsetof( ccV3F_C4B_T2F, vertices);
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texCoods
diff = offsetof( ccV3F_C4B_T2F, texCoords);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
// color
diff = offsetof( ccV3F_C4B_T2F, colors);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CC_INCREMENT_GL_DRAWS(1);