OSG的“hello world”

本文展示了一个使用OSG进行三维动画路径设置的例子。通过MatrixTransform和AnimationPathCallback结合使用,实现了模型沿指定路径移动的效果,并对场景进行了优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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();

}

 

转载于:https://www.cnblogs.com/linxuemufeng/p/11235156.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值