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

Revision 5174, 14.1 kB (checked in by robert, 8 years ago)

Added pick handler.

  • 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/EventQueue>
21#include <osgGA/EventVisitor>
22#include <osgGA/TrackballManipulator>
23#include <osgGA/StateSetManipulator>
24
25#include <osgFX/Scribe>
26
27// ----------- Begining of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events.
28class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback
29{
30public:
31
32    MyKeyboardMouseCallback(osgGA::EventQueue* eventQueue) :
33        _done(false),
34        _eventQueue(eventQueue)
35    {
36    }
37
38    virtual void shutdown()
39    {
40        _done = true;
41    }
42
43    virtual void specialKeyPress( Producer::KeyCharacter key )
44    {
45        if (key==Producer::KeyChar_Escape)
46                    shutdown();
47
48        _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
49    }
50
51    virtual void specialKeyRelease( Producer::KeyCharacter key )
52    {
53        _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
54    }
55
56    virtual void keyPress( Producer::KeyCharacter key)
57    {
58        _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
59    }
60
61    virtual void keyRelease( Producer::KeyCharacter key)
62    {
63        _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
64    }
65
66    virtual void mouseMotion( float mx, float my )
67    {
68        _eventQueue->mouseMotion( mx, my );
69    }
70
71    virtual void buttonPress( float mx, float my, unsigned int mbutton )
72    {
73        _eventQueue->mouseButtonPress(mx, my, mbutton);
74    }
75   
76    virtual void buttonRelease( float mx, float my, unsigned int mbutton )
77    {
78        _eventQueue->mouseButtonRelease(mx, my, mbutton);
79    }
80
81    bool done() { return _done; }
82
83private:
84
85    bool                                _done;
86    osg::ref_ptr<osgGA::EventQueue>     _eventQueue;
87};
88
89class MyActionAdapter : public osgGA::GUIActionAdapter, public osg::Referenced
90{
91public:
92    // Override from GUIActionAdapter
93    virtual void requestRedraw() {}
94
95    // Override from GUIActionAdapter
96    virtual void requestContinuousUpdate(bool =true) {}
97
98    // Override from GUIActionAdapter
99    virtual void requestWarpPointer(float ,float ) {}
100   
101};
102
103// ----------- End of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events.
104
105
106class CreateModelToSaveVisitor : public osg::NodeVisitor
107{
108public:
109
110    CreateModelToSaveVisitor():
111        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)       
112    {
113        _group = new osg::Group;
114        _addToModel = false;
115    }
116   
117    virtual void apply(osg::Node& node)
118    {
119        osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node);
120        if (scribe)
121        {
122            for(unsigned int i=0; i<scribe->getNumChildren(); ++i)
123            {
124                _group->addChild(scribe->getChild(i));
125            }
126        }
127        else
128        {
129            traverse(node);
130        }
131    }
132   
133    osg::ref_ptr<osg::Group> _group;
134    bool _addToModel;
135};
136
137// class to handle events with a pick
138class PickHandler : public osgGA::GUIEventHandler {
139public:
140
141    PickHandler(osgUtil::SceneView* sceneView):
142        _sceneView(sceneView),
143        _mx(0.0),_my(0.0) {}
144
145    ~PickHandler() {}
146
147    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
148    {
149        switch(ea.getEventType())
150        {
151            case(osgGA::GUIEventAdapter::KEYUP):
152            {
153                if (ea.getKey()=='s')
154                {
155                    saveSelectedModel();
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);
172                }
173                return true;
174            }   
175
176            default:
177                return false;
178        }
179    }
180
181    void pick(const osgGA::GUIEventAdapter& ea)
182    {
183
184        osg::Node* scene = _sceneView.valid() ? _sceneView->getSceneData() : 0;
185        if (!scene) return;
186
187        // remap the mouse x,y into viewport coordinates.
188       
189        float mx = _sceneView->getViewport()->x() + (int)((float)_sceneView->getViewport()->width()*(ea.getXnormalized()*0.5f+0.5f));
190        float my = _sceneView->getViewport()->y() + (int)((float)_sceneView->getViewport()->height()*(ea.getYnormalized()*0.5f+0.5f));
191   
192        // do the pick traversal.
193        osgUtil::PickVisitor pick(_sceneView->getViewport(),
194                                  _sceneView->getProjectionMatrix(),
195                                  _sceneView->getViewMatrix(), mx, my);
196        scene->accept(pick);
197
198        osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList();
199        if (!segHitList.empty() && !segHitList.begin()->second.empty())
200        {
201            std::cout<<"Got hits"<<std::endl;
202
203            // get the hits for the first segment
204            osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second;
205
206            // just take the first hit - nearest the eye point.
207            osgUtil::Hit& hit = hits.front();
208
209            osg::NodePath& nodePath = hit._nodePath;
210            osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
211            osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
212
213            if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
214
215            // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
216            if (parent && node)
217            {
218
219                std::cout<<"  parent "<<parent->className()<<std::endl;
220
221                osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
222                if (!parentAsScribe)
223                {
224                    // node not already picked, so highlight it with an osgFX::Scribe
225                    osgFX::Scribe* scribe = new osgFX::Scribe();
226                    scribe->addChild(node);
227                    parent->replaceChild(node,scribe);
228                }
229                else
230                {
231                    // node already picked so we want to remove scribe to unpick it.
232                    osg::Node::ParentList parentList = parentAsScribe->getParents();
233                    for(osg::Node::ParentList::iterator itr=parentList.begin();
234                        itr!=parentList.end();
235                        ++itr)
236                    {
237                        (*itr)->replaceChild(parentAsScribe,node);
238                    }
239                }
240            }
241
242        }
243    }
244
245    void saveSelectedModel()
246    {
247        CreateModelToSaveVisitor cmtsv;
248        _sceneView->getSceneData()->accept(cmtsv);
249       
250        if (cmtsv._group->getNumChildren()>0)
251        {
252            std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl;
253            osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg");
254        }
255    }
256
257protected:
258
259    osg::observer_ptr<osgUtil::SceneView> _sceneView;
260    float _mx,_my;
261};
262
263int main( int argc, char **argv )
264{
265    if (argc<2)
266    {
267        std::cout << argv[0] <<": requires filename argument." << std::endl;
268        return 1;
269    }
270
271    // load the scene.
272    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
273    if (!loadedModel)
274    {
275        std::cout << argv[0] <<": No data loaded." << std::endl;
276        return 1;
277    }
278   
279    // create the window to draw to.
280    osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface;
281    renderSurface->setWindowName("osgkeyboardmouse");
282    renderSurface->setWindowRectangle(100,100,800,600);
283    renderSurface->useBorder(true);
284    renderSurface->realize();
285   
286
287    // create the view of the scene.
288    osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView;
289    sceneView->setDefaults();
290    sceneView->setSceneData(loadedModel.get());
291   
292    // create the event queue, note that Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits, so
293    // we need to construct the event queue so that it knows about this convention.
294    osg::ref_ptr<osgGA::EventQueue> eventQueue = new osgGA::EventQueue(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);
295
296    // set up a KeyboardMouse to manage the events comming in from the RenderSurface
297    osg::ref_ptr<Producer::KeyboardMouse>  kbm = new Producer::KeyboardMouse(renderSurface.get());
298
299    // create a KeyboardMouseCallback to handle the mouse events within this applications
300    osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(eventQueue.get());
301
302    // create a tracball manipulator to move the camera around in response to keyboard/mouse events
303    osg::ref_ptr<osgGA::TrackballManipulator> cameraManipulator = new osgGA::TrackballManipulator;
304
305    // keep a list of event handlers to manipulate the application/scene with in response to keyboard/mouse events
306    typedef std::list< osg::ref_ptr<osgGA::GUIEventHandler> > EventHandlers;
307    EventHandlers eventHandlers;
308   
309    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
310    statesetManipulator->setStateSet(sceneView->getGlobalStateSet());
311    eventHandlers.push_back(statesetManipulator.get());
312    eventHandlers.push_back(new PickHandler(sceneView.get()));
313   
314    // create an event visitor to pass the events down to the scene graph nodes
315    osg::ref_ptr<osgGA::EventVisitor> eventVisitor = new osgGA::EventVisitor;
316
317    // create an action adapter to allow event handlers to request actions from the GUI.
318    osg::ref_ptr<MyActionAdapter> actionAdapter = new MyActionAdapter;
319
320    // record the timer tick at the start of rendering.   
321    osg::Timer_t start_tick = osg::Timer::instance()->tick();
322   
323    unsigned int frameNum = 0;
324
325    eventQueue->setStartTick(start_tick);
326
327    // set the mouse input range (note WindowSize name in appropriate here so osgGA::GUIEventAdapter API really needs looking at, Robert Osfield, June 2006).
328    // 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.
329    eventQueue->getCurrentEventState()->setWindowSize(-1.0, -1.0, 1.0, 1.0);
330
331
332    // home the manipulator.
333    osg::ref_ptr<osgGA::GUIEventAdapter> dummyEvent =  eventQueue->createEvent();
334    cameraManipulator->setNode(sceneView->getSceneData());
335    cameraManipulator->home(*dummyEvent, *actionAdapter);
336
337
338    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
339    while( renderSurface->isRealized() && !kbmcb->done())
340    {
341        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
342        osg::FrameStamp* frameStamp = new osg::FrameStamp;
343        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
344        frameStamp->setFrameNumber(frameNum++);
345       
346        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
347        sceneView->setFrameStamp(frameStamp);
348
349        // pass any keyboard mouse events onto the local keyboard mouse callback.
350        kbm->update( *kbmcb );
351       
352        // create an event to signal the new frame.
353        eventQueue->frame(frameStamp->getReferenceTime());
354
355        // get the event since the last frame.
356        osgGA::EventQueue::Events events;
357        eventQueue->takeEvents(events);
358
359        if (eventVisitor.valid())
360        {
361            eventVisitor->setTraversalNumber(frameStamp->getFrameNumber());
362        }
363
364        // dispatch the events in order of arrival.
365        for(osgGA::EventQueue::Events::iterator event_itr = events.begin();
366            event_itr != events.end();
367            ++event_itr)
368        {
369            bool handled = false;
370
371            if (eventVisitor.valid() && sceneView->getSceneData())
372            {
373                eventVisitor->reset();
374                eventVisitor->addEvent(event_itr->get());
375                sceneView->getSceneData()->accept(*eventVisitor);
376                if (eventVisitor->getEventHandled())
377                    handled = true;
378            }
379
380            if (cameraManipulator.valid() && !handled)
381            {
382                /*handled =*/ cameraManipulator->handle(*(*event_itr), *actionAdapter);
383            }
384
385            for(EventHandlers::iterator handler_itr=eventHandlers.begin();
386                handler_itr!=eventHandlers.end() && !handled;
387                ++handler_itr)
388            {   
389                handled = (*handler_itr)->handle(*(*event_itr),*actionAdapter,0,0);
390            }
391
392            // osg::notify(osg::NOTICE)<<"  Handled event "<<(*event_itr)->getTime()<<" "<< handled<<std::endl;
393
394        }
395
396
397        // update view matrices
398        if (cameraManipulator.valid())
399        {
400            sceneView->setViewMatrix(cameraManipulator->getInverseMatrix());
401        }
402
403        // update the viewport dimensions, incase the window has been resized.
404        sceneView->setViewport(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight());
405
406        // do the update traversal the scene graph - such as updating animations
407        sceneView->update();
408       
409        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins
410        sceneView->cull();
411       
412        // draw the rendering bins.
413        sceneView->draw();
414
415        // Swap Buffers
416        renderSurface->swapBuffers();
417    }
418
419    return 0;
420}
Note: See TracBrowser for help on using the browser.