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