| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 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 | |
|---|
| 22 | using namespace osg; |
|---|
| 23 | |
|---|
| 24 | class 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 | |
|---|
| 57 | void 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 | |
|---|
| 78 | osg::Node* NodeTrackerCallback::getTrackNode() |
|---|
| 79 | { |
|---|
| 80 | osg::NodePath nodePath; |
|---|
| 81 | if (_trackNodePath.getNodePath(nodePath)) return nodePath.back(); |
|---|
| 82 | else return 0; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | const osg::Node* NodeTrackerCallback::getTrackNode() const |
|---|
| 86 | { |
|---|
| 87 | osg::NodePath nodePath; |
|---|
| 88 | if (_trackNodePath.getNodePath(nodePath)) return nodePath.back(); |
|---|
| 89 | else return 0; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void 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 | |
|---|
| 103 | void 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 | } |
|---|