2013-10-25 22:54:15 +08:00
|
|
|
/* -*-c++-*-
|
2009-07-16 19:49:37 +08:00
|
|
|
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
|
2008-11-22 20:14:19 +08:00
|
|
|
*
|
2013-10-25 22:54:15 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2008-11-22 20:14:19 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2013-10-25 22:54:15 +08:00
|
|
|
*
|
2008-11-22 20:14:19 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-10-25 22:54:15 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-11-22 20:14:19 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/FlightManipulator>
|
|
|
|
#include <osgGA/DriveManipulator>
|
|
|
|
#include <osgGA/KeySwitchMatrixManipulator>
|
|
|
|
#include <osgGA/StateSetManipulator>
|
|
|
|
#include <osgGA/AnimationPathManipulator>
|
|
|
|
#include <osgGA/TerrainManipulator>
|
|
|
|
|
|
|
|
#include <osgAnimation/Bone>
|
|
|
|
#include <osgAnimation/Skeleton>
|
|
|
|
#include <osgAnimation/RigGeometry>
|
|
|
|
#include <osgAnimation/Timeline>
|
|
|
|
#include <osgAnimation/AnimationManagerBase>
|
2008-12-17 04:29:00 +08:00
|
|
|
#include <osgAnimation/TimelineAnimationManager>
|
2008-11-22 20:14:19 +08:00
|
|
|
|
2009-10-28 21:31:24 +08:00
|
|
|
#include <osgAnimation/ActionStripAnimation>
|
|
|
|
#include <osgAnimation/ActionBlendIn>
|
|
|
|
#include <osgAnimation/ActionBlendOut>
|
|
|
|
#include <osgAnimation/ActionAnimation>
|
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
struct NoseBegin : public osgAnimation::Action::Callback
|
|
|
|
{
|
2009-10-10 17:35:16 +08:00
|
|
|
virtual void operator()(osgAnimation::Action* action, osgAnimation::ActionVisitor* nv)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
|
|
|
std::cout << "sacrebleu, it scratches my nose, let me scratch it" << std::endl;
|
|
|
|
std::cout << "process NoseBegin call back " << action->getName() << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NoseEnd : public osgAnimation::Action::Callback
|
|
|
|
{
|
2009-10-10 17:35:16 +08:00
|
|
|
virtual void operator()(osgAnimation::Action* action, osgAnimation::ActionVisitor* nv)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
|
|
|
std::cout << "shhhrt shrrrrt shhhhhhrrrrt, haaa it's better"<< std::endl;
|
|
|
|
std::cout << "process NoseEnd call back " << action->getName() << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExampleTimelineUsage : public osgGA::GUIEventHandler
|
|
|
|
{
|
2009-10-28 21:31:24 +08:00
|
|
|
osg::ref_ptr<osgAnimation::ActionStripAnimation> _mainLoop;
|
|
|
|
osg::ref_ptr<osgAnimation::ActionStripAnimation> _scratchHead;
|
|
|
|
osg::ref_ptr<osgAnimation::ActionStripAnimation> _scratchNose;
|
2008-12-17 04:29:00 +08:00
|
|
|
osg::ref_ptr<osgAnimation::TimelineAnimationManager> _manager;
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
bool _releaseKey;
|
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
ExampleTimelineUsage(osgAnimation::TimelineAnimationManager* manager)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
|
|
|
_releaseKey = false;
|
|
|
|
_manager = manager;
|
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
const osgAnimation::AnimationList& list = _manager->getAnimationList();
|
|
|
|
osgAnimation::AnimationMap map;
|
|
|
|
for (osgAnimation::AnimationList::const_iterator it = list.begin(); it != list.end(); it++)
|
|
|
|
map[(*it)->getName()] = *it;
|
|
|
|
|
2009-10-28 21:31:24 +08:00
|
|
|
_mainLoop = new osgAnimation::ActionStripAnimation(map["Idle_Main"].get(),0.0,0.0);
|
2008-11-22 20:14:19 +08:00
|
|
|
_mainLoop->setLoop(0); // means forever
|
|
|
|
|
2009-10-28 21:31:24 +08:00
|
|
|
_scratchHead = new osgAnimation::ActionStripAnimation(map["Idle_Head_Scratch.02"].get(),0.2,0.3);
|
2008-11-22 20:14:19 +08:00
|
|
|
_scratchHead->setLoop(1); // one time
|
|
|
|
|
|
|
|
map["Idle_Nose_Scratch.01"]->setDuration(10.0); // set this animation duration to 10 seconds
|
2009-10-28 21:31:24 +08:00
|
|
|
_scratchNose = new osgAnimation::ActionStripAnimation(map["Idle_Nose_Scratch.01"].get(),0.2,0.3);
|
2008-11-22 20:14:19 +08:00
|
|
|
_scratchNose->setLoop(1); // one time
|
|
|
|
|
|
|
|
// add the main loop at priority 0 at time 0.
|
2013-10-25 22:54:15 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
osgAnimation::Timeline* tml = _manager->getTimeline();
|
|
|
|
tml->play();
|
|
|
|
tml->addActionAt(0.0, _mainLoop.get(), 0);
|
|
|
|
|
|
|
|
|
|
|
|
// add a scratch head priority 1 at 3.0 second.
|
|
|
|
tml->addActionAt(5.0, _scratchHead.get(), 1);
|
|
|
|
|
|
|
|
// populate time with scratch head
|
|
|
|
for (int i = 1; i < 20; i++)
|
|
|
|
{
|
|
|
|
// we add a scratch head priority 1 each 10 second
|
|
|
|
// note:
|
2015-04-13 18:43:56 +08:00
|
|
|
// it's possible to add the same instance more than once on the timeline
|
2008-11-22 20:14:19 +08:00
|
|
|
// the only things you need to take care is if you remove it. It will remove
|
2015-04-13 18:43:56 +08:00
|
|
|
// all instance that exist on the timeline. If you need to differentiate
|
2008-11-22 20:14:19 +08:00
|
|
|
// it's better to create a new instance
|
|
|
|
tml->addActionAt(5.0 + 10.0 * i, _scratchHead.get(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we will add the scratch nose action only when the player hit a key
|
|
|
|
// in the operator()
|
|
|
|
|
|
|
|
// now we will add callback at end and begin of animation of Idle_Nose_Scratch.02
|
|
|
|
_scratchNose->setCallback(0.0, new NoseBegin);
|
|
|
|
_scratchNose->setCallback(_scratchNose->getNumFrames()-1, new NoseEnd);
|
|
|
|
}
|
|
|
|
|
2009-01-07 19:24:47 +08:00
|
|
|
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
|
|
|
if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP)
|
|
|
|
{
|
|
|
|
_releaseKey = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
|
|
|
|
{
|
|
|
|
if (_releaseKey) // we hit a key and release it execute an action
|
|
|
|
{
|
|
|
|
osgAnimation::Timeline* tml = _manager->getTimeline();
|
2016-05-30 19:30:05 +08:00
|
|
|
// don't play if already playing
|
2008-11-22 20:14:19 +08:00
|
|
|
if (!tml->isActive(_scratchNose.get()))
|
|
|
|
{
|
|
|
|
// add this animation on top of two other
|
|
|
|
// we add one to evaluate the animation at the next frame, else we
|
|
|
|
// will miss the current frame
|
|
|
|
tml->addActionAt(tml->getCurrentFrame() + 1, _scratchNose.get(), 2);
|
|
|
|
}
|
|
|
|
_releaseKey = false;
|
|
|
|
}
|
2013-10-25 22:54:15 +08:00
|
|
|
traverse(node, nv);
|
2008-11-22 20:14:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-25 22:54:15 +08:00
|
|
|
osgGA::GUIEventHandler::operator()(node, nv);
|
|
|
|
}
|
2008-11-22 20:14:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
2009-07-16 19:49:37 +08:00
|
|
|
std::cerr << "This example works only with nathan.osg" << std::endl;
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
osg::ArgumentParser psr(&argc, argv);
|
|
|
|
|
|
|
|
osgViewer::Viewer viewer(psr);
|
|
|
|
|
2009-07-16 19:49:37 +08:00
|
|
|
std::string file = "nathan.osg";
|
2013-10-25 22:54:15 +08:00
|
|
|
if(argc >= 2)
|
2008-11-22 20:14:19 +08:00
|
|
|
file = psr[1];
|
|
|
|
|
|
|
|
// replace the manager
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedmodel = osgDB::readRefNodeFile(file);
|
|
|
|
osg::Group* root = dynamic_cast<osg::Group*>(loadedmodel.get());
|
2009-07-16 19:49:37 +08:00
|
|
|
if (!root) {
|
|
|
|
osg::notify(osg::FATAL) << "can't read file " << file << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-12-17 04:29:00 +08:00
|
|
|
osgAnimation::AnimationManagerBase* animationManager = dynamic_cast<osgAnimation::AnimationManagerBase*>(root->getUpdateCallback());
|
2013-10-25 22:54:15 +08:00
|
|
|
if(!animationManager)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-07-16 19:49:37 +08:00
|
|
|
osg::notify(osg::FATAL) << "Did not find AnimationManagerBase updateCallback needed to animate elements" << std::endl;
|
2008-11-22 20:14:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
osg::ref_ptr<osgAnimation::TimelineAnimationManager> tl = new osgAnimation::TimelineAnimationManager(*animationManager);
|
2008-12-17 04:49:12 +08:00
|
|
|
root->setUpdateCallback(tl.get());
|
2013-10-25 22:54:15 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
ExampleTimelineUsage* callback = new ExampleTimelineUsage(tl.get());
|
2008-12-17 04:29:00 +08:00
|
|
|
root->setEventCallback(callback);
|
|
|
|
root->getUpdateCallback()->addNestedCallback(callback);
|
2013-10-25 22:54:15 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
// add the state manipulator
|
|
|
|
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
|
2013-10-25 22:54:15 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
// add the thread model handler
|
|
|
|
viewer.addEventHandler(new osgViewer::ThreadingHandler);
|
|
|
|
|
|
|
|
// add the window size toggle handler
|
|
|
|
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
|
2013-10-25 22:54:15 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
// add the stats handler
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
|
|
|
|
// add the help handler
|
|
|
|
viewer.addEventHandler(new osgViewer::HelpHandler(psr.getApplicationUsage()));
|
|
|
|
|
|
|
|
// add the LOD Scale handler
|
|
|
|
viewer.addEventHandler(new osgViewer::LODScaleHandler);
|
|
|
|
|
|
|
|
// add the screen capture handler
|
|
|
|
viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
|
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
viewer.setSceneData(root);
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
return viewer.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|