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

Revision 10064, 16.1 kB (checked in by robert, 5 years ago)

From Paul Martz, "Looks like the people who created these two examples were a bit careless with cut and paste."

  • 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    osg::Group* root = new osg::Group;
140    root->addChild(dragger);
141    root->addChild(selection);
142    root->addChild(createHUD());
143
144    float scale = scene->getBound().radius() * 1.6;
145    dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
146                       osg::Matrix::translate(scene->getBound().center()));
147    cmdMgr->connect(*dragger, *selection);
148
149    return root;
150}
151
152osg::Node* createDemoScene(osgManipulator::CommandManager* cmdMgr) {
153 
154    osg::Group* root = new osg::Group;
155
156    osg::ref_ptr<osg::Geode> geode_1 = new osg::Geode;
157    osg::ref_ptr<osg::MatrixTransform> transform_1 = new osg::MatrixTransform;
158
159    osg::ref_ptr<osg::Geode> geode_2 = new osg::Geode;
160    osg::ref_ptr<osg::MatrixTransform> transform_2 = new osg::MatrixTransform;
161
162    osg::ref_ptr<osg::Geode> geode_3 = new osg::Geode;
163    osg::ref_ptr<osg::MatrixTransform> transform_3 = new osg::MatrixTransform;
164
165    osg::ref_ptr<osg::Geode> geode_4 = new osg::Geode;
166    osg::ref_ptr<osg::MatrixTransform> transform_4 = new osg::MatrixTransform;
167
168    osg::ref_ptr<osg::Geode> geode_5 = new osg::Geode;
169    osg::ref_ptr<osg::MatrixTransform> transform_5 = new osg::MatrixTransform;
170
171    osg::ref_ptr<osg::Geode> geode_6 = new osg::Geode;
172    osg::ref_ptr<osg::MatrixTransform> transform_6 = new osg::MatrixTransform;
173
174    osg::ref_ptr<osg::Geode> geode_7 = new osg::Geode;
175    osg::ref_ptr<osg::MatrixTransform> transform_7 = new osg::MatrixTransform;
176
177 
178
179
180
181    const float radius = 0.8f;
182    const float height = 1.0f;
183    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
184    hints->setDetailRatio(2.0f);
185    osg::ref_ptr<osg::ShapeDrawable> shape;
186
187    shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, -2.0f), 10, 10.0f, 0.1f), hints.get());
188    shape->setColor(osg::Vec4(0.5f, 0.5f, 0.7f, 1.0f));
189    geode_1->addDrawable(shape.get());
190
191    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), radius * 2,radius), hints.get());
192    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
193    geode_2->addDrawable(shape.get());
194
195    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(-3.0f, 0.0f, 0.0f), radius,radius), hints.get());
196    shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f));
197    geode_3->addDrawable(shape.get());
198
199    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius,radius), hints.get());
200    shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f));
201    geode_4->addDrawable(shape.get());
202
203    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, -3.0f, 0.0f), radius, height), hints.get());
204    shape->setColor(osg::Vec4(0.2f, 0.5f, 0.7f, 1.0f));
205    geode_5->addDrawable(shape.get());
206
207    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 3.0f, 0.0f), radius, height), hints.get());
208    shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
209    geode_6->addDrawable(shape.get());
210
211    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, 0.0f, 3.0f), 2.0f, 2.0f), hints.get());
212    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
213    geode_7->addDrawable(shape.get());
214
215
216
217
218
219
220    // material
221    osg::ref_ptr<osg::Material> matirial = new osg::Material;
222    matirial->setColorMode(osg::Material::DIFFUSE);
223    matirial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
224    matirial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
225    matirial->setShininess(osg::Material::FRONT_AND_BACK, 64.0f);
226    root->getOrCreateStateSet()->setAttributeAndModes(matirial.get(), osg::StateAttribute::ON);
227
228      transform_1.get()->addChild(addDraggerToScene(geode_1.get(),cmdMgr,"TabBoxDragger"));
229    transform_2.get()->addChild(addDraggerToScene(geode_2.get(),cmdMgr,"TabPlaneDragger"));
230    transform_3.get()->addChild(addDraggerToScene(geode_3.get(),cmdMgr,"TabPlaneTrackballDragger"));
231    transform_4.get()->addChild(addDraggerToScene(geode_4.get(),cmdMgr,"TrackballDragger"));
232    transform_5.get()->addChild(addDraggerToScene(geode_5.get(),cmdMgr,"Translate1DDragger"));
233    transform_6.get()->addChild(addDraggerToScene(geode_6.get(),cmdMgr,"Translate2DDragger"));
234    transform_7.get()->addChild(addDraggerToScene(geode_7.get(),cmdMgr,"TranslateAxisDragger"));
235
236    root->addChild(transform_1.get());
237    root->addChild(transform_2.get());
238    root->addChild(transform_3.get());
239    root->addChild(transform_4.get());
240    root->addChild(transform_5.get());
241    root->addChild(transform_6.get());
242    root->addChild(transform_7.get());
243
244 
245 
246    return root;
247}
248
249
250class PickModeHandler : public osgGA::GUIEventHandler
251{
252    public:
253        enum Modes
254        {
255            VIEW = 0,
256            PICK
257        };
258
259        PickModeHandler():
260            _mode(VIEW),
261            _activeDragger(0)
262        {
263        }       
264       
265        bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa,
266                    osg::Object*, osg::NodeVisitor*)
267        {
268            osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
269            if (!view) return false;
270
271            if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Tab &&
272                ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&
273                _activeDragger == 0)
274            {
275                _mode = ! _mode;
276            }
277           
278            if (VIEW == _mode) return false;
279
280            switch (ea.getEventType())
281            {
282                case osgGA::GUIEventAdapter::PUSH:
283                {
284                    osgUtil::LineSegmentIntersector::Intersections intersections;
285
286                    _pointer.reset();
287
288                    if (view->computeIntersections(ea.getX(),ea.getY(),intersections))
289                    {
290                        _pointer.setCamera(view->getCamera());
291                        _pointer.setMousePosition(ea.getX(), ea.getY());
292
293                        for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
294                            hitr != intersections.end();
295                            ++hitr)
296                        {
297                            _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
298                        }
299                        for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin();
300                             itr != _pointer._hitList.front().first.end();
301                             ++itr)
302                        {
303                            osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr);
304                            if (dragger)
305                            {
306
307                                dragger->handle(_pointer, ea, aa);
308                                _activeDragger = dragger;
309                                break;
310                            }                   
311                        }
312                    }
313                }
314                case osgGA::GUIEventAdapter::DRAG:
315                case osgGA::GUIEventAdapter::RELEASE:
316                {
317                    if (_activeDragger)
318                    {
319                        _pointer._hitIter = _pointer._hitList.begin();
320                        _pointer.setCamera(view->getCamera());
321                        _pointer.setMousePosition(ea.getX(), ea.getY());
322
323                        _activeDragger->handle(_pointer, ea, aa);
324                    }
325                    break;
326                }
327        default:
328            break;
329            }
330
331            if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
332            {
333                _activeDragger = 0;
334                _pointer.reset();
335            }
336
337            return true;
338        }
339       
340    private:
341        unsigned int _mode;
342        osgManipulator::Dragger* _activeDragger;
343        osgManipulator::PointerInfo _pointer;
344};
345
346int main( int argc, char **argv )
347{
348
349    // use an ArgumentParser object to manage the program arguments.
350    osg::ArgumentParser arguments(&argc,argv);
351   
352    // set up the usage document, in case we need to print out how to use this program.
353    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
354    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
355    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
356    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
357    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters");
358    arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available");
359    arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available");
360    arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings.");
361
362    arguments.getApplicationUsage()->addCommandLineOption("--dragger <draggername>","Use the specified dragger for manipulation [TabPlaneDragger,TabPlaneTrackballDragger,TrackballDragger,Translate1DDragger,Translate2DDragger,TranslateAxisDragger,TabBoxDragger]");
363   
364
365    // construct the viewer.
366    osgViewer::Viewer viewer;
367
368    // get details on keyboard and mouse bindings used by the viewer.
369    viewer.getUsage(*arguments.getApplicationUsage());
370
371    // if user request help write it out to cout.
372    bool helpAll = arguments.read("--help-all");
373    unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) |
374                            ((helpAll ||  arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) |
375                            ((helpAll ||  arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 );
376    if (helpType)
377    {
378        arguments.getApplicationUsage()->write(std::cout, helpType);
379        return 1;
380    }
381
382    // report any errors if they have occurred when parsing the program arguments.
383    if (arguments.errors())
384    {
385        arguments.writeErrorMessages(std::cout);
386        return 1;
387    }
388
389    std::string dragger_name = "TabBoxDragger";
390    arguments.read("--dragger", dragger_name);
391
392    osg::Timer_t start_tick = osg::Timer::instance()->tick();
393
394    // read the scene from the list of file specified command line args.
395    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
396
397    // create a command manager
398    osg::ref_ptr<osgManipulator::CommandManager> cmdMgr = new osgManipulator::CommandManager;
399
400    // if no model has been successfully loaded report failure.
401    bool tragger2Scene(true);
402    if (!loadedModel)
403    {
404        //std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
405        //return 1;
406        loadedModel = createDemoScene(cmdMgr.get());
407        tragger2Scene=false;
408    }
409
410    // any option left unread are converted into errors to write out later.
411    arguments.reportRemainingOptionsAsUnrecognized();
412
413    // report any errors if they have occurred when parsing the program arguments.
414    if (arguments.errors())
415    {
416        arguments.writeErrorMessages(std::cout);
417    }
418
419    osg::Timer_t end_tick = osg::Timer::instance()->tick();
420
421    std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
422
423
424    // optimize the scene graph, remove redundant nodes and state etc.
425    osgUtil::Optimizer optimizer;
426    optimizer.optimize(loadedModel.get());
427
428   
429    // pass the loaded scene graph to the viewer.
430    if ( tragger2Scene ) {
431        viewer.setSceneData(addDraggerToScene(loadedModel.get(), cmdMgr.get(), dragger_name));
432    } else {
433        viewer.setSceneData(loadedModel.get());
434    }
435    viewer.addEventHandler(new PickModeHandler());
436
437    return viewer.run();
438}
Note: See TracBrowser for help on using the browser.