osg渲染属性和opengl的接口分析

inline void State::applyModeList(ModeMap& modeMap,const StateSet::ModeList& modeList)//对每一个渲染叶都调用一次
{
    StateSet::ModeList::const_iterator ds_mitr = modeList.begin();
    ModeMap::iterator this_mitr=modeMap.begin();

    while (this_mitr!=modeMap.end() && ds_mitr!=modeList.end())   //modeList第二个变量是0,或者1,表示stateset::ON或者stateset::Off
    {//从_modelList第一位遍历,分大于,等于,小于,由于firt是已经经过排序,所以可以分位三种情况考虑
        if (this_mitr->first<ds_mitr->first)  因为this_mitr->first已经经过排序,this_mitr->first(最小的)小于ds_mitr->first,但ds_mitr->first可能与其它的
//this_mitr->first相等,所以ds_mitr->first可能在ModeList,也可能不在modelList
        {

            // note GLMode = this_mitr->first
            ModeStack& ms = this_mitr->second;//modeMap。已经保存的确modeList第一个分量已经排过序
            if (ms.changed)//处理的是this_mitr->first,属于_modeMap并传递给OpenGL, 只要改变的时候才能传递,change表示是否需要传递给openGL
            {
                ms.changed = false;//ms.changed是真的,说明已经处理过
                if (!ms.valueVec.empty())//ms.valueVec是非空的
                {
                    bool new_value = ms.valueVec.back() & StateAttribute::ON;
                    applyMode(this_mitr->first,new_value,ms);
                }
                else//ms.valueVect是空的说明是在本函数中插入的
                {
                    // assume default of disabled.
                    applyMode(this_mitr->first,ms.global_default_value,ms);

                }

            }

            ++this_mitr;//增加this_mitr,重新遍历

        }
        else if (ds_mitr->first<this_mitr->first)//由于this_mitr->first是最小的,所以ds_mitr->first肯定小于所有的确modMap,所以一定是一个新的mode
        {

            // ds_mitr->first is a new mode, therefore
            // need to insert a new mode entry for ds_mistr->first.
            ModeStack& ms = modeMap[ds_mitr->first];//插入到this_mitr->first前

            bool new_value = ds_mitr->second & StateAttribute::ON;//求与操作,是on或者off
            applyMode(ds_mitr->first,new_value,ms);

            // will need to disable this mode on next apply so set it to changed.
            ms.changed = true;

            ++ds_mitr;

        }
        else//如果相等,则mode是相同的,检查override性
        {
            // this_mitr & ds_mitr refer to the same mode, check the override
            // if any otherwise just apply the incoming mode.

            ModeStack& ms = this_mitr->second;
            //当前modmap是OVERRIDE,而当前modeList是PROJECTED,属于父节点,并且在收集状态的阶段已经push_back到valueVec里面,并且ds_mitr没有
保护
            if (!ms.valueVec.empty() && (ms.valueVec.back() & StateAttribute::OVERRIDE) && !(ds_mitr->second & StateAttribute::PROTECTED))
            {
                // override is on, just treat as a normal apply on modes.

                if (ms.changed)//上一次已经插入新的,并改变了
                {
                    ms.changed = false;
                    bool new_value = ms.valueVec.back() & StateAttribute::ON;
                    applyMode(this_mitr->first,new_value,ms);

                }
            }
            else
            {
                // no override on or no previous entry, therefore consider incoming mode.由于valueVec.empty() 是个空的,说明不是在pushState()里面用插入的
                bool new_value = ds_mitr->second & StateAttribute::ON;//如果是protected类型,则直接使用当前的类型即可,父节点的被重复
                if (applyMode(ds_mitr->first,new_value,ms))//如果返回false则传递给opengl,否则不传递给opengl,因为上次已经传递过了
                {返回真说明已经glEnable或者glDisable.
                    ms.changed = true;
                }
            }

            ++this_mitr;
            ++ds_mitr;
        }
    }
    经过以上遍历,modeMap或者modelList已经遍历完
    // iterator over the remaining state modes to apply any previous changes.//遍历完毕后应用剩余的osg属性
    for(;
        this_mitr!=modeMap.end();
        ++this_mitr)
    {
        // note GLMode = this_mitr->first
        ModeStack& ms = this_mitr->second;
        if (ms.changed)
        {
            ms.changed = false;
            if (!ms.valueVec.empty())
            {
                bool new_value = ms.valueVec.back() & StateAttribute::ON;
                applyMode(this_mitr->first,new_value,ms);
            }
            else
            {
                // assume default of disabled.
                applyMode(this_mitr->first,ms.global_default_value,ms);

            }

        }
    }

    // iterator over the remaining incoming modes to apply any new mode.
    for(;
        ds_mitr!=modeList.end();
        ++ds_mitr)
    {
        ModeStack& ms = modeMap[ds_mitr->first];//global_default_value  ...opengl默认打开或者关闭的值

        bool new_value = ds_mitr->second & StateAttribute::ON;
        applyMode(ds_mitr->first,new_value,ms);

        // will need to disable this mode on next apply so set it to changed.
        ms.changed = true;
    }
}
原文地址:https://www.cnblogs.com/lizhengjin/p/1834039.html