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)
|
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
OSG_NOTICE<<"NodeTrackerCallback::setTrackNode(Node*): Unable to set tracked node due to null Node*"<<std::endl;
|
2005-10-24 18:51:50 +08:00
|
|
|
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
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
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
|
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
OSG_NOTICE<<"NodeTrackerCallback::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl;
|
2005-10-24 18:51:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 04:15:32 +08:00
|
|
|
osg::Node* NodeTrackerCallback::getTrackNode()
|
|
|
|
{
|
|
|
|
osg::NodePath nodePath;
|
|
|
|
if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const osg::Node* NodeTrackerCallback::getTrackNode() const
|
|
|
|
{
|
|
|
|
osg::NodePath nodePath;
|
|
|
|
if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:51:50 +08:00
|
|
|
void NodeTrackerCallback::operator()(Node* node, NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
if (nv->getVisitorType()==NodeVisitor::UPDATE_VISITOR)
|
|
|
|
{
|
|
|
|
update(*node);
|
|
|
|
}
|
|
|
|
|
|
|
|
traverse(node,nv);
|
|
|
|
}
|
|
|
|
|
2006-02-28 03:49:47 +08:00
|
|
|
|
|
|
|
void NodeTrackerCallback::update(osg::Node& node)
|
|
|
|
{
|
|
|
|
osg::NodePath nodePath;
|
2010-02-16 04:15:32 +08:00
|
|
|
if (_trackNodePath.getNodePath(nodePath))
|
2006-02-28 03:49:47 +08:00
|
|
|
{
|
2010-02-16 04:15:32 +08:00
|
|
|
ApplyMatrixVisitor applyMatrix(computeWorldToLocal(nodePath));
|
|
|
|
node.accept(applyMatrix);
|
2006-02-28 03:49:47 +08:00
|
|
|
}
|
|
|
|
}
|