查看: 800|回复: 1

参照网上下的动画例子做动画,总提示animation.exe已退出工作

[复制链接]

该用户从未签到

发表于 2014-5-4 16:59:44 | 显示全部楼层 |阅读模式
我想做一条直线变形的动画,于是在网上下了对折硬纸的动画源代码,参照那个写直线变形的代码,却总是执行时提示animation.exe已退出工作,请关闭程序,而网上下的代码运行时则没有问题,是我写的代码不对吗,请大神赐教,看我哪里写的不对。
#include <osg/Group>
#include <osgAnimation/MorphGeometry>
#include <osgAnimation/BasicAnimationManager>
#include <osgDB/ReadFile>
#include <osgUtil/SmoothingVisitor>
#include <osgViewer/Viewer>
#include <osg/LineWidth>
#pragma comment( lib, "opengl32.lib")
#include <osg/Geometry>

osg::Geometry* createSourceGeometry()
{
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back( osg::Vec3(0,0,0) );
    vertices->push_back( osg::Vec3(5,0,0) );
   
   
    osg::ref_ptr<osg::Geometry> line = new osg::Geometry;
        line->setVertexArray(vertices);
        line ->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
        line->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet:INES, 0, vertices->size()));

        //线宽        
        osg::LineWidth * linewidth = new osg::LineWidth;
        linewidth->setWidth(10.0);
        line->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
   
    osgUtil::SmoothingVisitor smv;
    smv.smooth( *line );
    return line.release();
}

osg::Geometry* createTargetGeometry()
{
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back( osg::Vec3(0,0,0) );
    vertices->push_back( osg::Vec3(4.8,0.12,0) );
   
   
   osg::ref_ptr<osg::Geometry> line = new osg::Geometry;
        line->setVertexArray(vertices);
        line ->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
        line->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size()));

        //线宽        
        osg::LineWidth * linewidth = new osg::LineWidth;
        linewidth->setWidth(10.0);
        line->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
   
    osgUtil::SmoothingVisitor smv;
    smv.smooth( *line );
    return line.release();
}

void createMorphKeyframes( osgAnimation::FloatKeyframeContainer* container )
{
    container->push_back( osgAnimation::FloatKeyframe(0.0, 0.0) );
    container->push_back( osgAnimation::FloatKeyframe(2.0, 1.0) );
}

int main( int argc, char** argv )
{
    osg::ref_ptr<osgAnimation::FloatLinearChannel> channel =
        new osgAnimation::FloatLinearChannel;
    channel->setName( "0" );
    channel->setTargetName( "MorphCallback" );
    createMorphKeyframes( channel->getOrCreateSampler()->getOrCreateKeyframeContainer() );
    osg::ref_ptr<osgAnimation::Animation> anim = new osgAnimation::Animation;
    anim->setPlayMode( osgAnimation::Animation::PPONG );
    anim->addChannel( channel.get() );
   
    osg::ref_ptr<osgAnimation::BasicAnimationManager> mng =
        new osgAnimation::BasicAnimationManager;
    mng->registerAnimation( anim.get() );
   
    osg::ref_ptr<osgAnimation::MorphGeometry> morph =
        new osgAnimation::MorphGeometry( *createSourceGeometry() );
    morph->addMorphTarget( createTargetGeometry() );
   
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable( morph.get() );
    geode->setUpdateCallback( new osgAnimation::UpdateMorph("MorphCallback") );
   
    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild( geode.get() );
    root->setUpdateCallback( mng );
   
    mng->playAnimation( anim.get() );
   
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    return viewer.run();
}

该用户从未签到

发表于 2014-5-4 22:11:52 | 显示全部楼层
本帖最后由 buaahc 于 2014-5-4 22:37 编辑

你没有为geomatry添加法线数组的原因,osgUtil::SmoothingVisitor smv;
    smv.smooth( *line );对直线没用,
你应该为变形体添加法线数组       
osg::ref_ptr<osg::Vec3Array> NormalVertices = new osg::Vec3Array;//添加法线数组
        NormalVertices->push_back( osg::Vec3(0,0,1) );//添加法线数组
        NormalVertices->push_back( osg::Vec3(0,0,1) );//添加法线数组

        osg::ref_ptr<osg::Geometry> line = new osg::Geometry;
        line->setVertexArray(vertices);
        line->setNormalArray(NormalVertices);//添加法线数组
变形体的两个geometry都添加
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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