2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-10-24 18:51:50 +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
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osg/NodeTrackerCallback>
|
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
#include <osg/CameraView>
|
2006-11-27 22:52:07 +08:00
|
|
|
#include <osg/Camera>
|
2005-10-24 18:51:50 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
class ApplyMatrixVisitor : public NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ApplyMatrixVisitor(const osg::Matrix& matrix):
|
|
|
|
_matrix(matrix) {}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
virtual void apply(Camera& camera)
|
2005-10-24 18:51:50 +08:00
|
|
|
{
|
|
|
|
camera.setViewMatrix(_matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void apply(CameraView& cv)
|
|
|
|
{
|
|
|
|
cv.setPosition(_matrix.getTrans());
|
2006-08-01 01:31:21 +08:00
|
|
|
cv.setAttitude(_matrix.getRotate());
|
2005-10-24 18:51:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void apply(MatrixTransform& mt)
|
|
|
|
{
|
|
|
|
mt.setMatrix(_matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void apply(PositionAttitudeTransform& pat)
|
|
|
|
{
|
|
|
|
pat.setPosition(_matrix.getTrans());
|
2006-08-01 01:31:21 +08:00
|
|
|
pat.setAttitude(_matrix.getRotate());
|
2005-10-24 18:51:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Matrix _matrix;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void NodeTrackerCallback::setTrackNode(osg::Node* node)
|
|
|
|
{
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"NodeTrackerCallback::setTrackNode(Node*): Unable to set tracked node due to null Node*"<<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-12-07 23:29:29 +08:00
|
|
|
NodePathList parentNodePaths = node->getParentalNodePaths();
|
2005-10-24 18:51:50 +08:00
|
|
|
|
2005-12-07 23:29:29 +08:00
|
|
|
if (!parentNodePaths.empty())
|
2005-10-24 18:51:50 +08:00
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"NodeTrackerCallback::setTrackNode(Node*): Path set"<<std::endl;
|
2006-02-28 03:49:47 +08:00
|
|
|
setTrackNodePath(parentNodePaths[0]);
|
2005-10-24 18:51:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"NodeTrackerCallback::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeTrackerCallback::operator()(Node* node, NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
if (nv->getVisitorType()==NodeVisitor::UPDATE_VISITOR)
|
|
|
|
{
|
|
|
|
update(*node);
|
|
|
|
}
|
|
|
|
|
|
|
|
traverse(node,nv);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NodeTrackerCallback::validateNodePath() const
|
|
|
|
{
|
2007-07-12 00:06:04 +08:00
|
|
|
for(ObserverNodePath::const_iterator itr = _trackNodePath.begin();
|
2006-02-28 03:49:47 +08:00
|
|
|
itr != _trackNodePath.begin();
|
|
|
|
++itr)
|
2005-10-24 18:51:50 +08:00
|
|
|
{
|
2006-02-28 03:49:47 +08:00
|
|
|
if (*itr==0)
|
2005-10-24 18:51:50 +08:00
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Warning: tracked node path has been invalidated by changes in the scene graph."<<std::endl;
|
2007-07-12 00:06:04 +08:00
|
|
|
const_cast<ObserverNodePath&>(_trackNodePath).clear();
|
2006-02-28 03:49:47 +08:00
|
|
|
return false;
|
2005-10-24 18:51:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-28 03:49:47 +08:00
|
|
|
|
|
|
|
void NodeTrackerCallback::update(osg::Node& node)
|
|
|
|
{
|
|
|
|
if (!validateNodePath()) return;
|
|
|
|
|
|
|
|
osg::NodePath nodePath;
|
2007-07-12 00:06:04 +08:00
|
|
|
for(ObserverNodePath::iterator itr = _trackNodePath.begin();
|
2006-03-01 02:54:29 +08:00
|
|
|
itr != _trackNodePath.end();
|
2006-02-28 03:49:47 +08:00
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
nodePath.push_back(itr->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
ApplyMatrixVisitor applyMatrix(computeWorldToLocal(nodePath));
|
|
|
|
node.accept(applyMatrix);
|
|
|
|
}
|