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