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

Revision 14241, 13.6 kB (checked in by robert, 4 days ago)

Moved widgets from VolumeEditorWidget? to TransferFunctionWidget?, and widget utilities into WidgetUtils?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[6941]1/* OpenSceneGraph example, osgkeyboardmouse.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
[6817]19// Simple example of use of osgViewer::GraphicsWindow + Viewer
[2540]20// example that provides the user with control over view position with basic picking.
21
22#include <osg/Timer>
[3994]23#include <osg/io_utils>
[5174]24#include <osg/observer_ptr>
[2540]25
[5676]26#include <osgUtil/IntersectionVisitor>
[5764]27#include <osgUtil/PolytopeIntersector>
28#include <osgUtil/LineSegmentIntersector>
[2540]29
30#include <osgDB/ReadFile>
[4955]31#include <osgDB/WriteFile>
[2540]32
[5173]33#include <osgGA/TrackballManipulator>
34#include <osgGA/StateSetManipulator>
35
[6817]36#include <osgViewer/Viewer>
[5679]37
[2540]38#include <osgFX/Scribe>
39
[5962]40#include <iostream>
[4955]41
[5174]42class CreateModelToSaveVisitor : public osg::NodeVisitor
43{
44public:
45
46    CreateModelToSaveVisitor():
[14195]47        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
[5174]48    {
49        _group = new osg::Group;
50        _addToModel = false;
51    }
[14195]52
[5174]53    virtual void apply(osg::Node& node)
54    {
55        osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node);
56        if (scribe)
57        {
58            for(unsigned int i=0; i<scribe->getNumChildren(); ++i)
59            {
60                _group->addChild(scribe->getChild(i));
61            }
62        }
63        else
64        {
65            traverse(node);
66        }
67    }
[14195]68
[5174]69    osg::ref_ptr<osg::Group> _group;
70    bool _addToModel;
71};
72
[6034]73class DeleteSelectedNodesVisitor : public osg::NodeVisitor
74{
75public:
76
77    DeleteSelectedNodesVisitor():
[14195]78        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
[6034]79    {
80    }
[14195]81
[6034]82    virtual void apply(osg::Node& node)
83    {
84        osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node);
85        if (scribe)
86        {
87            _selectedNodes.push_back(scribe);
88        }
89        else
90        {
91            traverse(node);
92        }
93    }
[14195]94
[6034]95    void pruneSelectedNodes()
96    {
97        for(SelectedNodes::iterator itr = _selectedNodes.begin();
98            itr != _selectedNodes.end();
99            ++itr)
100        {
101            osg::Node* node = itr->get();
102            osg::Node::ParentList parents = node->getParents();
103            for(osg::Node::ParentList::iterator pitr = parents.begin();
104                pitr != parents.end();
105                ++pitr)
106            {
[14241]107                (*pitr)->removeChild(node);
[6034]108            }
109        }
110    }
[14195]111
[6034]112    typedef std::vector< osg::ref_ptr<osgFX::Scribe> > SelectedNodes;
113    SelectedNodes _selectedNodes;
[14195]114
[6034]115};
116
[5174]117// class to handle events with a pick
[14195]118class PickHandler : public osgGA::GUIEventHandler
[5962]119{
[14195]120public:
[5174]121
[5679]122    PickHandler():
[6734]123        _mx(0.0),_my(0.0),
124        _usePolytopeIntersector(false),
125        _useWindowCoordinates(false) {}
[5174]126
127    ~PickHandler() {}
128
[5679]129    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
[5174]130    {
[6817]131        osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
[6034]132        if (!viewer) return false;
[5679]133
[5174]134        switch(ea.getEventType())
135        {
136            case(osgGA::GUIEventAdapter::KEYUP):
137            {
[6034]138                if (ea.getKey()=='s')
[5174]139                {
[5679]140                    saveSelectedModel(viewer->getSceneData());
[14195]141                }
[6034]142                else if (ea.getKey()=='o')
143                {
[12529]144                    osg::notify(osg::NOTICE)<<"Saved model to file 'saved_model.osgt'"<<std::endl;
145                    osgDB::writeNodeFile(*(viewer->getSceneData()), "saved_model.osgt");
[5174]146                }
[6734]147                else if (ea.getKey()=='p')
148                {
149                    _usePolytopeIntersector = !_usePolytopeIntersector;
150                    if (_usePolytopeIntersector)
151                    {
152                        osg::notify(osg::NOTICE)<<"Using PolytopeIntersector"<<std::endl;
153                    } else {
154                        osg::notify(osg::NOTICE)<<"Using LineSegmentIntersector"<<std::endl;
155                    }
156                }
157                else if (ea.getKey()=='c')
158                {
159                    _useWindowCoordinates = !_useWindowCoordinates;
160                    if (_useWindowCoordinates)
161                    {
162                        osg::notify(osg::NOTICE)<<"Using window coordinates for picking"<<std::endl;
163                    } else {
164                        osg::notify(osg::NOTICE)<<"Using projection coordiates for picking"<<std::endl;
165                    }
166                }
[6034]167                else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Delete || ea.getKey()==osgGA::GUIEventAdapter::KEY_BackSpace)
168                {
169                    osg::notify(osg::NOTICE)<<"Delete"<<std::endl;
170                    DeleteSelectedNodesVisitor dsnv;
171                    viewer->getSceneData()->accept(dsnv);
172                    dsnv.pruneSelectedNodes();
173                }
[5174]174                return false;
175            }
176            case(osgGA::GUIEventAdapter::PUSH):
177            case(osgGA::GUIEventAdapter::MOVE):
178            {
179                _mx = ea.getX();
180                _my = ea.getY();
181                return false;
182            }
183            case(osgGA::GUIEventAdapter::RELEASE):
184            {
185                if (_mx == ea.getX() && _my == ea.getY())
186                {
187                    // only do a pick if the mouse hasn't moved
[5679]188                    pick(ea,viewer);
[5174]189                }
190                return true;
[14195]191            }
[5174]192
193            default:
194                return false;
195        }
196    }
197
[6817]198    void pick(const osgGA::GUIEventAdapter& ea, osgViewer::Viewer* viewer)
[5174]199    {
[5679]200        osg::Node* scene = viewer->getSceneData();
[5174]201        if (!scene) return;
202
[5683]203        osg::notify(osg::NOTICE)<<std::endl;
[5677]204
[5683]205        osg::Node* node = 0;
206        osg::Group* parent = 0;
[5677]207
[6734]208        if (_usePolytopeIntersector)
[5683]209        {
[6734]210            osgUtil::PolytopeIntersector* picker;
211            if (_useWindowCoordinates)
212            {
213                // use window coordinates
214                // remap the mouse x,y into viewport coordinates.
215                osg::Viewport* viewport = viewer->getCamera()->getViewport();
216                double mx = viewport->x() + (int)((double )viewport->width()*(ea.getXnormalized()*0.5+0.5));
217                double my = viewport->y() + (int)((double )viewport->height()*(ea.getYnormalized()*0.5+0.5));
[5684]218
[6734]219                // half width, height.
220                double w = 5.0f;
221                double h = 5.0f;
222                picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h );
223            } else {
224                double mx = ea.getXnormalized();
225                double my = ea.getYnormalized();
226                double w = 0.05;
227                double h = 0.05;
228                picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::PROJECTION, mx-w, my-h, mx+w, my+h );
229            }
[5683]230            osgUtil::IntersectionVisitor iv(picker);
231
232            viewer->getCamera()->accept(iv);
233
234            if (picker->containsIntersections())
235            {
[7626]236                osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection();
[5683]237
[7626]238                osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl
239                    <<"  Distance to ref. plane "<<intersection.distance
240                    <<", max. dist "<<intersection.maxDistance
241                    <<", primitive index "<<intersection.primitiveIndex
242                    <<", numIntersectionPoints "
243                    <<intersection.numIntersectionPoints
244                    <<std::endl;
[5683]245
[7626]246                osg::NodePath& nodePath = intersection.nodePath;
247                node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
248                parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
[5683]249
[14195]250                if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size "<<nodePath.size()<<std::endl;
[7626]251                toggleScribe(parent, node);
[5683]252            }
253
254        }
255        else
[5676]256        {
[6734]257            osgUtil::LineSegmentIntersector* picker;
258            if (!_useWindowCoordinates)
259            {
260                // use non dimensional coordinates - in projection/clip space
261                picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::PROJECTION, ea.getXnormalized(),ea.getYnormalized() );
262            } else {
263                // use window coordinates
264                // remap the mouse x,y into viewport coordinates.
265                osg::Viewport* viewport = viewer->getCamera()->getViewport();
266                float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f));
267                float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f));
268                picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, mx, my );
269            }
[5683]270            osgUtil::IntersectionVisitor iv(picker);
[5676]271
[5683]272            viewer->getCamera()->accept(iv);
273
274            if (picker->containsIntersections())
[5676]275            {
[5683]276                osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection();
277                osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl;
[5676]278
[5683]279                osg::NodePath& nodePath = intersection.nodePath;
280                node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
281                parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
[5676]282
[5683]283                if (node) std::cout<<"  Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
[6734]284                toggleScribe(parent, node);
[5683]285            }
[14195]286        }
[5683]287
288        // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
[6734]289    }
[5683]290
[6734]291    void toggleScribe(osg::Group* parent, osg::Node* node) {
292        if (!parent || !node) return;
[5683]293
[6734]294        std::cout<<"  parent "<<parent->className()<<std::endl;
295
296        osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
297        if (!parentAsScribe)
298        {
299            // node not already picked, so highlight it with an osgFX::Scribe
300            osgFX::Scribe* scribe = new osgFX::Scribe();
301            scribe->addChild(node);
302            parent->replaceChild(node,scribe);
303        }
304        else
305        {
306            // node already picked so we want to remove scribe to unpick it.
307            osg::Node::ParentList parentList = parentAsScribe->getParents();
308            for(osg::Node::ParentList::iterator itr=parentList.begin();
309                itr!=parentList.end();
310                ++itr)
[5683]311            {
[6734]312                (*itr)->replaceChild(parentAsScribe,node);
[5683]313            }
[5676]314        }
[6734]315
[5174]316    }
317
[5679]318    void saveSelectedModel(osg::Node* scene)
[5174]319    {
[5679]320        if (!scene) return;
[14195]321
[5174]322        CreateModelToSaveVisitor cmtsv;
[5679]323        scene->accept(cmtsv);
[14195]324
[5174]325        if (cmtsv._group->getNumChildren()>0)
326        {
[12529]327            std::cout<<"Writing selected compoents to 'selected_model.osgt'"<<std::endl;
328            osgDB::writeNodeFile(*cmtsv._group, "selected_model.osgt");
[5174]329        }
330    }
331
332protected:
333
334    float _mx,_my;
[6734]335    bool _usePolytopeIntersector;
336    bool _useWindowCoordinates;
[5174]337};
338
[2540]339int main( int argc, char **argv )
340{
[6913]341    osg::ref_ptr<osg::Node> loadedModel;
[14195]342
[2540]343    // load the scene.
[6913]344    if (argc>1) loadedModel = osgDB::readNodeFile(argv[1]);
[14195]345
[6913]346    // if not loaded assume no arguments passed in, try use default mode instead.
[12529]347    if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osgt");
[14195]348
349    if (!loadedModel)
[2540]350    {
351        std::cout << argv[0] <<": No data loaded." << std::endl;
352        return 1;
353    }
[14195]354
[2540]355    // create the window to draw to.
[5962]356    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
357    traits->x = 200;
358    traits->y = 200;
359    traits->width = 800;
360    traits->height = 600;
361    traits->windowDecoration = true;
362    traits->doubleBuffer = true;
363    traits->sharedContext = 0;
[2540]364
[5962]365    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
366    osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
367    if (!gw)
368    {
369        osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl;
370        return 1;
371    }
372
[2540]373    // create the view of the scene.
[6817]374    osgViewer::Viewer viewer;
375    viewer.getCamera()->setGraphicsContext(gc.get());
376    viewer.getCamera()->setViewport(0,0,800,600);
[5592]377    viewer.setSceneData(loadedModel.get());
[14195]378
[5173]379    // create a tracball manipulator to move the camera around in response to keyboard/mouse events
[5592]380    viewer.setCameraManipulator( new osgGA::TrackballManipulator );
[2540]381
[6817]382    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(viewer.getCamera()->getStateSet());
[5592]383    viewer.addEventHandler(statesetManipulator.get());
[5173]384
[5592]385    // add the pick handler
[5679]386    viewer.addEventHandler(new PickHandler());
[2540]387
[6817]388    viewer.realize();
[5173]389
[2540]390    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
[6817]391    while(!viewer.done())
[2540]392    {
[6817]393        viewer.frame();
[2540]394    }
395
396    return 0;
397}
Note: See TracBrowser for help on using the browser.