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

Revision 5683, 12.7 kB (checked in by robert, 7 years ago)

Added basic PolytopeIntersector? functionality based on checking vertices against
polytopes.

  • 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
16#include <osgDB/ReadFile>
17#include <osgDB/WriteFile>
18
19#include <osgGA/TrackballManipulator>
20#include <osgGA/StateSetManipulator>
21
22#include <osgViewer/SimpleViewer>
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():
141        _mx(0.0),_my(0.0) {}
142
143    ~PickHandler() {}
144
145    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
146    {
147        osgViewer::SimpleViewer* viewer = dynamic_cast<osgViewer::SimpleViewer*>(&aa);
148
149        switch(ea.getEventType())
150        {
151            case(osgGA::GUIEventAdapter::KEYUP):
152            {
153                if (ea.getKey()=='s' && viewer)
154                {
155                    saveSelectedModel(viewer->getSceneData());
156                }
157                return false;
158            }
159            case(osgGA::GUIEventAdapter::PUSH):
160            case(osgGA::GUIEventAdapter::MOVE):
161            {
162                _mx = ea.getX();
163                _my = ea.getY();
164                return false;
165            }
166            case(osgGA::GUIEventAdapter::RELEASE):
167            {
168                if (_mx == ea.getX() && _my == ea.getY())
169                {
170                    // only do a pick if the mouse hasn't moved
171                    pick(ea,viewer);
172                }
173                return true;
174            }   
175
176            default:
177                return false;
178        }
179    }
180
181    void pick(const osgGA::GUIEventAdapter& ea, osgViewer::SimpleViewer* viewer)
182    {
183        osg::Node* scene = viewer->getSceneData();
184        if (!scene) return;
185
186        osg::notify(osg::NOTICE)<<std::endl;
187
188        osg::Node* node = 0;
189        osg::Group* parent = 0;
190
191        bool usePolytopePicking = true;
192        if (usePolytopePicking)
193        {
194            // use window coordinates
195            // remap the mouse x,y into viewport coordinates.
196            osg::Viewport* viewport = viewer->getCamera()->getViewport();
197            float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f));
198            float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f));
199
200            float width = 10.0f;
201            float height = 10.0f;
202
203            float min_x = mx - width*0.5;
204            float max_x = mx + width*0.5;
205            float min_y = my - height*0.5;
206            float max_y = my + height*0.5;
207
208            osg::Polytope polytope;
209            polytope.add(osg::Plane(1,0,0,-min_x));
210            polytope.add(osg::Plane(-1,0,0,max_x));
211            polytope.add(osg::Plane(0,1,0,-min_y));
212            polytope.add(osg::Plane(0,-1,0,max_y));
213            polytope.add(osg::Plane(0,0,1,0));
214
215            osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, polytope );
216
217            osgUtil::IntersectionVisitor iv(picker);
218
219            viewer->getCamera()->accept(iv);
220
221            if (picker->containsIntersections())
222            {
223                osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection();
224
225                osg::NodePath& nodePath = intersection.nodePath;
226                node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
227                parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
228
229                if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
230
231            }
232
233        }
234        else
235        {
236
237            #if 0
238            // use non dimension coordinates - in projection/clip space
239            osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),-1.0), osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),1.0) );
240            #else
241            // use window coordinates
242            // remap the mouse x,y into viewport coordinates.
243            osg::Viewport* viewport = viewer->getCamera()->getViewport();
244            float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f));
245            float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f));
246            osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) );
247            #endif
248
249            osgUtil::IntersectionVisitor iv(picker);
250
251            viewer->getCamera()->accept(iv);
252
253            if (picker->containsIntersections())
254            {
255                osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection();
256                osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl;
257
258                osg::NodePath& nodePath = intersection.nodePath;
259                node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
260                parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
261
262                if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
263
264            }
265        }       
266
267        // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
268        if (parent && node)
269        {
270
271            std::cout<<"  parent "<<parent->className()<<std::endl;
272
273            osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
274            if (!parentAsScribe)
275            {
276                // node not already picked, so highlight it with an osgFX::Scribe
277                osgFX::Scribe* scribe = new osgFX::Scribe();
278                scribe->addChild(node);
279                parent->replaceChild(node,scribe);
280            }
281            else
282            {
283                // node already picked so we want to remove scribe to unpick it.
284                osg::Node::ParentList parentList = parentAsScribe->getParents();
285                for(osg::Node::ParentList::iterator itr=parentList.begin();
286                    itr!=parentList.end();
287                    ++itr)
288                {
289                    (*itr)->replaceChild(parentAsScribe,node);
290                }
291            }
292        }
293    }
294
295    void saveSelectedModel(osg::Node* scene)
296    {
297        if (!scene) return;
298   
299        CreateModelToSaveVisitor cmtsv;
300        scene->accept(cmtsv);
301       
302        if (cmtsv._group->getNumChildren()>0)
303        {
304            std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl;
305            osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg");
306        }
307    }
308
309protected:
310
311    float _mx,_my;
312};
313
314int main( int argc, char **argv )
315{
316    if (argc<2)
317    {
318        std::cout << argv[0] <<": requires filename argument." << std::endl;
319        return 1;
320    }
321
322    // load the scene.
323    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
324    if (!loadedModel)
325    {
326        std::cout << argv[0] <<": No data loaded." << std::endl;
327        return 1;
328    }
329   
330    // create the window to draw to.
331    osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface;
332    renderSurface->setWindowName("osgkeyboardmouse");
333    renderSurface->setWindowRectangle(100,100,800,600);
334    renderSurface->useBorder(true);
335    renderSurface->realize();
336   
337
338    // create the view of the scene.
339    osgViewer::SimpleViewer viewer;
340    viewer.setSceneData(loadedModel.get());
341   
342    // set up a KeyboardMouse to manage the events comming in from the RenderSurface
343    osg::ref_ptr<Producer::KeyboardMouse>  kbm = new Producer::KeyboardMouse(renderSurface.get());
344
345    // create a KeyboardMouseCallback to handle the mouse events within this applications
346    osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(viewer.getEventQueue());
347
348    // create a tracball manipulator to move the camera around in response to keyboard/mouse events
349    viewer.setCameraManipulator( new osgGA::TrackballManipulator );
350
351    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
352    statesetManipulator->setStateSet(viewer.getSceneView()->getGlobalStateSet());
353    viewer.addEventHandler(statesetManipulator.get());
354
355    // add the pick handler
356    viewer.addEventHandler(new PickHandler());
357   
358    // set the window dimensions
359    viewer.getEventQueue()->getCurrentEventState()->setWindowRectangle(100,100,800,600);
360
361    // set the mouse input range.
362    // 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.
363    viewer.getEventQueue()->getCurrentEventState()->setInputRange(-1.0, -1.0, 1.0, 1.0);
364
365    // Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits.
366    // we need to construct the event queue so that it knows about this convention.
367    viewer.getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);
368
369    viewer.init();
370
371    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
372    while( renderSurface->isRealized() && !kbmcb->done())
373    {
374        // update the window dimensions, in case the window has been resized.
375         viewer.getEventQueue()->windowResize(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight(), false);
376
377        // pass any keyboard mouse events onto the local keyboard mouse callback.
378        kbm->update( *kbmcb );
379       
380        viewer.frame();
381
382        // Swap Buffers
383        renderSurface->swapBuffers();
384    }
385
386    return 0;
387}
Note: See TracBrowser for help on using the browser.