| 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/Skinning> |
|---|
| 30 | #include <osgAnimation/Timeline> |
|---|
| 31 | #include <osgAnimation/AnimationManagerBase> |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | struct NoseBegin : public osgAnimation::Action::Callback |
|---|
| 35 | { |
|---|
| 36 | virtual void operator()(osgAnimation::Action* action) |
|---|
| 37 | { |
|---|
| 38 | std::cout << "sacrebleu, it scratches my nose, let me scratch it" << std::endl; |
|---|
| 39 | std::cout << "process NoseBegin call back " << action->getName() << std::endl << std::endl; |
|---|
| 40 | } |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | struct NoseEnd : public osgAnimation::Action::Callback |
|---|
| 44 | { |
|---|
| 45 | virtual void operator()(osgAnimation::Action* action) |
|---|
| 46 | { |
|---|
| 47 | std::cout << "shhhrt shrrrrt shhhhhhrrrrt, haaa it's better"<< std::endl; |
|---|
| 48 | std::cout << "process NoseEnd call back " << action->getName() << std::endl << std::endl; |
|---|
| 49 | } |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | struct ExampleTimelineUsage : public osgGA::GUIEventHandler |
|---|
| 53 | { |
|---|
| 54 | osg::ref_ptr<osgAnimation::StripAnimation> _mainLoop; |
|---|
| 55 | osg::ref_ptr<osgAnimation::StripAnimation> _scratchHead; |
|---|
| 56 | osg::ref_ptr<osgAnimation::StripAnimation> _scratchNose; |
|---|
| 57 | osg::ref_ptr<osgAnimation::AnimationManagerTimeline> _manager; |
|---|
| 58 | |
|---|
| 59 | bool _releaseKey; |
|---|
| 60 | |
|---|
| 61 | ExampleTimelineUsage(osgAnimation::AnimationManagerTimeline* manager) |
|---|
| 62 | { |
|---|
| 63 | _releaseKey = false; |
|---|
| 64 | _manager = manager; |
|---|
| 65 | |
|---|
| 66 | osgAnimation::AnimationMap map = _manager->getAnimationMap(); |
|---|
| 67 | _mainLoop = new osgAnimation::StripAnimation(map["Idle_Main"].get(),0.0,0.0); |
|---|
| 68 | _mainLoop->setLoop(0); |
|---|
| 69 | |
|---|
| 70 | _scratchHead = new osgAnimation::StripAnimation(map["Idle_Head_Scratch.02"].get(),0.2,0.3); |
|---|
| 71 | _scratchHead->setLoop(1); |
|---|
| 72 | |
|---|
| 73 | map["Idle_Nose_Scratch.01"]->setDuration(10.0); |
|---|
| 74 | _scratchNose = new osgAnimation::StripAnimation(map["Idle_Nose_Scratch.01"].get(),0.2,0.3); |
|---|
| 75 | _scratchNose->setLoop(1); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | osgAnimation::Timeline* tml = _manager->getTimeline(); |
|---|
| 80 | tml->play(); |
|---|
| 81 | tml->addActionAt(0.0, _mainLoop.get(), 0); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | tml->addActionAt(5.0, _scratchHead.get(), 1); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | for (int i = 1; i < 20; i++) |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | tml->addActionAt(5.0 + 10.0 * i, _scratchHead.get(), 1); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | _scratchNose->setCallback(0.0, new NoseBegin); |
|---|
| 104 | _scratchNose->setCallback(_scratchNose->getNumFrames()-1, new NoseEnd); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 108 | { |
|---|
| 109 | if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP) |
|---|
| 110 | { |
|---|
| 111 | _releaseKey = true; |
|---|
| 112 | } |
|---|
| 113 | return false; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 117 | { |
|---|
| 118 | if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| 119 | { |
|---|
| 120 | if (_releaseKey) |
|---|
| 121 | { |
|---|
| 122 | osgAnimation::Timeline* tml = _manager->getTimeline(); |
|---|
| 123 | |
|---|
| 124 | if (!tml->isActive(_scratchNose.get())) |
|---|
| 125 | { |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | tml->addActionAt(tml->getCurrentFrame() + 1, _scratchNose.get(), 2); |
|---|
| 130 | } |
|---|
| 131 | _releaseKey = false; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv); |
|---|
| 137 | if (ev && ev->getActionAdapter() && !ev->getEvents().empty()) |
|---|
| 138 | { |
|---|
| 139 | for(osgGA::EventQueue::Events::iterator itr = ev->getEvents().begin(); |
|---|
| 140 | itr != ev->getEvents().end(); |
|---|
| 141 | ++itr) |
|---|
| 142 | { |
|---|
| 143 | handleWithCheckAgainstIgnoreHandledEventsMask(*(*itr), *(ev->getActionAdapter()), node, nv); |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | traverse(node, nv); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | int main (int argc, char* argv[]) |
|---|
| 154 | { |
|---|
| 155 | std::cerr << "This example workd only with osgAnimation/nathan.osg" << std::endl; |
|---|
| 156 | |
|---|
| 157 | osg::ArgumentParser psr(&argc, argv); |
|---|
| 158 | |
|---|
| 159 | osgViewer::Viewer viewer(psr); |
|---|
| 160 | osg::ref_ptr<osg::Group> group = new osg::Group(); |
|---|
| 161 | |
|---|
| 162 | std::string file = "osgAnimation/nathan.osg"; |
|---|
| 163 | if(argc >= 2) |
|---|
| 164 | file = psr[1]; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | osgAnimation::AnimationManagerBase* animationManager = dynamic_cast<osgAnimation::AnimationManagerBase*>(osgDB::readNodeFile(file)); |
|---|
| 168 | if(!animationManager) |
|---|
| 169 | { |
|---|
| 170 | std::cerr << "Couldn't convert the file's toplevel object into an AnimationManager." << std::endl; |
|---|
| 171 | return 1; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | osg::ref_ptr<osgAnimation::AnimationManagerTimeline> tl = new osgAnimation::AnimationManagerTimeline(*animationManager); |
|---|
| 175 | |
|---|
| 176 | animationManager->removeChildren(0, animationManager->getNumChildren()); |
|---|
| 177 | ExampleTimelineUsage* callback = new ExampleTimelineUsage(tl.get()); |
|---|
| 178 | group->addChild(tl.get()); |
|---|
| 179 | group->setEventCallback(callback); |
|---|
| 180 | group->setUpdateCallback(callback); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | viewer.addEventHandler(new osgViewer::HelpHandler(psr.getApplicationUsage())); |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | viewer.addEventHandler(new osgViewer::LODScaleHandler); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | viewer.addEventHandler(new osgViewer::ScreenCaptureHandler); |
|---|
| 203 | |
|---|
| 204 | viewer.setSceneData(group.get()); |
|---|
| 205 | |
|---|
| 206 | return viewer.run(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|