zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    使用bootstrap建立响应式网页——通栏轮播图(carousel)
    java的HashMap与ConcurrentHashMap
    JVM性能调优
    Spring注解@Component、@Repository、@Service、@Controller区别
    Java过滤器与SpringMVC拦截器之间的关系与区别
    Java线程(二):线程同步synchronized和volatile
    Java线程(一):线程安全与不安全
    MySQL 加锁处理分析
    Spring 事务机制详解
    使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
  • 原文地址:https://www.cnblogs.com/coolbear/p/4277340.html
Copyright © 2011-2022 走看看