| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <iostream> |
|---|
| 16 | #include <osgDB/ReadFile> |
|---|
| 17 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 18 | #include <osgGA/TrackballManipulator> |
|---|
| 19 | #include <osgGA/FlightManipulator> |
|---|
| 20 | #include <osgGA/DriveManipulator> |
|---|
| 21 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 22 | #include <osgGA/StateSetManipulator> |
|---|
| 23 | #include <osgGA/AnimationPathManipulator> |
|---|
| 24 | #include <osgGA/TerrainManipulator> |
|---|
| 25 | |
|---|
| 26 | #include <osgAnimation/Bone> |
|---|
| 27 | #include <osgAnimation/Skeleton> |
|---|
| 28 | #include <osgAnimation/RigGeometry> |
|---|
| 29 | #include <osgAnimation/Timeline> |
|---|
| 30 | #include <osgAnimation/AnimationManagerBase> |
|---|
| 31 | #include <osgAnimation/TimelineAnimationManager> |
|---|
| 32 | |
|---|
| 33 | #include <osgAnimation/ActionStripAnimation> |
|---|
| 34 | #include <osgAnimation/ActionBlendIn> |
|---|
| 35 | #include <osgAnimation/ActionBlendOut> |
|---|
| 36 | #include <osgAnimation/ActionAnimation> |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | struct NoseBegin : public osgAnimation::Action::Callback |
|---|
| 40 | { |
|---|
| 41 | virtual void operator()(osgAnimation::Action* action, osgAnimation::ActionVisitor* nv) |
|---|
| 42 | { |
|---|
| 43 | std::cout << "sacrebleu, it scratches my nose, let me scratch it" << std::endl; |
|---|
| 44 | std::cout << "process NoseBegin call back " << action->getName() << std::endl << std::endl; |
|---|
| 45 | } |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | struct NoseEnd : public osgAnimation::Action::Callback |
|---|
| 49 | { |
|---|
| 50 | virtual void operator()(osgAnimation::Action* action, osgAnimation::ActionVisitor* nv) |
|---|
| 51 | { |
|---|
| 52 | std::cout << "shhhrt shrrrrt shhhhhhrrrrt, haaa it's better"<< std::endl; |
|---|
| 53 | std::cout << "process NoseEnd call back " << action->getName() << std::endl << std::endl; |
|---|
| 54 | } |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | struct ExampleTimelineUsage : public osgGA::GUIEventHandler |
|---|
| 58 | { |
|---|
| 59 | osg::ref_ptr<osgAnimation::ActionStripAnimation> _mainLoop; |
|---|
| 60 | osg::ref_ptr<osgAnimation::ActionStripAnimation> _scratchHead; |
|---|
| 61 | osg::ref_ptr<osgAnimation::ActionStripAnimation> _scratchNose; |
|---|
| 62 | osg::ref_ptr<osgAnimation::TimelineAnimationManager> _manager; |
|---|
| 63 | |
|---|
| 64 | bool _releaseKey; |
|---|
| 65 | |
|---|
| 66 | ExampleTimelineUsage(osgAnimation::TimelineAnimationManager* manager) |
|---|
| 67 | { |
|---|
| 68 | _releaseKey = false; |
|---|
| 69 | _manager = manager; |
|---|
| 70 | |
|---|
| 71 | const osgAnimation::AnimationList& list = _manager->getAnimationList(); |
|---|
| 72 | osgAnimation::AnimationMap map; |
|---|
| 73 | for (osgAnimation::AnimationList::const_iterator it = list.begin(); it != list.end(); it++) |
|---|
| 74 | map[(*it)->getName()] = *it; |
|---|
| 75 | |
|---|
| 76 | _mainLoop = new osgAnimation::ActionStripAnimation(map["Idle_Main"].get(),0.0,0.0); |
|---|
| 77 | _mainLoop->setLoop(0); |
|---|
| 78 | |
|---|
| 79 | _scratchHead = new osgAnimation::ActionStripAnimation(map["Idle_Head_Scratch.02"].get(),0.2,0.3); |
|---|
| 80 | _scratchHead->setLoop(1); |
|---|
| 81 | |
|---|
| 82 | map["Idle_Nose_Scratch.01"]->setDuration(10.0); |
|---|
| 83 | _scratchNose = new osgAnimation::ActionStripAnimation(map["Idle_Nose_Scratch.01"].get(),0.2,0.3); |
|---|
| 84 | _scratchNose->setLoop(1); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | osgAnimation::Timeline* tml = _manager->getTimeline(); |
|---|
| 89 | tml->play(); |
|---|
| 90 | tml->addActionAt(0.0, _mainLoop.get(), 0); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | tml->addActionAt(5.0, _scratchHead.get(), 1); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | for (int i = 1; i < 20; i++) |
|---|
| 98 | { |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | tml->addActionAt(5.0 + 10.0 * i, _scratchHead.get(), 1); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | _scratchNose->setCallback(0.0, new NoseBegin); |
|---|
| 113 | _scratchNose->setCallback(_scratchNose->getNumFrames()-1, new NoseEnd); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) |
|---|
| 117 | { |
|---|
| 118 | if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP) |
|---|
| 119 | { |
|---|
| 120 | _releaseKey = true; |
|---|
| 121 | } |
|---|
| 122 | return false; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 126 | { |
|---|
| 127 | if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| 128 | { |
|---|
| 129 | if (_releaseKey) |
|---|
| 130 | { |
|---|
| 131 | osgAnimation::Timeline* tml = _manager->getTimeline(); |
|---|
| 132 | |
|---|
| 133 | if (!tml->isActive(_scratchNose.get())) |
|---|
| 134 | { |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | tml->addActionAt(tml->getCurrentFrame() + 1, _scratchNose.get(), 2); |
|---|
| 139 | } |
|---|
| 140 | _releaseKey = false; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | else |
|---|
| 144 | { |
|---|
| 145 | osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv); |
|---|
| 146 | if (ev && ev->getActionAdapter() && !ev->getEvents().empty()) |
|---|
| 147 | { |
|---|
| 148 | for(osgGA::EventQueue::Events::iterator itr = ev->getEvents().begin(); |
|---|
| 149 | itr != ev->getEvents().end(); |
|---|
| 150 | ++itr) |
|---|
| 151 | { |
|---|
| 152 | handleWithCheckAgainstIgnoreHandledEventsMask(*(*itr), *(ev->getActionAdapter()), node, nv); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | traverse(node, nv); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | int main (int argc, char* argv[]) |
|---|
| 163 | { |
|---|
| 164 | std::cerr << "This example works only with nathan.osg" << std::endl; |
|---|
| 165 | |
|---|
| 166 | osg::ArgumentParser psr(&argc, argv); |
|---|
| 167 | |
|---|
| 168 | osgViewer::Viewer viewer(psr); |
|---|
| 169 | |
|---|
| 170 | std::string file = "nathan.osg"; |
|---|
| 171 | if(argc >= 2) |
|---|
| 172 | file = psr[1]; |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | osg::Group* root = dynamic_cast<osg::Group*>(osgDB::readNodeFile(file)); |
|---|
| 176 | if (!root) { |
|---|
| 177 | osg::notify(osg::FATAL) << "can't read file " << file << std::endl; |
|---|
| 178 | return 1; |
|---|
| 179 | } |
|---|
| 180 | osgAnimation::AnimationManagerBase* animationManager = dynamic_cast<osgAnimation::AnimationManagerBase*>(root->getUpdateCallback()); |
|---|
| 181 | if(!animationManager) |
|---|
| 182 | { |
|---|
| 183 | osg::notify(osg::FATAL) << "Did not find AnimationManagerBase updateCallback needed to animate elements" << std::endl; |
|---|
| 184 | return 1; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | osg::ref_ptr<osgAnimation::TimelineAnimationManager> tl = new osgAnimation::TimelineAnimationManager(*animationManager); |
|---|
| 188 | root->setUpdateCallback(tl.get()); |
|---|
| 189 | |
|---|
| 190 | ExampleTimelineUsage* callback = new ExampleTimelineUsage(tl.get()); |
|---|
| 191 | root->setEventCallback(callback); |
|---|
| 192 | root->getUpdateCallback()->addNestedCallback(callback); |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | viewer.addEventHandler(new osgViewer::HelpHandler(psr.getApplicationUsage())); |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | viewer.addEventHandler(new osgViewer::LODScaleHandler); |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | viewer.addEventHandler(new osgViewer::ScreenCaptureHandler); |
|---|
| 216 | |
|---|
| 217 | viewer.setSceneData(root); |
|---|
| 218 | |
|---|
| 219 | return viewer.run(); |
|---|
| 220 | } |
|---|
| 221 | |
|---|