source: osgVisual/trunk/src/core/core_manipulator.cpp @ 280

Last change on this file since 280 was 241, checked in by Torben Dannhauer, 14 years ago
File size: 6.7 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
2 *
3 * This library is based on OpenSceneGraph, 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 * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
9 * You have to aquire licenses for all used proprietary modules.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * OpenSceneGraph Public License for more details.
15*/
16
17#include <core_manipulator.h>
18
19using namespace osgVisual;
20
21core_manipulator::core_manipulator()
22{
23#ifdef USE_SPACENAVIGATOR
24        _mouse = NULL;
25#endif
26        _currentTrackingID = -1;
27
28}
29
30bool core_manipulator::init(osgViewer::Viewer* viewer, osg::ArgumentParser& arguments, std::string configFilename, osg::Node* rootNode)
31{
32        _rootNode = rootNode;
33        _viewer = viewer;
34
35        // Setup manipulators
36        if(!visual_dataIO::getInstance()->isSlave()) // set up the camera manipulators if not slave.
37    {
38        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
39
40        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
41        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
42        keyswitchManipulator->addMatrixManipulator( '3', "Terrain", new osgGA::TerrainManipulator() );
43                _nt = new osgGA::NodeTrackerManipulator();
44                _nt->setTrackNode(NULL);
45                keyswitchManipulator->addMatrixManipulator( '4', "NodeTrackerManipulator", _nt );
46               
47#ifdef USE_SPACENAVIGATOR
48                // SpaceNavigator manipulator
49                _mouse = new SpaceMouse();
50                _mouse->initialize();
51                _mouseTrackerManip = new NodeTrackerSpaceMouse(_mouse);
52                _mouseTrackerManip->setTrackerMode(NodeTrackerSpaceMouse::NODE_CENTER);
53                _mouseTrackerManip->setRotationMode(NodeTrackerSpaceMouse::ELEVATION_AZIM);
54                _mouseTrackerManip->setAutoComputeHomePosition( true );
55                keyswitchManipulator->addMatrixManipulator( '5', "Spacemouse Node Tracker", _mouseTrackerManip );
56                keyswitchManipulator->addMatrixManipulator( '6', "Spacemouse Free (Airplane)", new FreeManipulator(_mouse) );
57#endif
58
59                // objectMounted Manipulator for Camera control by Nodes
60                _objectMountedCameraManip = new objectMountedManipulator();
61                keyswitchManipulator->addMatrixManipulator( '7', "Object mounted Camera", _objectMountedCameraManip );
62
63                // Animation path manipulator
64                std::string pathfile = util::getAnimationPathFromXMLConfig(configFilename);
65        char keyForAnimationPath = '8';
66                if( pathfile != "" )
67        {
68            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
69            if (apm || !apm->valid()) 
70            {
71                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
72                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
73                keyswitchManipulator->selectMatrixManipulator(num);
74                ++keyForAnimationPath;
75            }
76        }
77
78        viewer->setCameraManipulator( keyswitchManipulator.get() );
79    }   // If not Slave END
80
81    viewer->addEventHandler( new osgGA::StateSetManipulator(_rootNode->getOrCreateStateSet()) );                // add the state manipulator
82    viewer->addEventHandler(new osgViewer::ThreadingHandler);                           // add the thread model handler
83    viewer->addEventHandler(new osgViewer::WindowSizeHandler);                          // add the window size toggle handler
84    viewer->addEventHandler(new osgViewer::StatsHandler);                                       // add the stats handler
85    viewer->addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));                       // add the help handler
86    viewer->addEventHandler(new osgViewer::RecordCameraPathHandler);            // add the record camera path handler
87    viewer->addEventHandler(new osgViewer::LODScaleHandler);                            // add the LOD Scale handler
88    viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);                       // add the screen capture handler
89
90        // Install Callback
91        _callback = new core_manipulatorCallback(this);
92        viewer->getCamera()->addEventCallback(_callback);
93
94        return true;
95}
96
97void core_manipulator::shutdown()
98{
99        // Remove and Delete Callback
100        _viewer->getCamera()->removeEventCallback(_callback);
101        _callback= NULL;
102
103#ifdef USE_SPACENAVIGATOR
104        //Delete SpaceMouse driver
105        if(_mouse)
106        {
107                _mouse->shutdown();
108                delete _mouse;
109        }
110        _mouseTrackerManip = NULL;
111#endif
112
113        _objectMountedCameraManip = NULL;
114        _nt = NULL;
115        _rootNode = NULL;
116        _viewer = NULL;
117}
118
119void core_manipulator::trackNode( int trackingID )
120{
121        osg::ref_ptr<osg::Node> tmp = visual_object::findNodeByTrackingID(trackingID, _rootNode);
122        if(tmp.valid())
123        {
124                _currentTrackingID = trackingID;
125                trackNode(tmp);
126        }
127}
128
129void core_manipulator::trackNode( osg::Node* node_ )
130{
131        if(!node_)
132                return;
133
134        osg::Node* node = NULL;
135        // Check if tracked node is a visual_object
136        osgVisual::visual_object* trackedObject = dynamic_cast<osgVisual::visual_object*>(node_);
137        if(trackedObject)
138        {
139                node = trackedObject->getGeometry();
140
141                // Object mounted manipulator ( Only working with visual_object, not with osg::Node )
142                if (_objectMountedCameraManip.valid())
143                        _objectMountedCameraManip->setAttachedObject( trackedObject );
144        }
145        else
146                node = node_;
147
148        // Spacemouse Node Tracker
149#ifdef USE_SPACENAVIGATOR
150        if (_mouseTrackerManip.valid())
151        {
152                _mouseTrackerManip->setTrackNode( node );
153                _mouseTrackerManip->setMinimumDistance( 100 );
154        }
155#endif
156
157        // Classical OSG Nodetracker
158        if(_nt.valid())
159        {
160                osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER;
161                osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM;
162                _nt->setTrackerMode(trackerMode);
163                _nt->setRotationMode(rotationMode);
164                _nt->setMinimumDistance( 100 );
165                _nt->setTrackNode( node );
166                _nt->setAutoComputeHomePosition( true );
167                _nt->setDistance( 250 );
168        }
169}
170
171void core_manipulator::core_manipulatorCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
172{
173        //OSG_NOTIFY( osg::ALWAYS ) << "---- Executing core_manipulatorCallback .." <<  std::endl;
174
175        if(!_manipulators->_updaterSlot.empty())
176        {
177                int idToTrack = visual_dataIO::getInstance()->getSlotDataAsDouble(_manipulators->_updaterSlot, osgVisual::dataIO_slot::TO_OBJ );
178                if(idToTrack!=_manipulators->_currentTrackingID)
179                        _manipulators->trackNode(idToTrack);
180        }
181
182        traverse(node, nv);
183}
Note: See TracBrowser for help on using the repository browser.