#include <osgViewer/Viewer>
#include 
<osg/Node>
#include 
<osg/Geometry>
#include 
<osg/Geode>
#include 
<osg/Group>
#include 
<osg/Texture2D>
#include 
<osg/TexEnv>
#include 
<osg/TexGen>

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

#include 
<osgUtil/Optimizer>

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("D:\\my work\\aerolite\\aerolite-0.10\\bin\\cow.osg");

    osg::ref_ptr
<osg::Image> image=osgDB::readImageFile("D:\\");

    
if (image.get())
    {
        
//创建二维纹理
        osg::ref_ptr<osg::Texture2D> texture=new osg::Texture2D;
        texture
->setImage(image.get());

        
//设置自动生成坐标
        osg::ref_ptr<osg::TexGen> texgen=new osg::TexGen;
        texgen
->setMode(osg::TexGen::SPHERE_MAP);

        
//设置纹理环境
        osg::ref_ptr<osg::TexEnv> texenv=new osg::TexEnv;
        texenv
->setMode(osg::TexEnv::REPLACE);//替换物体贴图

        
//设置blend操作颜色
        texenv->setColor(osg::Vec4(0.6,0.6,0.6,0.0));

        
//启用单元一自动生成纹理坐标,并使用纹理

        osg::ref_ptr
<osg::StateSet> stateset=new osg::StateSet;
        stateset
->setTextureAttributeAndModes(1,texture.get(),osg::StateAttribute::ON);
        stateset
->setTextureAttributeAndModes(1,texgen.get(),osg::StateAttribute::ON);

        
//设置纹理环境
        stateset->setTextureAttribute(1,texenv.get());

        
//设置物体状态
        node->setStateSet(stateset.get());
    }

    root
->addChild(node.get());

    viewer
->setSceneData(root.get());

    viewer
->realize();

    viewer
->run();

    
return 0;
}
posted on 2011-04-01 15:05  jackdesk  阅读(3355)  评论(0编辑  收藏  举报