|
我想做一条直线变形的动画,于是在网上下了对折硬纸的动画源代码,参照那个写直线变形的代码,却总是执行时提示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();
} |
|