Added support for recording animation paths in the osgProducer::Viewer.

This commit is contained in:
Robert Osfield
2003-04-05 22:24:48 +00:00
parent 79fd5786d8
commit c7a7bd03cb
6 changed files with 145 additions and 10 deletions

View File

@@ -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();

View File

@@ -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);

View File

@@ -1,5 +1,5 @@
#include <osgProducer/ViewerEventHandler>
#include <osgGA/AnimationPathManipulator>
#include <osgDB/WriteFile>
#include <osgText/Text>
@@ -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<osgProducer::Viewer*>(_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<<tcpmitr->first<<" "<<cp._position<<" "<<cp._rotation<<std::endl;
}
fout.close();
osg::notify(osg::NOTICE) << "Saved camera animation to 'saved_animation.path'"<< std::endl;
}
}
else if (ea.getKey()=='z')
{
viewer->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;apmNo<kscm->getNumCameraManipualtors() && apm==0;++apmNo)
{
apm = dynamic_cast<osgGA::AnimationPathManipulator*>(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;