root/OpenSceneGraph/trunk/examples/osgkeyboardmouse/osgkeyboardmouse.cpp @ 5600

Revision 5600, 10.6 kB (checked in by robert, 8 years ago)

Various clean ups to comments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
2//
3// Simple example of use of Producer::RenderSurface + KeyboardMouseCallback + SceneView
4// example that provides the user with control over view position with basic picking.
5
6#include <Producer/RenderSurface>
7#include <Producer/KeyboardMouse>
8#include <Producer/Trackball>
9
10#include <osg/Timer>
11#include <osg/io_utils>
12#include <osg/observer_ptr>
13
14#include <osgUtil/SceneView>
15#include <osgUtil/IntersectVisitor>
16
17#include <osgDB/ReadFile>
18#include <osgDB/WriteFile>
19
20#include <osgGA/SimpleViewer>
21#include <osgGA/TrackballManipulator>
22#include <osgGA/StateSetManipulator>
23
24#include <osgFX/Scribe>
25
26// ----------- Begining of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events.
27class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback
28{
29public:
30
31    MyKeyboardMouseCallback(osgGA::EventQueue* eventQueue) :
32        _done(false),
33        _eventQueue(eventQueue)
34    {
35    }
36
37    virtual void shutdown()
38    {
39        _done = true;
40    }
41
42    virtual void specialKeyPress( Producer::KeyCharacter key )
43    {
44        if (key==Producer::KeyChar_Escape)
45                    shutdown();
46
47        _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
48    }
49
50    virtual void specialKeyRelease( Producer::KeyCharacter key )
51    {
52        _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
53    }
54
55    virtual void keyPress( Producer::KeyCharacter key)
56    {
57        _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
58    }
59
60    virtual void keyRelease( Producer::KeyCharacter key)
61    {
62        _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
63    }
64
65    virtual void mouseMotion( float mx, float my )
66    {
67        _eventQueue->mouseMotion( mx, my );
68    }
69
70    virtual void buttonPress( float mx, float my, unsigned int mbutton )
71    {
72        _eventQueue->mouseButtonPress(mx, my, mbutton);
73    }
74   
75    virtual void buttonRelease( float mx, float my, unsigned int mbutton )
76    {
77        _eventQueue->mouseButtonRelease(mx, my, mbutton);
78    }
79
80    bool done() { return _done; }
81
82private:
83
84    bool                                _done;
85    osg::ref_ptr<osgGA::EventQueue>     _eventQueue;
86};
87
88class MyActionAdapter : public osgGA::GUIActionAdapter, public osg::Referenced
89{
90public:
91    // Override from GUIActionAdapter
92    virtual void requestRedraw() {}
93
94    // Override from GUIActionAdapter
95    virtual void requestContinuousUpdate(bool =true) {}
96
97    // Override from GUIActionAdapter
98    virtual void requestWarpPointer(float ,float ) {}
99   
100};
101
102// ----------- End of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events.
103
104
105class CreateModelToSaveVisitor : public osg::NodeVisitor
106{
107public:
108
109    CreateModelToSaveVisitor():
110        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)       
111    {
112        _group = new osg::Group;
113        _addToModel = false;
114    }
115   
116    virtual void apply(osg::Node& node)
117    {
118        osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node);
119        if (scribe)
120        {
121            for(unsigned int i=0; i<scribe->getNumChildren(); ++i)
122            {
123                _group->addChild(scribe->getChild(i));
124            }
125        }
126        else
127        {
128            traverse(node);
129        }
130    }
131   
132    osg::ref_ptr<osg::Group> _group;
133    bool _addToModel;
134};
135
136// class to handle events with a pick
137class PickHandler : public osgGA::GUIEventHandler {
138public:
139
140    PickHandler(osgUtil::SceneView* sceneView):
141        _sceneView(sceneView),
142        _mx(0.0),_my(0.0) {}
143
144    ~PickHandler() {}
145
146    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
147    {
148        switch(ea.getEventType())
149        {
150            case(osgGA::GUIEventAdapter::KEYUP):
151            {
152                if (ea.getKey()=='s')
153                {
154                    saveSelectedModel();
155                }
156                return false;
157            }
158            case(osgGA::GUIEventAdapter::PUSH):
159            case(osgGA::GUIEventAdapter::MOVE):
160            {
161                _mx = ea.getX();
162                _my = ea.getY();
163                return false;
164            }
165            case(osgGA::GUIEventAdapter::RELEASE):
166            {
167                if (_mx == ea.getX() && _my == ea.getY())
168                {
169                    // only do a pick if the mouse hasn't moved
170                    pick(ea);
171                }
172                return true;
173            }   
174
175            default:
176                return false;
177        }
178    }
179
180    void pick(const osgGA::GUIEventAdapter& ea)
181    {
182
183        osg::Node* scene = _sceneView.valid() ? _sceneView->getSceneData() : 0;
184        if (!scene) return;
185
186        // remap the mouse x,y into viewport coordinates.
187       
188        float mx = _sceneView->getViewport()->x() + (int)((float)_sceneView->getViewport()->width()*(ea.getXnormalized()*0.5f+0.5f));
189        float my = _sceneView->getViewport()->y() + (int)((float)_sceneView->getViewport()->height()*(ea.getYnormalized()*0.5f+0.5f));
190   
191        // do the pick traversal.
192        osgUtil::PickVisitor pick(_sceneView->getViewport(),
193                                  _sceneView->getProjectionMatrix(),
194                                  _sceneView->getViewMatrix(), mx, my);
195        scene->accept(pick);
196
197        osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList();
198        if (!segHitList.empty() && !segHitList.begin()->second.empty())
199        {
200            std::cout<<"Got hits"<<std::endl;
201
202            // get the hits for the first segment
203            osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second;
204
205            // just take the first hit - nearest the eye point.
206            osgUtil::Hit& hit = hits.front();
207
208            osg::NodePath& nodePath = hit._nodePath;
209            osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
210            osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
211
212            if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
213
214            // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
215            if (parent && node)
216            {
217
218                std::cout<<"  parent "<<parent->className()<<std::endl;
219
220                osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
221                if (!parentAsScribe)
222                {
223                    // node not already picked, so highlight it with an osgFX::Scribe
224                    osgFX::Scribe* scribe = new osgFX::Scribe();
225                    scribe->addChild(node);
226                    parent->replaceChild(node,scribe);
227                }
228                else
229                {
230                    // node already picked so we want to remove scribe to unpick it.
231                    osg::Node::ParentList parentList = parentAsScribe->getParents();
232                    for(osg::Node::ParentList::iterator itr=parentList.begin();
233                        itr!=parentList.end();
234                        ++itr)
235                    {
236                        (*itr)->replaceChild(parentAsScribe,node);
237                    }
238                }
239            }
240
241        }
242    }
243
244    void saveSelectedModel()
245    {
246        CreateModelToSaveVisitor cmtsv;
247        _sceneView->getSceneData()->accept(cmtsv);
248       
249        if (cmtsv._group->getNumChildren()>0)
250        {
251            std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl;
252            osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg");
253        }
254    }
255
256protected:
257
258    osg::observer_ptr<osgUtil::SceneView> _sceneView;
259    float _mx,_my;
260};
261
262int main( int argc, char **argv )
263{
264    if (argc<2)
265    {
266        std::cout << argv[0] <<": requires filename argument." << std::endl;
267        return 1;
268    }
269
270    // load the scene.
271    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
272    if (!loadedModel)
273    {
274        std::cout << argv[0] <<": No data loaded." << std::endl;
275        return 1;
276    }
277   
278    // create the window to draw to.
279    osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface;
280    renderSurface->setWindowName("osgkeyboardmouse");
281    renderSurface->setWindowRectangle(100,100,800,600);
282    renderSurface->useBorder(true);
283    renderSurface->realize();
284   
285
286    // create the view of the scene.
287    osgGA::SimpleViewer viewer;
288    viewer.setSceneData(loadedModel.get());
289   
290    // set up a KeyboardMouse to manage the events comming in from the RenderSurface
291    osg::ref_ptr<Producer::KeyboardMouse>  kbm = new Producer::KeyboardMouse(renderSurface.get());
292
293    // create a KeyboardMouseCallback to handle the mouse events within this applications
294    osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(viewer.getEventQueue());
295
296    // create a tracball manipulator to move the camera around in response to keyboard/mouse events
297    viewer.setCameraManipulator( new osgGA::TrackballManipulator );
298
299    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
300    statesetManipulator->setStateSet(viewer.getSceneView()->getGlobalStateSet());
301    viewer.addEventHandler(statesetManipulator.get());
302
303    // add the pick handler
304    viewer.addEventHandler(new PickHandler(viewer.getSceneView()));
305   
306    // set the window dimensions
307    viewer.getEventQueue()->getCurrentEventState()->setWindowRectangle(100,100,800,600);
308
309    // set the mouse input range.
310    // Producer defaults to using non-dimensional units, so we pass this onto osgGA, most windowing toolkits use pixel coords so use the window size instead.
311    viewer.getEventQueue()->getCurrentEventState()->setInputRange(-1.0, -1.0, 1.0, 1.0);
312
313    // Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits.
314    // we need to construct the event queue so that it knows about this convention.
315    viewer.getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);
316
317    viewer.init();
318
319    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
320    while( renderSurface->isRealized() && !kbmcb->done())
321    {
322        // update the window dimensions, in case the window has been resized.
323         viewer.getEventQueue()->windowResize(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight(), false);
324
325        // pass any keyboard mouse events onto the local keyboard mouse callback.
326        kbm->update( *kbmcb );
327       
328        viewer.frame();
329
330        // Swap Buffers
331        renderSurface->swapBuffers();
332    }
333
334    return 0;
335}
Note: See TracBrowser for help on using the browser.