root/OpenSceneGraph/trunk/src/osg/NodeTrackerCallback.cpp @ 13041

Revision 13041, 2.9 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osg/NodeTrackerCallback>
15#include <osg/NodeVisitor>
16#include <osg/MatrixTransform>
17#include <osg/PositionAttitudeTransform>
18#include <osg/CameraView>
19#include <osg/Camera>
20#include <osg/Notify>
21
22using namespace osg;
23
24class ApplyMatrixVisitor : public NodeVisitor
25{
26    public:
27
28        ApplyMatrixVisitor(const osg::Matrix& matrix):
29            _matrix(matrix) {}
30
31        virtual void apply(Camera& camera)
32        {
33            camera.setViewMatrix(_matrix);
34        }
35
36        virtual void apply(CameraView& cv)
37        {
38            cv.setPosition(_matrix.getTrans());
39            cv.setAttitude(_matrix.getRotate());
40        }
41
42        virtual void apply(MatrixTransform& mt)
43        {
44            mt.setMatrix(_matrix);
45        }
46
47        virtual void apply(PositionAttitudeTransform& pat)
48        {
49            pat.setPosition(_matrix.getTrans());
50            pat.setAttitude(_matrix.getRotate());
51        }
52
53        osg::Matrix _matrix;
54};
55
56
57void NodeTrackerCallback::setTrackNode(osg::Node* node)
58{
59    if (!node)
60    {
61        OSG_NOTICE<<"NodeTrackerCallback::setTrackNode(Node*):  Unable to set tracked node due to null Node*"<<std::endl;
62        return;
63    }
64
65    NodePathList parentNodePaths = node->getParentalNodePaths();
66
67    if (!parentNodePaths.empty())
68    {
69        OSG_INFO<<"NodeTrackerCallback::setTrackNode(Node*): Path set"<<std::endl;
70        setTrackNodePath(parentNodePaths[0]);
71    }
72    else
73    {
74        OSG_NOTICE<<"NodeTrackerCallback::setTrackNode(Node*): Unable to set tracked node due to empty parental path."<<std::endl;
75    }
76}
77
78osg::Node* NodeTrackerCallback::getTrackNode()
79{
80    osg::NodePath nodePath;
81    if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
82    else return 0;
83}
84
85const osg::Node* NodeTrackerCallback::getTrackNode() const
86{
87    osg::NodePath nodePath;
88    if (_trackNodePath.getNodePath(nodePath)) return nodePath.back();
89    else return 0;
90}
91
92void NodeTrackerCallback::operator()(Node* node, NodeVisitor* nv)
93{
94    if (nv->getVisitorType()==NodeVisitor::UPDATE_VISITOR)
95    {
96        update(*node);
97    }
98
99    traverse(node,nv);
100}
101
102
103void NodeTrackerCallback::update(osg::Node& node)
104{
105    osg::NodePath nodePath;
106    if (_trackNodePath.getNodePath(nodePath))
107    {
108        ApplyMatrixVisitor applyMatrix(computeWorldToLocal(nodePath));
109        node.accept(applyMatrix);
110    }
111}
Note: See TracBrowser for help on using the browser.