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

Revision 5832, 12.8 kB (checked in by robert, 8 years ago)

Further work osgViewer::Viewer and related classes.

  • 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 + SimpleViewer
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/IntersectionVisitor>
15#include <osgUtil/PolytopeIntersector>
16#include <osgUtil/LineSegmentIntersector>
17
18#include <osgDB/ReadFile>
19#include <osgDB/WriteFile>
20
21#include <osgGA/TrackballManipulator>
22#include <osgGA/StateSetManipulator>
23
24#include <osgViewer/SimpleViewer>
25
26#include <osgFX/Scribe>
27
28// ----------- Begining of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events.
29class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback
30{
31public:
32
33    MyKeyboardMouseCallback(osgGA::EventQueue* eventQueue) :
34        _done(false),
35        _eventQueue(eventQueue)
36    {
37    }
38
39    virtual void shutdown()
40    {
41        _done = true;
42    }
43
44    virtual void specialKeyPress( Producer::KeyCharacter key )
45    {
46        if (key==Producer::KeyChar_Escape)
47                    shutdown();
48
49        _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
50    }
51
52    virtual void specialKeyRelease( Producer::KeyCharacter key )
53    {
54        _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
55    }
56
57    virtual void keyPress( Producer::KeyCharacter key)
58    {
59        _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
60    }
61
62    virtual void keyRelease( Producer::KeyCharacter key)
63    {
64        _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
65    }
66
67    virtual void mouseMotion( float mx, float my )
68    {
69        _eventQueue->mouseMotion( mx, my );
70    }
71
72    virtual void buttonPress( float mx, float my, unsigned int mbutton )
73    {
74        _eventQueue->mouseButtonPress(mx, my, mbutton);
75    }
76   
77    virtual void buttonRelease( float mx, float my, unsigned int mbutton )
78    {
79        _eventQueue->mouseButtonRelease(mx, my, mbutton);
80    }
81
82    bool done() { return _done; }
83
84private:
85
86    bool                                _done;
87    osg::ref_ptr<osgGA::EventQueue>     _eventQueue;
88};
89
90class MyActionAdapter : public osgGA::GUIActionAdapter, public osg::Referenced
91{
92public:
93    // Override from GUIActionAdapter
94    virtual void requestRedraw() {}
95
96    // Override from GUIActionAdapter
97    virtual void requestContinuousUpdate(bool =true) {}
98
99    // Override from GUIActionAdapter
100    virtual void requestWarpPointer(float ,float ) {}
101   
102};
103
104// ----------- End of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events.
105
106
107class CreateModelToSaveVisitor : public osg::NodeVisitor
108{
109public:
110
111    CreateModelToSaveVisitor():
112        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)       
113    {
114        _group = new osg::Group;
115        _addToModel = false;
116    }
117   
118    virtual void apply(osg::Node& node)
119    {
120        osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node);
121        if (scribe)
122        {
123            for(unsigned int i=0; i<scribe->getNumChildren(); ++i)
124            {
125                _group->addChild(scribe->getChild(i));
126            }
127        }
128        else
129        {
130            traverse(node);
131        }
132    }
133   
134    osg::ref_ptr<osg::Group> _group;
135    bool _addToModel;
136};
137
138// class to handle events with a pick
139class PickHandler : public osgGA::GUIEventHandler {
140public:
141
142    PickHandler():
143        _mx(0.0),_my(0.0) {}
144
145    ~PickHandler() {}
146
147    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
148    {
149        osgViewer::SimpleViewer* viewer = dynamic_cast<osgViewer::SimpleViewer*>(&aa);
150
151        switch(ea.getEventType())
152        {
153            case(osgGA::GUIEventAdapter::KEYUP):
154            {
155                if (ea.getKey()=='s' && viewer)
156                {
157                    saveSelectedModel(viewer->getSceneData());
158                }
159                return false;
160            }
161            case(osgGA::GUIEventAdapter::PUSH):
162            case(osgGA::GUIEventAdapter::MOVE):
163            {
164                _mx = ea.getX();
165                _my = ea.getY();
166               
167                osg::notify(osg::NOTICE)<<"_mx="<<_mx<<" _my="<<_my<<std::endl;
168                osg::notify(osg::NOTICE)<<"  range ="<<ea.getXmin()<<", "<<ea.getXmax()<<std::endl;
169               
170                return false;
171            }
172            case(osgGA::GUIEventAdapter::RELEASE):
173            {
174                if (_mx == ea.getX() && _my == ea.getY())
175                {
176                    // only do a pick if the mouse hasn't moved
177                    pick(ea,viewer);
178                }
179                return true;
180            }   
181
182            default:
183                return false;
184        }
185    }
186
187    void pick(const osgGA::GUIEventAdapter& ea, osgViewer::SimpleViewer* viewer)
188    {
189        osg::Node* scene = viewer->getSceneData();
190        if (!scene) return;
191
192        osg::notify(osg::NOTICE)<<std::endl;
193
194        osg::Node* node = 0;
195        osg::Group* parent = 0;
196
197        bool usePolytopePicking = false;
198        if (usePolytopePicking)
199        {
200
201#if 0
202            // use window coordinates
203            // remap the mouse x,y into viewport coordinates.
204            osg::Viewport* viewport = viewer->getCamera()->getViewport();
205            double mx = viewport->x() + (int)((double )viewport->width()*(ea.getXnormalized()*0.5+0.5));
206            double my = viewport->y() + (int)((double )viewport->height()*(ea.getYnormalized()*0.5+0.5));
207
208            // half width, height.
209            double w = 5.0f;
210            double h = 5.0f;
211            osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h );
212#else
213            double mx = ea.getXnormalized();
214            double my = ea.getYnormalized();
215            double w = 0.05;
216            double h = 0.05;
217            osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::PROJECTION, mx-w, my-h, mx+w, my+h );
218#endif
219            osgUtil::IntersectionVisitor iv(picker);
220
221            viewer->getCamera()->accept(iv);
222
223            if (picker->containsIntersections())
224            {
225                osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection();
226
227                osg::NodePath& nodePath = intersection.nodePath;
228                node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
229                parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
230
231                if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
232
233            }
234
235        }
236        else
237        {
238
239            #if 0
240            // use non dimensional coordinates - in projection/clip space
241            osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::PROJECTION, ea.getXnormalized(),ea.getYnormalized() );
242            #else
243            // use window coordinates
244            // remap the mouse x,y into viewport coordinates.
245            osg::Viewport* viewport = viewer->getCamera()->getViewport();
246            float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f));
247            float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f));
248            osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, mx, my );
249            #endif
250
251            osgUtil::IntersectionVisitor iv(picker);
252
253            viewer->getCamera()->accept(iv);
254
255            if (picker->containsIntersections())
256            {
257                osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection();
258                osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl;
259
260                osg::NodePath& nodePath = intersection.nodePath;
261                node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
262                parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
263
264                if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
265
266            }
267        }       
268
269        // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
270        if (parent && node)
271        {
272
273            std::cout<<"  parent "<<parent->className()<<std::endl;
274
275            osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
276            if (!parentAsScribe)
277            {
278                // node not already picked, so highlight it with an osgFX::Scribe
279                osgFX::Scribe* scribe = new osgFX::Scribe();
280                scribe->addChild(node);
281                parent->replaceChild(node,scribe);
282            }
283            else
284            {
285                // node already picked so we want to remove scribe to unpick it.
286                osg::Node::ParentList parentList = parentAsScribe->getParents();
287                for(osg::Node::ParentList::iterator itr=parentList.begin();
288                    itr!=parentList.end();
289                    ++itr)
290                {
291                    (*itr)->replaceChild(parentAsScribe,node);
292                }
293            }
294        }
295    }
296
297    void saveSelectedModel(osg::Node* scene)
298    {
299        if (!scene) return;
300   
301        CreateModelToSaveVisitor cmtsv;
302        scene->accept(cmtsv);
303       
304        if (cmtsv._group->getNumChildren()>0)
305        {
306            std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl;
307            osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg");
308        }
309    }
310
311protected:
312
313    float _mx,_my;
314};
315
316int main( int argc, char **argv )
317{
318    if (argc<2)
319    {
320        std::cout << argv[0] <<": requires filename argument." << std::endl;
321        return 1;
322    }
323
324    // load the scene.
325    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
326    if (!loadedModel)
327    {
328        std::cout << argv[0] <<": No data loaded." << std::endl;
329        return 1;
330    }
331   
332    // create the window to draw to.
333    osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface;
334    renderSurface->setWindowName("osgkeyboardmouse");
335    renderSurface->setWindowRectangle(100,100,800,600);
336    renderSurface->useBorder(true);
337    renderSurface->realize();
338   
339
340    // create the view of the scene.
341    osgViewer::SimpleViewer viewer;
342    viewer.setSceneData(loadedModel.get());
343   
344    // set up a KeyboardMouse to manage the events comming in from the RenderSurface
345    osg::ref_ptr<Producer::KeyboardMouse>  kbm = new Producer::KeyboardMouse(renderSurface.get());
346
347    // create a KeyboardMouseCallback to handle the mouse events within this applications
348    osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(viewer.getEventQueue());
349
350    // create a tracball manipulator to move the camera around in response to keyboard/mouse events
351    viewer.setCameraManipulator( new osgGA::TrackballManipulator );
352
353    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
354    statesetManipulator->setStateSet(viewer.getSceneView()->getGlobalStateSet());
355    viewer.addEventHandler(statesetManipulator.get());
356
357    // add the pick handler
358    viewer.addEventHandler(new PickHandler());
359   
360    // set the window dimensions
361    viewer.getEventQueue()->getCurrentEventState()->setWindowRectangle(100,100,800,600);
362
363    // set the mouse input range.
364    // 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.
365    viewer.getEventQueue()->setUseFixedMouseInputRange(true);
366    viewer.getEventQueue()->setMouseInputRange(-1.0, -1.0, 1.0, 1.0);
367
368    // Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits.
369    // we need to construct the event queue so that it knows about this convention.
370    viewer.getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);
371   
372
373    viewer.init();
374
375    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
376    while( renderSurface->isRealized() && !kbmcb->done())
377    {
378        // update the window dimensions, in case the window has been resized.
379         viewer.getEventQueue()->windowResize(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight());
380
381        // pass any keyboard mouse events onto the local keyboard mouse callback.
382        kbm->update( *kbmcb );
383       
384        viewer.frame();
385
386        // Swap Buffers
387        renderSurface->swapBuffers();
388    }
389
390    return 0;
391}
Note: See TracBrowser for help on using the browser.