【OSG案例详细分析与讲解】之三:【3D场景优化与分页加载】

文章目录

一、【3D场景优化与分页加载】前言

二、【3D场景优化与分页加载】简化场景和优化渲染

三、【3D场景优化与分页加载】场景纹理图像优化处理

1.代码

2.成效

四、【3D场景优化与分页加载】几何节点数组交换

五、【3D场景优化与分页加载】分析和修改场景图内存

六、【3D场景优化与分页加载】分页数据库操作

七、【3D场景优化与分页加载】总结


一、【3D场景优化与分页加载】前言

随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

二、【3D场景优化与分页加载】简化场景和优化渲染

class StripStateVisitor : public osg::NodeVisitor
{
public:
    StripStateVisitor(bool useStateSets, bool useDisplayLists, bool useVBO):
        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
        _useStateSets(useStateSets),
        _useDisplayLists(useDisplayLists),
        _useVBO(useVBO) {}

    bool _useStateSets;
    bool _useDisplayLists;
    bool _useVBO;

    void apply(osg::Node& node)
    {
        if (!_useStateSets && node.getStateSet()) node.setStateSet(0);
        traverse(node);
    }

    void apply(osg::Drawable& drawable)
    {
        if (!_useStateSets && drawable.getStateSet())
        {
            drawable.setStateSet(0);
        }

        drawable.setUseDisplayList(_useDisplayLists);
        drawable.setUseVertexBufferObjects(_useVBO);
    }
};


三、【3D场景优化与分页加载】场景纹理图像优化处理

1.代码

代码如下(示例):

class OptimizeImageVisitor : public osg::NodeVisitor
{
public:
    OptimizeImageVisitor(osgDB::ImageProcessor* imageProcessor, bool compressImages, bool generateMipmaps):
        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
        _imageProcessor(imageProcessor),
        _compressImages(compressImages),
        _generateMipmaps(generateMipmaps) {}

    osg::ref_ptr _imageProcessor;
    bool _compressImages;
    bool _generateMipmaps;

    void apply(osg::Node& node)
    {
        processStateSet(node.getStateSet());
        traverse(node);
    }

    void apply(osg::Geode& node)
    {
        processStateSet(node.getStateSet());
        for(unsigned int i = 0; igetStateSet());
        }

