OSG的“hello world”

#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osg/AnimationPath>

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

#include <osgUtil/Optimizer>

osg::ref_ptr<osg::Node> MatrixOperation()
{
    osg::ref_ptr<osg::Group> group = new osg::Group();
    osg::ref_ptr<osg::MatrixTransform> matrix = new osg::MatrixTransform();
    osg::ref_ptr<osg::MatrixTransform> matrix1 = new osg::MatrixTransform();
    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("glider.osg");

    matrix->addChild(osgDB::readNodeFile("glider.osg"));
    //matrix->setMatrix(osg::Matrix::translate(5.0, 0.0, 0.0));
    matrix->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(6.0, 0.0, 0.0), osg::Z_AXIS, 1.0));
    matrix->addChild(matrix1.get());

    matrix1->addChild(node.get());
    matrix1->setMatrix(osg::Matrix::translate(5.0, 0.0, 0.0));


    //group->addChild(osgDB::readNodeFile("glider.osg"));
    group->addChild(node.get());
    group->addChild(matrix.get());

    return group;
}


int main()
{
    //创建场景对象  场景浏览器
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    //创建场景组节点
    osg::ref_ptr<osg::Group>  group = new osg::Group;
    //创建一个节点 ,读取牛得模型
    osg::ref_ptr<osg::Node>  node = osgDB::readNodeFile("cow.osg");
    //添加到场景
    group->addChild(node);
    //优化场景数据
    osgUtil::Optimizer optimizer;
    optimizer.optimize(group.get());

    //设置场景数据
    viewer->setSceneData(MatrixOperation().get());

    //初始化并创建窗口
    viewer->realize();
    //开始渲染
    return viewer->run();

}

猜你喜欢

转载自www.cnblogs.com/linxuemufeng/p/11235156.html