Briefly describe the rendering process of CCNode and Sprite
When a Sprite is created, the texture is loaded to generate the texture id. Draw in the Sprite override Node
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 _debugDrawNode->clear(); Vec2 vertices[4] = { Vec2( _quad.bl.vertices.x, _quad.bl.vertices.y ), Vec2( _quad.br.vertices.x, _quad.br.vertices.y ), Vec2( _quad.tr.vertices.x, _quad.tr.vertices.y ), Vec2( _quad.tl.vertices.x, _quad.tl.vertices.y ), }; _debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));#endif //CC_SPRITE_DEBUG_DRAW }}
Add the corresponding vertex information, texture id, and view matrix to the render queue. When will the draw function be called? See the visit function in Node.
void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags){ // quick return if not visible. children won't be drawn. if (!_visible) { return; } uint32_t flags = processParentFlags(parentTransform, parentFlags); // IMPORTANT: // To ease the migration to v3.0, we still support the Mat4 stack, // but it is deprecated and your code should not rely on it Director* director = Director::getInstance(); director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform); bool visibleByCamera = isVisitableByVisitingCamera(); int i = 0; if(!_children.empty()) { sortAllChildren(); // draw children zOrder < 0 for( ; i < _children.size(); i++ ) { auto node = _children.at(i); if ( node && node->_localZOrder < 0 ) node->visit(renderer, _modelViewTransform, flags); else break; } // self draw if (visibleByCamera) this->draw(renderer, _modelViewTransform, flags); for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) (*it)->visit(renderer, _modelViewTransform, flags); } else if (visibleByCamera) { this->draw(renderer, _modelViewTransform, flags); } director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); // FIX ME: Why need to set _orderOfArrival to 0?? // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920 // reset for next frame // _orderOfArrival = 0;}
Visit first arranges subnodes according to zorder. It first calls the draw function of the subnode whose zorder is greater than 0, then calls its own draw function, and then calls the draw function of the subnode whose zorder is less than 0, during the final rendering, all nodes are rendered in the order of the queue in the render.
Void Director: drawScene () {if (_ runningScene) {// clear draw stats _ renderer-> clearDrawStats (); // render the scene _ runningScene-> render (_ renderer); --------- call the visit and draw functions of all nodes, add the node rendering information to the rendering queue _ eventDispatcher-> dispatchEvent (_ eventAfterVisit);} // draw the events node if (_ icationicationnode) {_ icationicationnode-> visit (_ renderer, mat4: IDENTITY, 0);} if (_ displayStats) {showStats ();} _ renderer-> render (); ---------------- render all nodes