From c7a7bd03cb22791e581329156fb166770fdff148 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 5 Apr 2003 22:24:48 +0000 Subject: [PATCH] Added support for recording animation paths in the osgProducer::Viewer. --- examples/osgcameragroup/osgcameragroup.cpp | 2 +- include/osgProducer/OsgCameraGroup | 8 +++ include/osgProducer/Viewer | 25 +++++++- src/osgProducer/OsgCameraGroup.cpp | 17 +++++ src/osgProducer/Viewer.cpp | 30 +++++++-- src/osgProducer/ViewerEventHandler.cpp | 73 +++++++++++++++++++++- 6 files changed, 145 insertions(+), 10 deletions(-) diff --git a/examples/osgcameragroup/osgcameragroup.cpp b/examples/osgcameragroup/osgcameragroup.cpp index 334efed9c..16cb645d5 100644 --- a/examples/osgcameragroup/osgcameragroup.cpp +++ b/examples/osgcameragroup/osgcameragroup.cpp @@ -194,7 +194,7 @@ int main( int argc, char **argv ) // update the main producer camera - cg->setView(old_style_osg_camera->getModelViewMatrix().ptr()); + cg->setView(old_style_osg_camera->getModelViewMatrix()); // fire off the cull and draw traversals of the scene. cg->frame(); diff --git a/include/osgProducer/OsgCameraGroup b/include/osgProducer/OsgCameraGroup index 2165eb824..a0c936930 100644 --- a/include/osgProducer/OsgCameraGroup +++ b/include/osgProducer/OsgCameraGroup @@ -141,6 +141,14 @@ class OSGPRODUCER_EXPORT OsgCameraGroup : public Producer::CameraGroup /** Realize the render surfaces (OpenGL graphics) and various threads, and call any realize callbacks.*/ virtual void realize( ThreadingModel thread_model= SingleThreaded ); + /** Set the model view matrix of the camera group, + * by individually set all the camera groups's camera.*/ + void setView(const osg::Matrix& matrix); + + /** Get the model view martrix of the camera group, + * taking its value for camera 0.*/ + const osg::Matrix getViewMatrix() const; + /** Dispatch the cull and draw for each of the Camera's for this frame.*/ virtual void frame(); diff --git a/include/osgProducer/Viewer b/include/osgProducer/Viewer index a015abd04..d09227519 100644 --- a/include/osgProducer/Viewer +++ b/include/osgProducer/Viewer @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -71,12 +72,14 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction bool done() const { return _done; } + virtual void realize( ThreadingModel thread_model= SingleThreaded ); + virtual void sync(); virtual void update(); - - virtual void realize( ThreadingModel thread_model= SingleThreaded ); + /** Dispatch the cull and draw for each of the Camera's for this frame.*/ + virtual void frame(); virtual void requestRedraw() {} virtual void requestContinuousUpdate(bool) {} @@ -84,10 +87,23 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction typedef std::list< osg::ref_ptr > EventHandlerList; EventHandlerList& getEventHandlerList() { return _eventHandlerList; } + const EventHandlerList& getEventHandlerList() const { return _eventHandlerList; } + + osgGA::KeySwitchCameraManipulator* getKeySwitchCameraManipulator() { return _keyswitchManipulator.get(); } + const osgGA::KeySwitchCameraManipulator* getKeySwitchCameraManipulator() const { return _keyswitchManipulator.get(); } unsigned int addCameraManipulator(osgGA::CameraManipulator* cm); void selectCameraManipulator(unsigned int no); + + void setRecordingAnimationPath(bool on) { _recordingAnimationPath = on; } + bool getRecordingAnimationPath() const { return _recordingAnimationPath; } + + void setAnimationPath(osg::AnimationPath* path) { _animationPath = path; } + osg::AnimationPath* getAnimationPath() { return _animationPath.get(); } + const osg::AnimationPath* getAnimationPath() const { return _animationPath.get(); } + + /** Get the keyboard and mouse usage of this viewer.*/ virtual void getUsage(osg::ApplicationUsage& usage) const; @@ -108,6 +124,11 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction osg::ref_ptr _old_style_osg_camera; osg::ref_ptr _updateVisitor; + + + bool _recordingAnimationPath; + osg::ref_ptr _animationPath; + }; } diff --git a/src/osgProducer/OsgCameraGroup.cpp b/src/osgProducer/OsgCameraGroup.cpp index 332d476c7..be001ba58 100644 --- a/src/osgProducer/OsgCameraGroup.cpp +++ b/src/osgProducer/OsgCameraGroup.cpp @@ -340,6 +340,23 @@ const osg::Node* OsgCameraGroup::getTopMostSceneData() const return _scene_data.get(); } +void OsgCameraGroup::setView(const osg::Matrix& matrix) +{ + Producer::Matrix pm(matrix.ptr()); + CameraGroup::setView(pm); +} + +const osg::Matrix OsgCameraGroup::getViewMatrix() const +{ + osg::Matrix matrix; + if (_cfg && _cfg->getNumberOfCameras()>=1) + { + Producer::Camera *cam = _cfg->getCamera(0); + matrix.set(cam->getViewMatrix()); + } + return matrix; +} + void OsgCameraGroup::frame() { osg::Node* node = getTopMostSceneData(); diff --git a/src/osgProducer/Viewer.cpp b/src/osgProducer/Viewer.cpp index e93d8cd62..76711525a 100644 --- a/src/osgProducer/Viewer.cpp +++ b/src/osgProducer/Viewer.cpp @@ -22,7 +22,8 @@ using namespace osgProducer; Viewer::Viewer(): _done(0), _frameNumber(0), - _kbmcb(0) + _kbmcb(0), + _recordingAnimationPath(false) { } @@ -30,7 +31,8 @@ Viewer::Viewer(Producer::CameraConfig *cfg): OsgCameraGroup(cfg), _done(false), _frameNumber(0), - _kbmcb(0) + _kbmcb(0), + _recordingAnimationPath(false) { } @@ -38,7 +40,8 @@ Viewer::Viewer(const std::string& configFile): OsgCameraGroup(configFile), _done(false), _frameNumber(0), - _kbmcb(0) + _kbmcb(0), + _recordingAnimationPath(false) { } @@ -46,7 +49,8 @@ Viewer::Viewer(osg::ArgumentParser& arguments): OsgCameraGroup(arguments), _done(false), _frameNumber(0), - _kbmcb(0) + _kbmcb(0), + _recordingAnimationPath(false) { // report the usage options. if (arguments.getApplicationUsage()) @@ -242,9 +246,25 @@ void Viewer::update() } // update the main producer camera - if (_old_style_osg_camera.valid()) setView(_old_style_osg_camera->getModelViewMatrix().ptr()); + if (_old_style_osg_camera.valid()) setView(_old_style_osg_camera->getModelViewMatrix()); } +void Viewer::frame() +{ + + if (getRecordingAnimationPath() && getAnimationPath()) + { + osg::Matrix matrix; + matrix.invert(getViewMatrix()); + osg::Quat quat; + quat.set(matrix); + getAnimationPath()->insert(_frameStamp->getReferenceTime(),osg::AnimationPath::ControlPoint(matrix.getTrans(),quat)); + } + + OsgCameraGroup::frame(); +} + + void Viewer::selectCameraManipulator(unsigned int no) { if (_keyswitchManipulator.valid()) _keyswitchManipulator->selectCameraManipulator(no); diff --git a/src/osgProducer/ViewerEventHandler.cpp b/src/osgProducer/ViewerEventHandler.cpp index eebdb64a9..ed3ed3a23 100644 --- a/src/osgProducer/ViewerEventHandler.cpp +++ b/src/osgProducer/ViewerEventHandler.cpp @@ -1,5 +1,5 @@ #include - +#include #include #include @@ -682,7 +682,7 @@ ViewerEventHandler::ViewerEventHandler(OsgCameraGroup* cg): cam->addPostDrawCallback(new DrawCallback(this,0)); } -bool ViewerEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) +bool ViewerEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) { if(!_cg) return false; @@ -751,6 +751,75 @@ bool ViewerEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActio setDisplayHelp(!getDisplayHelp()); return true; } + case 'Z' : + case 'z' : + { + osgProducer::Viewer* viewer = dynamic_cast(_cg); + if (viewer) + { + if (viewer->getRecordingAnimationPath()) + { + // have already been recording so switch of recording. + viewer->setRecordingAnimationPath(false); + + osg::notify(osg::NOTICE) << "Finished recording camera animation, press 'Z' to reply."<< std::endl; + + if (viewer->getAnimationPath()) + { + std::ofstream fout("saved_animation.path"); + const osg::AnimationPath::TimeControlPointMap& tcpm = viewer->getAnimationPath()->getTimeControlPointMap(); + for(osg::AnimationPath::TimeControlPointMap::const_iterator tcpmitr=tcpm.begin(); + tcpmitr!=tcpm.end(); + ++tcpmitr) + { + const osg::AnimationPath::ControlPoint& cp = tcpmitr->second; + fout<first<<" "<setRecordingAnimationPath(true); + viewer->setAnimationPath(new osg::AnimationPath()); + viewer->getAnimationPath()->setLoopMode(osg::AnimationPath::LOOP); + osg::notify(osg::NOTICE) << "Recording camera animation, press 'z' to finish recording."<< std::endl; + } + + if (ea.getKey()=='Z') + { + osgGA::AnimationPathManipulator* apm = 0; + unsigned int apmNo = 0; + + osgGA::KeySwitchCameraManipulator* kscm = viewer->getKeySwitchCameraManipulator(); + if (kscm) + { + for(apmNo=0;apmNogetNumCameraManipualtors() && apm==0;++apmNo) + { + apm = dynamic_cast(kscm->getCameraManipulator(apmNo)); + } + } + + if (!apm) + { + apm = new osgGA::AnimationPathManipulator(); + apmNo = viewer->addCameraManipulator(apm); + } + + apm->setAnimationPath(viewer->getAnimationPath()); + apm->home(ea,aa); + + viewer->selectCameraManipulator(apmNo); + } + + break; + } + return true; + } default: break;