        traverse(node);
    }

    void processStateSet(osg::StateSet* stateset)
    {
        if (!stateset) return;

        for(unsigned int ti=0; tigetNumTextureAttributeLists(); ++ti)
        {
            osg::StateAttribute* sa = stateset->getTextureAttribute(ti, osg::StateAttribute::TEXTURE);
            osg::Texture* texture = dynamic_cast(sa);
            if (texture)
            {
                for(unsigned int i=0; igetNumImages(); ++i)
                {
                    proccessImage(texture->getImage(i));
                }
            }
        };
    }


    void proccessImage(osg::Image* image)
    {
        if (!image) return;

        if (_imageProcessor.valid())
        {
            OSG_NOTICE<<"Will be using ImageProcessor to process image "<getFileName()<getFileName()<

2.成效

代码如下(示例):

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

该处使用的url网络请求的数据。

四、【3D场景优化与分页加载】几何节点数组交换

class SwapArrayVisitor : public osg::ArrayVisitor
{
public:
    SwapArrayVisitor(osg::Array* array):
        _array(array) {}

    template 
    void apply_imp(ARRAY& array)
    {
        if (array.getType()!=_array->getType())
        {
            OSG_NOTICE<<"Arrays incompatible"<(_array));
    }

    virtual void apply(osg::ByteArray& ba) { apply_imp(ba); }
    virtual void apply(osg::ShortArray& ba) { apply_imp(ba); }
    virtual void apply(osg::IntArray& ba) { apply_imp(ba); }
    virtual void apply(osg::UByteArray& ba) { apply_imp(ba); }
    virtual void apply(osg::UShortArray& ba) { apply_imp(ba); }
    virtual void apply(osg::UIntArray& ba) { apply_imp(ba); }
    virtual void apply(osg::Vec4ubArray& ba) { apply_imp(ba); }
    virtual void apply(osg::FloatArray& ba) { apply_imp(ba); }
    virtual void apply(osg::Vec2Array& ba) { apply_imp(ba); }
    virtual void apply(osg::Vec3Array& ba) { apply_imp(ba); }
    virtual void apply(osg::Vec4Array& ba) { apply_imp(ba); }

    osg::Array* _array;
};

五、【3D场景优化与分页加载】分析和修改场景图内存

class MemoryVisitor : public osg::NodeVisitor
{
public:
     MemoryVisitor():
         osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}


    void reset()
    {
         _nodes.clear();
         _geometryMap.clear();
         _arrayMap.clear();
         _primitiveSetMap.clear();
    }

    void apply(osg::Node& node)
    {
        _nodes.insert(&node);
        traverse(node);
    }

    void apply(osg::Geode& geode)
    {
        _nodes.insert(&geode);
        for(unsigned int i=0; iasGeometry();
        if (geometry)
        {
            _geometryMap[geometry].insert(geode);

            apply(geometry, geometry->getVertexArray());
            apply(geometry, geometry->getNormalArray());
            apply(geometry, geometry->getColorArray());
            apply(geometry, geometry->getSecondaryColorArray());
            apply(geometry, geometry->getFogCoordArray());

            for(unsigned int i=0; igetNumTexCoordArrays(); ++i)
            {
                apply(geometry, geometry->getTexCoordArray(i));
            }
            for(unsigned int i=0; igetNumVertexAttribArrays(); ++i)
            {
                apply(geometry, geometry->getVertexAttribArray(i));
            }

            for(unsigned int i=0; igetNumPrimitiveSets(); ++i)
            {
                apply(geometry, geometry->getPrimitiveSet(i));
            }
        }
    }

    void apply(osg::Geometry* geometry, osg::Array* array)
    {
        if (!array) return;
        _arrayMap[array].insert(geometry);
    }

    void apply(osg::Geometry* geometry, osg::PrimitiveSet* primitiveSet)
    {
        if (!primitiveSet) return;
        _primitiveSetMap[primitiveSet].insert(geometry);
    }

    void report(std::ostream& out)
    {
        out<<"Nodes "<<_nodes.size()< > GeometryVector;
        GeometryVector newGeometries;
        for(GeometryMap::iterator itr = _geometryMap.begin();
            itr != _geometryMap.end();
            ++itr)
        {
            osg::Geometry* geometry = itr->first;
            bool useVBO = geometry->getUseVertexBufferObjects();
            osg::Geometry* newGeometry = osg::clone(geometry, osg::CopyOp(osg::CopyOp::DEEP_COPY_ALL));
            newGeometry->setUseVertexBufferObjects(false);
            newGeometry->setUseVertexBufferObjects(useVBO);
            newGeometries.push_back(newGeometry);
        }

        GeometryVector::iterator geom_itr = newGeometries.begin();
        for(GeometryMap::iterator itr = _geometryMap.begin();
            itr != _geometryMap.end();
            ++itr, ++geom_itr)
        {
            osg::Geometry* geometry = itr->first;
            Geodes& geodes = itr->second;
            for(Geodes::iterator gitr = geodes.begin();
                gitr != geodes.end();
                ++gitr)
            {
                osg::Geode* geode = const_cast(*gitr);
                geode->replaceDrawable(geometry, geom_itr->get());
            }
        }
    }

    typedef std::vector< osg::ref_ptr > GeometryVector;
    typedef std::pair ArrayPair;
    typedef std::vector< ArrayPair > ArrayVector;
    typedef std::pair PrimitiveSetPair;
    typedef std::vector< PrimitiveSetPair > PrimitiveSetVector;

    osg::Array* cloneArray(ArrayVector& arrayVector, osg::Array* array)
    {
        if (!array) return 0;
        osg::Array* newArray = static_cast(array->cloneType());
        arrayVector.push_back(ArrayPair(array,newArray));
        return newArray;
    }

    osg::PrimitiveSet* clonePrimitiveSet(PrimitiveSetVector& psVector, osg::PrimitiveSet* ps)
    {
        if (!ps) return 0;
        osg::PrimitiveSet* newPS = static_cast(ps->cloneType());
        psVector.push_back(PrimitiveSetPair(ps,newPS));
        return newPS;
    }

    void reallocate2()
    {
        OSG_NOTICE<<"Reallocating Arrays"<first;
            osg::ref_ptr newGeometry = osg::clone(geometry, osg::CopyOp::SHALLOW_COPY);
            newGeometries.push_back(newGeometry.get());

            newGeometry->setVertexArray(cloneArray(arrayVector, geometry->getVertexArray()));
            newGeometry->setNormalArray(cloneArray(arrayVector, geometry->getNormalArray()));
            newGeometry->setColorArray(cloneArray(arrayVector, geometry->getColorArray()));
            newGeometry->setSecondaryColorArray(cloneArray(arrayVector, geometry->getSecondaryColorArray()));
            newGeometry->setFogCoordArray(cloneArray(arrayVector, geometry->getFogCoordArray()));
            for(unsigned int i=0; igetNumTexCoordArrays(); ++i)
            {
                newGeometry->setTexCoordArray(i, cloneArray(arrayVector, geometry->getTexCoordArray(i)));
            }
            for(unsigned int i=0; igetNumVertexAttribArrays(); ++i)
            {
                newGeometry->setVertexAttribArray(i, cloneArray(arrayVector, geometry->getVertexAttribArray(i)));
            }

            for(unsigned int i=0; igetNumPrimitiveSets(); ++i)
            {
                newGeometry->setPrimitiveSet(i,clonePrimitiveSet(primitiveSetVector, geometry->getPrimitiveSet(i)));
            }
        }

        GeometryVector::iterator geom_itr = newGeometries.begin();
        for(GeometryMap::iterator itr = _geometryMap.begin();
            itr != _geometryMap.end();
            ++itr, ++geom_itr)
        {
            osg::Geometry* geometry = itr->first;
            Geodes& geodes = itr->second;
            for(Geodes::iterator gitr = geodes.begin();
                gitr != geodes.end();
                ++gitr)
            {
                osg::Geode* geode = const_cast(*gitr);
                geode->replaceDrawable(geometry, geom_itr->get());
            }
        }
    }

protected:

     typedef std::set  Nodes;
     typedef std::set  Geodes;
     typedef std::set  Geometries;
     typedef std::map GeometryMap;
     typedef std::map ArrayMap;
     typedef std::map PrimitiveSetMap;

     Nodes              _nodes;
     GeometryMap        _geometryMap;
     ArrayMap           _arrayMap;
     PrimitiveSetMap    _primitiveSetMap;
};

六、【3D场景优化与分页加载】分页数据库操作

class DatabasePagingOperation : public osg::Operation, public osgUtil::IncrementalCompileOperation::CompileCompletedCallback
{
public:

    DatabasePagingOperation(const std::string& filename,
                            const std::string& outputFilename,
                             SceneGraphProcessor* sceneGraphProcessor,
                             osgUtil::IncrementalCompileOperation* ico):
        osg::Referenced(true),
        Operation("DatabasePaging Operation", false),
        _filename(filename),
        _outputFilename(outputFilename),
        _modelReadyToMerge(false),
        _sceneGraphProcessor(sceneGraphProcessor),
        _incrementalCompileOperation(ico)
        {
        }

    virtual void operator () (osg::Object* /*object*/)
    {
        osg::notify(osg::NOTICE)<<"LoadAndCompileOperation "<<_filename<process(_loadedModel.get());
            }
        }

        if (_loadedModel.valid())
        {
            if (!_outputFilename.empty())
            {
                OSG_NOTICE<<"Writing out file "<<_outputFilename< compileSet =
                    new osgUtil::IncrementalCompileOperation::CompileSet(_loadedModel.get());

                compileSet->_compileCompletedCallback = this;

                _incrementalCompileOperation->add(compileSet.get());
            }
            else
            {
                _modelReadyToMerge = true;
            }
        }

        osg::notify(osg::NOTICE)<<"done LoadAndCompileOperation "<<_filename<                             _loadedModel;
    bool                                                _modelReadyToMerge;
    osg::ref_ptr                   _sceneGraphProcessor;
    osg::ref_ptr  _incrementalCompileOperation;
};


七、【3D场景优化与分页加载】总结

例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

你可能感兴趣的:(OSG案例详细分析与讲解,osg,visitor)