码迷,mamicode.com
首页 > 其他好文 > 详细

osg 纹理访问器

时间:2015-02-06 16:24:50      阅读:609      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

osg 纹理访问器

标签:

原文地址:http://www.cnblogs.com/coolbear/p/4277340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!