• osg 纹理访问器


    #include<osgViewer/Viewer>

    #include<osg/Node>
    #include<osg/Geode>
    #include<osg/Group>
    #include<osg/NodeVisitor>

    #include<osgDB/ReadFile>
    #include<osgDB/WriteFile>

    #include<osgUtil/Optimizer>

    #include<iostream>
    #include<string>
    #include<map>

    class TextureVisitor:public osg::NodeVisitor
    {
    public:
        TextureVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
        {
        }
        virtual void apply(osg::Node& node)
        {
            if(node.getStateSet())
            {
                apply(node.getStateSet());
            }
            traverse(node);
        }
        virtual void apply(osg::Geode& geode)
        {
            if(geode.getStateSet())
            {
                apply(geode.getStateSet());
            }
            unsigned int cnt = geode.getNumDrawables();
            for(unsigned int i = 0; i < cnt; i++)
            {
                apply(geode.getDrawable(i)->getStateSet());
            }
            traverse(geode);
        }
        void apply(osg::StateSet* state)
        {
            osg::StateSet::TextureAttributeList& texAttribList = state->getTextureAttributeList();
            for(unsigned int i = 0; i < texAttribList.size(); i++)
            {
                osg::Texture2D* tex2D = NULL;
                if(tex2D = dynamic_cast<osg::Texture2D*>(state->getTextureAttribute(i, osg::StateAttribute::TEXTURE)))
                {
                    if(tex2D->getImage())
                    {
                        _imageList.insert(std::make_pair(tex2D->getImage()->getFileName(), tex2D->getImage()));

                    }
                }
            }

        }
        std::map<std::string, osg::Image*>& getImages(void)
        {
            return _imageList;
        }
    protected:
        std::map<std::string, osg::Image*> _imageList;
    };
    int main()
    {
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
        osg::ref_ptr<osg::Group> root = new osg::Group();
        osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");

        TextureVisitor textureTV;
        node->accept(textureTV);
        std::map<std::string, osg::Image*> imageList = textureTV.getImages();

        std::map<std::string, osg::Image*>::iterator iter = imageList.begin();
        unsigned int cnt = 0;
        char* buffer = new char[2000];
        for(; iter != imageList.end(); iter++)
        {
            sprintf(buffer, "d:/TextureImage%d.jpg", cnt++);
            osgDB::writeImageFile(*(iter->second),buffer);
        } 
        root->addChild(node.get());
        osgUtil::Optimizer optimizer;
        optimizer.optimize(root.get());
        viewer->setSceneData(root.get());
        viewer->realize();
        viewer->run();
        return 0;
    }

  • 相关阅读:
    试述软件的概念和特点?软件复用的含义?构件包括哪些?
    Spring Security基本用法
    java中跳出循环的方式
    cookie和session区别
    spring中类型注解下的bean的加载顺序
    常见的异常
    aop使用场景
    缓存类似于redis
    旧版redis使用
    获取rdis的几种方式
  • 原文地址:https://www.cnblogs.com/coolbear/p/4277340.html
Copyright © 2020-2023  润新知