查看: 1211|回复: 2

请教打开 osg 模型文件 问题

[复制链接]

该用户从未签到

发表于 2011-7-20 00:00:57 | 显示全部楼层 |阅读模式
#include <iostream>
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/StateSetManipulator>
#include <osgUtil/SmoothingVisitor>
#include <osg/io_utils>
#include <osgAnimation/MorphGeometry>
#include <osgAnimation/BasicAnimationManager>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
struct GeometryFinder : public osg::NodeVisitor
{
    osg::ref_ptr<osg::Geometry> _geom;
    GeometryFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
    void apply(osg::Geode& geode)
    {
        if (_geom.valid())
            return;
        for (unsigned int i = 0; i < geode.getNumDrawables(); i++)
        {
            osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
            if (geom) {
                _geom = geom;
                return;
            }
        }
    }
};
osg::ref_ptr<osg::Geometry> getShape(const std::string& name)
{
    osg::ref_ptr<osg::Node> shape0 = osgDB::readNodeFile(name);
    if (shape0)
    {
        GeometryFinder finder;
        shape0->accept(finder);
        return finder._geom;
    }
    else
    {
        return NULL;
    }
}

int main (int argc, char* argv[])
{
    osg::ArgumentParser arguments(&argc, argv);
    osgViewer::Viewer viewer(arguments);
    osgAnimation::Animation* animation = new osgAnimation::Animation;
    osgAnimation::FloatLinearChannel* channel0 = new osgAnimation::FloatLinearChannel;
    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1,1.0));
    channel0->setTargetName("MorphNodeCallback");
    channel0->setName("0");
    animation->addChannel(channel0);
    animation->setName("Morph");
    animation->computeDuration();
    animation->setPlayMode(osgAnimation::Animation:PONG);
    osgAnimation::BasicAnimationManager* bam = new osgAnimation::BasicAnimationManager;
    bam->registerAnimation(animation);

    // osg::ref_ptr<osg::Geometry> geom0 = getShape("spaceship.osg");
    // osg::ref_ptr<osg::Geometry> geom0 = getShape("cow.osg");
    // osg::ref_ptr<osg::Geometry> geom0 = getShape("glider.osg");

    osg::ref_ptr<osg::Geometry> geom0 = getShape("lz.osg");
    if (!geom0) {
        std::cerr << "can't read morphtarget_shape0.osg" << std::endl;
        return 0;
    }

    // osg::ref_ptr<osg::Geometry> geom0 = getShape("spaceship.osg");
    // osg::ref_ptr<osg::Geometry> geom0 = getShape("cow.osg");
    // osg::ref_ptr<osg::Geometry> geom0 = getShape("glider.osg");


     osg::ref_ptr<osg::Geometry> geom1 = getShape("lz.osg");
    if (!geom1) {
        std::cerr << "can't read morphtarget_shape1.osg" << std::endl;
        return 0;
    }
    // initialize with the first shape
    osgAnimation::MorphGeometry* morph = new osgAnimation::MorphGeometry(*geom0);
    morph->addMorphTarget(geom1.get());
    viewer.setCameraManipulator(new osgGA::TrackballManipulator());

    osg::Group* scene = new osg::Group;
    scene->addUpdateCallback(bam);
   
    osg::Geode* geode = new osg::Geode;
    geode->addDrawable(morph);
    geode->addUpdateCallback(new osgAnimation::UpdateMorph("MorphNodeCallback"));
    scene->addChild(geode);
    viewer.addEventHandler(new osgViewer::StatsHandler());
    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
    // let's run !
    viewer.setSceneData( scene );
    viewer.realize();
    bam->playAnimation(animation);

    while (!viewer.done())
    {
        viewer.frame();
    }
    osgDB::writeNodeFile(*scene, "morph_scene.osg");
    return 0;
}

红字部分是我更改不同模型
glider.osg cow.osg 可以正常加载;
spaceship.osg 只能加载部分;
lz.osg 则不能正常加载,请指教
            

该用户从未签到

发表于 2011-7-20 02:05:41 | 显示全部楼层
。。。。。。。。。。。不知道楼主咋想的,建议下学习场景图基本知识

这样加载吧

  1. osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");
复制代码

该用户从未签到

发表于 2011-7-20 08:31:23 | 显示全部楼层
谁告诉您每个模型只有一个geometry的……
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表