| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <iostream> |
|---|
| 16 | #include <osg/Geometry> |
|---|
| 17 | #include <osg/Shape> |
|---|
| 18 | #include <osg/ShapeDrawable> |
|---|
| 19 | #include <osgViewer/Viewer> |
|---|
| 20 | #include <osgGA/TrackballManipulator> |
|---|
| 21 | #include <osg/MatrixTransform> |
|---|
| 22 | |
|---|
| 23 | #include <osgAnimation/AnimationManager> |
|---|
| 24 | #include <osgAnimation/Channel> |
|---|
| 25 | #include <osgAnimation/UpdateCallback> |
|---|
| 26 | |
|---|
| 27 | using namespace osgAnimation; |
|---|
| 28 | |
|---|
| 29 | osg::ref_ptr<osg::Geode> createAxis() |
|---|
| 30 | { |
|---|
| 31 | osg::ref_ptr<osg::Geode> geode (new osg::Geode()); |
|---|
| 32 | osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry()); |
|---|
| 33 | |
|---|
| 34 | osg::ref_ptr<osg::Vec3Array> vertices (new osg::Vec3Array()); |
|---|
| 35 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0)); |
|---|
| 36 | vertices->push_back (osg::Vec3 ( 10.0, 0.0, 0.0)); |
|---|
| 37 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0)); |
|---|
| 38 | vertices->push_back (osg::Vec3 ( 0.0, 10.0, 0.0)); |
|---|
| 39 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0)); |
|---|
| 40 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 10.0)); |
|---|
| 41 | geometry->setVertexArray (vertices.get()); |
|---|
| 42 | |
|---|
| 43 | osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array()); |
|---|
| 44 | colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f)); |
|---|
| 45 | colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f)); |
|---|
| 46 | colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 47 | colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 48 | colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f)); |
|---|
| 49 | colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f)); |
|---|
| 50 | geometry->setColorArray (colors.get()); |
|---|
| 51 | |
|---|
| 52 | geometry->setColorBinding (osg::Geometry::BIND_PER_VERTEX); |
|---|
| 53 | geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6)); |
|---|
| 54 | |
|---|
| 55 | geode->addDrawable( geometry.get() ); |
|---|
| 56 | geode->getOrCreateStateSet()->setMode(GL_LIGHTING, false); |
|---|
| 57 | return geode; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | int main (int argc, char* argv[]) |
|---|
| 62 | { |
|---|
| 63 | osgViewer::Viewer viewer; |
|---|
| 64 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| 65 | |
|---|
| 66 | osg::Group* root = new osg::Group; |
|---|
| 67 | |
|---|
| 68 | osg::ref_ptr<osg::Geode> axe = createAxis(); |
|---|
| 69 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 70 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),0.5))); |
|---|
| 71 | |
|---|
| 72 | osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform(); |
|---|
| 73 | trans->setName("AnimatedNode"); |
|---|
| 74 | trans->setDataVariance(osg::Object::DYNAMIC); |
|---|
| 75 | trans->setUpdateCallback(new osgAnimation::UpdateTransform("AnimatedCallback")); |
|---|
| 76 | trans->setMatrix(osg::Matrix::identity()); |
|---|
| 77 | trans->addChild (geode.get()); |
|---|
| 78 | |
|---|
| 79 | root->addChild (axe.get()); |
|---|
| 80 | root->addChild (trans.get()); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | osgAnimation::AnimationManager* mng = new osgAnimation::AnimationManager(); |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | mng->addChild(root); |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel; |
|---|
| 90 | channelAnimation1->setTargetName("AnimatedCallback"); |
|---|
| 91 | channelAnimation1->setName("position"); |
|---|
| 92 | channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0))); |
|---|
| 93 | channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0))); |
|---|
| 94 | osgAnimation::Animation* anim1 = new osgAnimation::Animation; |
|---|
| 95 | anim1->addChannel(channelAnimation1); |
|---|
| 96 | anim1->setPlaymode(osgAnimation::Animation::PPONG); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | osgAnimation::Vec3LinearChannel* channelAnimation2 = new osgAnimation::Vec3LinearChannel; |
|---|
| 100 | channelAnimation2->setTargetName("AnimatedCallback"); |
|---|
| 101 | channelAnimation2->setName("euler"); |
|---|
| 102 | channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0))); |
|---|
| 103 | channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(1.5, osg::Vec3(2*osg::PI,0,0))); |
|---|
| 104 | osgAnimation::Animation* anim2 = new osgAnimation::Animation; |
|---|
| 105 | anim2->addChannel(channelAnimation2); |
|---|
| 106 | anim2->setPlaymode(osgAnimation::Animation::LOOP); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | mng->registerAnimation(anim1); |
|---|
| 111 | mng->registerAnimation(anim2); |
|---|
| 112 | |
|---|
| 113 | mng->playAnimation(anim1); |
|---|
| 114 | mng->playAnimation(anim2); |
|---|
| 115 | |
|---|
| 116 | viewer.setSceneData( mng ); |
|---|
| 117 | return viewer.run(); |
|---|
| 118 | } |
|---|