溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

簡述CCNode和Sprite的渲染過程

發(fā)布時間:2020-04-01 06:08:53 來源:網絡 閱讀:478 作者:螞蟻雄心 欄目:開發(fā)技術

Sprite創(chuàng)建時,加載紋理,產生紋理id。Sprite重寫(override)Node中的draw

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

    }

}

將對應的頂點信息,紋理id,視圖矩陣加入render隊列。

draw函數在什么時候被調用?

請看Node中的visit函數

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首先根據zorder排列子節(jié)點,先調用zorder小于0的子節(jié)點的draw函數,再調用自身的draw,接著再調用zorder大于0的子節(jié)點的draw函數,最后渲染時,按照render中隊列的先后順序,渲染所有節(jié)點。

void Director::drawScene()

  if (_runningScene)

    {

        //clear draw stats

        _renderer->clearDrawStats();

        

        //render the scene

        _runningScene->render(_renderer);---------調用所有節(jié)點的visit和draw函數,添加節(jié)點渲染信息到渲染隊列中

        

        _eventDispatcher->dispatchEvent(_eventAfterVisit);

    }


    // draw the notifications node

    if (_notificationNode)

    {

        _notificationNode->visit(_renderer, Mat4::IDENTITY, 0);

    }


    if (_displayStats)

    {

        showStats();

    }

    _renderer->render();------------------渲染所有節(jié)點

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI