root/OpenSceneGraph/trunk/examples/osgmanipulator/osgmanipulator.cpp @ 10429

Revision 10429, 16.2 kB (checked in by robert, 5 years ago)

Introduced event handling directly into osgManipulator::Dragger to allow it be used with a global event handler passing in events.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgmanipulator.
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
19#include <osgDB/ReadFile>
20#include <osgUtil/Optimizer>
21#include <osgViewer/Viewer>
22#include <osg/CoordinateSystemNode>
23#include <osgText/Text>
24
25#include <osgManipulator/CommandManager>
26#include <osgManipulator/TabBoxDragger>
27#include <osgManipulator/TabPlaneDragger>
28#include <osgManipulator/TabPlaneTrackballDragger>
29#include <osgManipulator/TrackballDragger>
30#include <osgManipulator/Translate1DDragger>
31#include <osgManipulator/Translate2DDragger>
32#include <osgManipulator/TranslateAxisDragger>
33
34#include <osg/ShapeDrawable>
35#include <osg/MatrixTransform>
36#include <osg/Geometry>
37#include <osg/Material>
38
39#include <iostream>
40
41osgManipulator::Dragger* createDragger(const std::string& name)
42{
43    osgManipulator::Dragger* dragger = 0;
44    if ("TabPlaneDragger" == name)
45    {
46        osgManipulator::TabPlaneDragger* d = new osgManipulator::TabPlaneDragger();
47        d->setupDefaultGeometry();
48        dragger = d;
49    }
50    else if ("TabPlaneTrackballDragger" == name)
51    {
52        osgManipulator::TabPlaneTrackballDragger* d = new osgManipulator::TabPlaneTrackballDragger();
53        d->setupDefaultGeometry();
54        dragger = d;
55    }
56    else if ("TrackballDragger" == name)
57    {
58        osgManipulator::TrackballDragger* d = new osgManipulator::TrackballDragger();
59        d->setupDefaultGeometry();
60        dragger = d;
61    }
62    else if ("Translate1DDragger" == name)
63    {
64        osgManipulator::Translate1DDragger* d = new osgManipulator::Translate1DDragger();
65        d->setupDefaultGeometry();
66        dragger = d;
67    }
68    else if ("Translate2DDragger" == name)
69    {
70        osgManipulator::Translate2DDragger* d = new osgManipulator::Translate2DDragger();
71        d->setupDefaultGeometry();
72        dragger = d;
73    }
74    else if ("TranslateAxisDragger" == name)
75    {
76        osgManipulator::TranslateAxisDragger* d = new osgManipulator::TranslateAxisDragger();
77        d->setupDefaultGeometry();
78        dragger = d;
79    }
80    else
81    {
82        osgManipulator::TabBoxDragger* d = new osgManipulator::TabBoxDragger();
83        d->setupDefaultGeometry();
84        dragger = d;
85    }
86
87   
88 
89    return dragger;
90}
91
92
93osg::Node* createHUD()
94{
95    osg::Geode* geode = new osg::Geode();
96   
97    std::string timesFont("fonts/arial.ttf");
98
99    osg::StateSet* stateset = geode->getOrCreateStateSet();
100    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
101
102    osgText::Text* text = new  osgText::Text;
103    geode->addDrawable( text );
104
105    osg::Vec3 position(50.0f,50.0f,0.0f);
106    text->setPosition(position);
107    text->setText("Use the Tab key to switch between the trackball and pick modes.");
108    text->setFont(timesFont);
109
110    osg::Camera* camera = new osg::Camera;
111
112    // set the projection matrix
113    camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
114
115    // set the view matrix   
116    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
117    camera->setViewMatrix(osg::Matrix::identity());
118
119    // only clear the depth buffer
120    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
121
122    // draw subgraph after main camera view.
123    camera->setRenderOrder(osg::Camera::POST_RENDER);
124
125    camera->addChild(geode);
126   
127    return camera;
128}
129
130osg::Node* addDraggerToScene(osg::Node* scene, osgManipulator::CommandManager* cmdMgr, const std::string& name)
131{
132    scene->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
133
134    osgManipulator::Selection* selection = new osgManipulator::Selection;
135    selection->addChild(scene);
136
137    osgManipulator::Dragger* dragger = createDragger(name);
138
139    dragger->setHandleEvents(true);
140
141    osg::Group* root = new osg::Group;
142    root->addChild(dragger);
143    root->addChild(selection);
144    root->addChild(createHUD());
145
146    float scale = scene->getBound().radius() * 1.6;
147    dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
148                       osg::Matrix::translate(scene->getBound().center()));
149    cmdMgr->connect(*dragger, *selection);
150
151    return root;
152}
153
154osg::Node* createDemoScene(osgManipulator::CommandManager* cmdMgr) {
155 
156    osg::Group* root = new osg::Group;
157
158    osg::ref_ptr<osg::Geode> geode_1 = new osg::Geode;
159    osg::ref_ptr<osg::MatrixTransform> transform_1 = new osg::MatrixTransform;
160
161    osg::ref_ptr<osg::Geode> geode_2 = new osg::Geode;
162    osg::ref_ptr<osg::MatrixTransform> transform_2 = new osg::MatrixTransform;
163
164    osg::ref_ptr<osg::Geode> geode_3 = new osg::Geode;
165    osg::ref_ptr<osg::MatrixTransform> transform_3 = new osg::MatrixTransform;
166
167    osg::ref_ptr<osg::Geode> geode_4 = new osg::Geode;
168    osg::ref_ptr<osg::MatrixTransform> transform_4 = new osg::MatrixTransform;
169
170    osg::ref_ptr<osg::Geode> geode_5 = new osg::Geode;
171    osg::ref_ptr<osg::MatrixTransform> transform_5 = new osg::MatrixTransform;
172
173    osg::ref_ptr<osg::Geode> geode_6 = new osg::Geode;
174    osg::ref_ptr<osg::MatrixTransform> transform_6 = new osg::MatrixTransform;
175
176    osg::ref_ptr<osg::Geode> geode_7 = new osg::Geode;
177    osg::ref_ptr<osg::MatrixTransform> transform_7 = new osg::MatrixTransform;
178
179 
180
181
182
183    const float radius = 0.8f;
184    const float height = 1.0f;
185    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
186    hints->setDetailRatio(2.0f);
187    osg::ref_ptr<osg::ShapeDrawable> shape;
188
189    shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, -2.0f), 10, 10.0f, 0.1f), hints.get());
190    shape->setColor(osg::Vec4(0.5f, 0.5f, 0.7f, 1.0f));
191    geode_1->addDrawable(shape.get());
192
193    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), radius * 2,radius), hints.get());
194    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
195    geode_2->addDrawable(shape.get());
196
197    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(-3.0f, 0.0f, 0.0f), radius,radius), hints.get());
198    shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f));
199    geode_3->addDrawable(shape.get());
200
201    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius,radius), hints.get());
202    shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f));
203    geode_4->addDrawable(shape.get());
204
205    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, -3.0f, 0.0f), radius, height), hints.get());
206    shape->setColor(osg::Vec4(0.2f, 0.5f, 0.7f, 1.0f));
207    geode_5->addDrawable(shape.get());
208
209    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 3.0f, 0.0f), radius, height), hints.get());
210    shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
211    geode_6->addDrawable(shape.get());
212
213    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, 0.0f, 3.0f), 2.0f, 2.0f), hints.get());
214    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
215    geode_7->addDrawable(shape.get());
216
217
218
219
220
221
222    // material
223    osg::ref_ptr<osg::Material> matirial = new osg::Material;
224    matirial->setColorMode(osg::Material::DIFFUSE);
225    matirial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
226    matirial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
227    matirial->setShininess(osg::Material::FRONT_AND_BACK, 64.0f);
228    root->getOrCreateStateSet()->setAttributeAndModes(matirial.get(), osg::StateAttribute::ON);
229
230      transform_1.get()->addChild(addDraggerToScene(geode_1.get(),cmdMgr,"TabBoxDragger"));
231    transform_2.get()->addChild(addDraggerToScene(geode_2.get(),cmdMgr,"TabPlaneDragger"));
232    transform_3.get()->addChild(addDraggerToScene(geode_3.get(),cmdMgr,"TabPlaneTrackballDragger"));
233    transform_4.get()->addChild(addDraggerToScene(geode_4.get(),cmdMgr,"TrackballDragger"));
234    transform_5.get()->addChild(addDraggerToScene(geode_5.get(),cmdMgr,"Translate1DDragger"));
235    transform_6.get()->addChild(addDraggerToScene(geode_6.get(),cmdMgr,"Translate2DDragger"));
236    transform_7.get()->addChild(addDraggerToScene(geode_7.get(),cmdMgr,"TranslateAxisDragger"));
237
238    root->addChild(transform_1.get());
239    root->addChild(transform_2.get());
240    root->addChild(transform_3.get());
241    root->addChild(transform_4.get());
242    root->addChild(transform_5.get());
243    root->addChild(transform_6.get());
244    root->addChild(transform_7.get());
245
246 
247 
248    return root;
249}
250
251
252class PickModeHandler : public osgGA::GUIEventHandler
253{
254    public:
255        enum Modes
256        {
257            VIEW = 0,
258            PICK
259        };
260
261        PickModeHandler():
262            _mode(VIEW),
263            _activeDragger(0)
264        {
265        }
266
267        bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa,
268                    osg::Object*, osg::NodeVisitor*)
269        {
270            osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
271            if (!view) return false;
272
273            if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Tab &&
274                ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&
275                _activeDragger == 0)
276            {
277                _mode = ! _mode;
278            }
279           
280            if (VIEW == _mode) return false;
281
282            switch (ea.getEventType())
283            {
284                case osgGA::GUIEventAdapter::PUSH:
285                {
286                    osgUtil::LineSegmentIntersector::Intersections intersections;
287
288                    _pointer.reset();
289
290                    if (view->computeIntersections(ea.getX(),ea.getY(),intersections))
291                    {
292                        _pointer.setCamera(view->getCamera());
293                        _pointer.setMousePosition(ea.getX(), ea.getY());
294
295                        for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
296                            hitr != intersections.end();
297                            ++hitr)
298                        {
299                            _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
300                        }
301                        for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin();
302                             itr != _pointer._hitList.front().first.end();
303                             ++itr)
304                        {
305                            osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr);
306                            if (dragger)
307                            {
308
309                                dragger->handle(_pointer, ea, aa);
310                                _activeDragger = dragger;
311                                break;
312                            }                   
313                        }
314                    }
315                }
316                case osgGA::GUIEventAdapter::DRAG:
317                case osgGA::GUIEventAdapter::RELEASE:
318                {
319                    if (_activeDragger)
320                    {
321                        _pointer._hitIter = _pointer._hitList.begin();
322                        _pointer.setCamera(view->getCamera());
323                        _pointer.setMousePosition(ea.getX(), ea.getY());
324
325                        _activeDragger->handle(_pointer, ea, aa);
326                    }
327                    break;
328                }
329        default:
330            break;
331            }
332
333            if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
334            {
335                _activeDragger = 0;
336                _pointer.reset();
337            }
338
339            return true;
340        }
341       
342    private:
343        unsigned int _mode;
344        osgManipulator::Dragger* _activeDragger;
345        osgManipulator::PointerInfo _pointer;
346};
347
348int main( int argc, char **argv )
349{
350
351    // use an ArgumentParser object to manage the program arguments.
352    osg::ArgumentParser arguments(&argc,argv);
353   
354    // set up the usage document, in case we need to print out how to use this program.
355    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
356    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
357    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
358    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
359    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters");
360    arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available");
361    arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available");
362    arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings.");
363
364    arguments.getApplicationUsage()->addCommandLineOption("--dragger <draggername>","Use the specified dragger for manipulation [TabPlaneDragger,TabPlaneTrackballDragger,TrackballDragger,Translate1DDragger,Translate2DDragger,TranslateAxisDragger,TabBoxDragger]");
365   
366
367    // construct the viewer.
368    osgViewer::Viewer viewer;
369
370    // get details on keyboard and mouse bindings used by the viewer.
371    viewer.getUsage(*arguments.getApplicationUsage());
372
373    // if user request help write it out to cout.
374    bool helpAll = arguments.read("--help-all");
375    unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) |
376                            ((helpAll ||  arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) |
377                            ((helpAll ||  arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 );
378    if (helpType)
379    {
380        arguments.getApplicationUsage()->write(std::cout, helpType);
381        return 1;
382    }
383
384    // report any errors if they have occurred when parsing the program arguments.
385    if (arguments.errors())
386    {
387        arguments.writeErrorMessages(std::cout);
388        return 1;
389    }
390
391    std::string dragger_name = "TabBoxDragger";
392    arguments.read("--dragger", dragger_name);
393
394    osg::Timer_t start_tick = osg::Timer::instance()->tick();
395
396    // read the scene from the list of file specified command line args.
397    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
398
399    // create a command manager
400    osg::ref_ptr<osgManipulator::CommandManager> cmdMgr = new osgManipulator::CommandManager;
401
402    // if no model has been successfully loaded report failure.
403    bool tragger2Scene(true);
404    if (!loadedModel)
405    {
406        //std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
407        //return 1;
408        loadedModel = createDemoScene(cmdMgr.get());
409        tragger2Scene=false;
410    }
411
412    // any option left unread are converted into errors to write out later.
413    arguments.reportRemainingOptionsAsUnrecognized();
414
415    // report any errors if they have occurred when parsing the program arguments.
416    if (arguments.errors())
417    {
418        arguments.writeErrorMessages(std::cout);
419    }
420
421    osg::Timer_t end_tick = osg::Timer::instance()->tick();
422
423    std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
424
425
426    // optimize the scene graph, remove redundant nodes and state etc.
427    osgUtil::Optimizer optimizer;
428    optimizer.optimize(loadedModel.get());
429
430   
431    // pass the loaded scene graph to the viewer.
432    if ( tragger2Scene ) {
433        viewer.setSceneData(addDraggerToScene(loadedModel.get(), cmdMgr.get(), dragger_name));
434    } else {
435        viewer.setSceneData(loadedModel.get());
436    }
437    // viewer.addEventHandler(new PickModeHandler());
438
439    return viewer.run();
440}
Note: See TracBrowser for help on using the browser.