|
本帖最后由 Crystal 于 2014-5-7 15:38 编辑
设置了一个初始的直线,然后设置了三个目标直线,想通过这些来做直线弯曲的动画,但是执行的结果却总是动画的初始状态不是我设置的,而好像是第一个变形状态,这是为什么呢,贴上代码,求帮助解答,多谢。(我的目的是想让直线先变成1,再变成2,最后变成3,但是好像不对,还请大神帮忙解决一下)
#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(10,0,0) );
vertices->push_back( osg::Vec3(10,0,0) );
vertices->push_back( osg::Vec3(20,0,0) );
vertices->push_back( osg::Vec3(20,0,0) );
vertices->push_back( osg::Vec3(30,0,0) );
vertices->push_back( osg::Vec3(0,0,0) );
vertices->push_back( osg::Vec3(10,10,0) );
vertices->push_back( osg::Vec3(10,0,0) );
vertices->push_back( osg::Vec3(10,10,0) );
vertices->push_back( osg::Vec3(10,10,0) );
vertices->push_back( osg::Vec3(20,10,0) );
vertices->push_back( osg::Vec3(20,0,0) );
vertices->push_back( osg::Vec3(10,10,0) );
vertices->push_back( osg::Vec3(20,0,0) );
vertices->push_back( osg::Vec3(20,10,0) );
vertices->push_back( osg::Vec3(30,0,0) );
vertices->push_back( osg::Vec3(20,10,0) );
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);//添加法线数组
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);
return line.release();
}
osg::Geometry* createTarget1Geometry()
{
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back( osg::Vec3(0,0,0) );
vertices->push_back( osg::Vec3(10.5,-2.0,0) );
vertices->push_back( osg::Vec3(10.5,-2.0,0) );
vertices->push_back( osg::Vec3(21,-1.5,0) );
vertices->push_back( osg::Vec3(21,-1.5,0) );
vertices->push_back( osg::Vec3(31.3,0,0) );
vertices->push_back( osg::Vec3(0,0,0) );
vertices->push_back( osg::Vec3(10.94,7.6,0) );
vertices->push_back( osg::Vec3(10.5,-2.0,0) );
vertices->push_back( osg::Vec3(10.94,7.6,0) );
vertices->push_back( osg::Vec3(10.94,7.6,0) );
vertices->push_back( osg::Vec3(20.7,8.7,0) );
vertices->push_back( osg::Vec3(21,-1.5,0) );
vertices->push_back( osg::Vec3(10.94,7.6,0) );
vertices->push_back( osg::Vec3(21,-1.5,0) );
vertices->push_back( osg::Vec3(20.7,8.7,0) );
vertices->push_back( osg::Vec3(31.3,0,0) );
vertices->push_back( osg::Vec3(20.7,8.7,0) );
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);//添加法线数组
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);
return line.release();
}
osg::Geometry* createTarget2Geometry()
{
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back( osg::Vec3(0,0,0) );
vertices->push_back( osg::Vec3(10.65,-3,0) );
vertices->push_back( osg::Vec3(10.65,-3,0) );
vertices->push_back( osg::Vec3(21.3,-1.9,0) );
vertices->push_back( osg::Vec3(21.3,-1.9,0) );
vertices->push_back( osg::Vec3(31.6,0,0) );
vertices->push_back( osg::Vec3(0,0,0) );
vertices->push_back( osg::Vec3(11.2,7,0) );
vertices->push_back( osg::Vec3(10.65,-3,0) );
vertices->push_back( osg::Vec3(11.2,7,0) );
vertices->push_back( osg::Vec3(11.2,7,0) );
vertices->push_back( osg::Vec3(20.86,8.3,0) );
vertices->push_back( osg::Vec3(21.3,-1.9,0) );
vertices->push_back( osg::Vec3(11.2,7,0) );
vertices->push_back( osg::Vec3(21.3,-1.9,0) );
vertices->push_back( osg::Vec3(20.86,8.3,0) );
vertices->push_back( osg::Vec3(31.6,0,0) );
vertices->push_back( osg::Vec3(20.86,8.3,0) );
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);//添加法线数组
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);
return line.release();
}
osg::Geometry* createTarget3Geometry()
{
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back( osg::Vec3(0,0,0) );
vertices->push_back( osg::Vec3(10.78,-3.6,0) );
vertices->push_back( osg::Vec3(10.78,-3.6,0) );
vertices->push_back( osg::Vec3(21.5,-2.4,0) );
vertices->push_back( osg::Vec3(21.5,-2.4,0) );
vertices->push_back( osg::Vec3(31.9,0,0) );
vertices->push_back( osg::Vec3(0,0,0) );
vertices->push_back( osg::Vec3(11.4,6.4,0) );
vertices->push_back( osg::Vec3(10.78,-3.6,0) );
vertices->push_back( osg::Vec3(11.4,6.4,0) );
vertices->push_back( osg::Vec3(11.4,6.4,0) );
vertices->push_back( osg::Vec3(21,8,0) );
vertices->push_back( osg::Vec3(21.5,-2.4,0) );
vertices->push_back( osg::Vec3(11.4,6.4,0) );
vertices->push_back( osg::Vec3(21.5,-2.4,0) );
vertices->push_back( osg::Vec3(21,8,0) );
vertices->push_back( osg::Vec3(31.9,0,0) );
vertices->push_back( osg::Vec3(21,8,0) );
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);//添加法线数组
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);
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) );
container->push_back( osgAnimation::FloatKeyframe(4.0, 2.0) );
container->push_back( osgAnimation::FloatKeyframe(6.0, 3.0) );
}
int main( int argc, char** argv )
{
osg::ref_ptr<osgAnimation::FloatLinearChannel> channel1 =
new osgAnimation::FloatLinearChannel;
channel1->setName( "0" );
channel1->setTargetName( "MorphCallback" );
createMorphKeyframes( channel1->getOrCreateSampler()->getOrCreateKeyframeContainer() );
osg::ref_ptr<osgAnimation::Animation> anim1= new osgAnimation::Animation;
anim1->setPlayMode( osgAnimation::Animation::ONCE );
anim1->addChannel( channel1.get() );
osg::ref_ptr<osgAnimation::BasicAnimationManager> mng =
new osgAnimation::BasicAnimationManager;
mng->registerAnimation( anim1.get() );
osg::ref_ptr<osgAnimation::MorphGeometry> morph =
new osgAnimation::MorphGeometry( *createSourceGeometry() );
morph->addMorphTarget( createTarget1Geometry() );
morph->addMorphTarget( createTarget2Geometry() );
morph->addMorphTarget( createTarget3Geometry() );
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.get() );
mng->playAnimation( anim1.get() );
osgViewer::Viewer viewer;
viewer.setSceneData( root.get() );
return viewer.run();
} |
|