cocos2dx auto culling 和 auto batching
(1)drawScene开始绘制场景
(2)遍历场景的子节点,调用visit函数,递归遍历子节点的子节点,以及子节点的子节点的子节点,以及…
(4)初始化QuadCommand对象,这就是渲染命令,会丢到渲染队列里
(5)丢完QuadCommand就完事了,接着就交给渲染逻辑处理了。
auto culling 的实现见如下代码:void Sprite::draw(Renderer *renderer,const Mat4 &transform,uint32_t flags) { // Don't do calculate the culling if the transform was not updated _insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transform,_contentSize) : _insideBounds; if(_insideBounds) { _quadCommand.init(_globalZOrder,_texture->getName(),getGLProgramState(),_blendFunc,&_quad,1,transform); renderer->addCommand(&_quadCommand); #if CC_SPRITE_DEBUG_DRAW _customDebugDrawCommand.init(_globalZOrder); _customDebugDrawCommand.func = CC_CALLBACK_0(Sprite::drawDebugData,this); renderer->addCommand(&_customDebugDrawCommand); #endif //CC_SPRITE_DEBUG_DRAW } }
在加入渲染队列前,它会做一个检测,当前的精灵是否在可见范围,如果是才加入渲染队列。如果不是则抛弃。
auto batching见图。
渲染队列里有2个A,2个B,3个A,渲染顺序就是2个A一次渲染,2个B一次渲染,3个A一次渲染。
代码如下:
//Start drawing verties in batch for(const auto& cmd : _batchedQuadCommands) { auto newMaterialID = cmd->getMaterialID(); if(_lastMaterialID != newMaterialID || newMaterialID == QuadCommand::MATERIAL_ID_DO_NOT_BATCH) { //Draw quads if(quadsToDraw > 0) { glDrawElements(GL_TRIANGLES,(GLsizei) quadsToDraw*6,GL_UNSIGNED_SHORT,(GLvoid*) (startQuad*6*sizeof(_indices[0])) ); _drawnBatches++; _drawnVertices += quadsToDraw*6; startQuad += quadsToDraw; quadsToDraw = 0; } //Use new material cmd->useMaterial(); _lastMaterialID = newMaterialID; } quadsToDraw += cmd->getQuadCount(); } //Draw any remaining quad if(quadsToDraw > 0) { glDrawElements(GL_TRIANGLES,(GLvoid*) (startQuad*6*sizeof(_indices[0])) ); _drawnBatches++; _drawnVertices += quadsToDraw*6; }auto batching还有另外一种使用。比如渲染字体。文字的长度决定了一次渲染多少个quad。所以renderCommand有一个参数是quad个数。_quadCommand.init 倒数第二个参数。这样也能实现一次性渲染多个文字。