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

Revision 10523, 12.4 kB (checked in by robert, 5 years ago)

Introduced TabBoxTrackballDragger?

  • 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/TabBoxDragger>
26#include <osgManipulator/TabBoxTrackballDragger>
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 ("TabBoxTrackballDragger" == name)
57    {
58        osgManipulator::TabBoxTrackballDragger* d = new osgManipulator::TabBoxTrackballDragger();
59        d->setupDefaultGeometry();
60        dragger = d;
61    }
62    else if ("TrackballDragger" == name)
63    {
64        osgManipulator::TrackballDragger* d = new osgManipulator::TrackballDragger();
65        d->setupDefaultGeometry();
66        dragger = d;
67    }
68    else if ("Translate1DDragger" == name)
69    {
70        osgManipulator::Translate1DDragger* d = new osgManipulator::Translate1DDragger();
71        d->setupDefaultGeometry();
72        dragger = d;
73    }
74    else if ("Translate2DDragger" == name)
75    {
76        osgManipulator::Translate2DDragger* d = new osgManipulator::Translate2DDragger();
77        d->setupDefaultGeometry();
78        dragger = d;
79    }
80    else if ("TranslateAxisDragger" == name)
81    {
82        osgManipulator::TranslateAxisDragger* d = new osgManipulator::TranslateAxisDragger();
83        d->setupDefaultGeometry();
84        dragger = d;
85    }
86    else
87    {
88        osgManipulator::TabBoxDragger* d = new osgManipulator::TabBoxDragger();
89        d->setupDefaultGeometry();
90        dragger = d;
91    }
92
93   
94 
95    return dragger;
96}
97
98
99osg::Node* addDraggerToScene(osg::Node* scene, const std::string& name)
100{
101    scene->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
102
103    osg::MatrixTransform* selection = new osg::MatrixTransform;
104    selection->addChild(scene);
105
106    osgManipulator::Dragger* dragger = createDragger(name);
107
108
109    osg::Group* root = new osg::Group;
110    root->addChild(dragger);
111    root->addChild(selection);
112
113    float scale = scene->getBound().radius() * 1.6;
114    dragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
115                       osg::Matrix::translate(scene->getBound().center()));
116
117    dragger->addTransformUpdating(selection);
118
119    // we want the dragger to handle it's own events automatically
120    dragger->setHandleEvents(true);
121
122    // if we don't set an activation key or mod mask then any mouse click on
123    // the dragger will activate it, however if do define either of ActivationModKeyMask or
124    // and ActivationKeyEvent then you'll have to press either than mod key or the specified key to
125    // be able to activate the dragger when you mouse click on it.  Please note the follow allows
126    // activation if either the ctrl key or the 'a' key is pressed and held down.
127    dragger->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_CTRL);
128    dragger->setActivationKeyEvent('a');
129
130    return root;
131}
132
133osg::Node* createDemoScene() {
134 
135    osg::Group* root = new osg::Group;
136
137    osg::ref_ptr<osg::Geode> geode_1 = new osg::Geode;
138    osg::ref_ptr<osg::MatrixTransform> transform_1 = new osg::MatrixTransform;
139
140    osg::ref_ptr<osg::Geode> geode_2 = new osg::Geode;
141    osg::ref_ptr<osg::MatrixTransform> transform_2 = new osg::MatrixTransform;
142
143    osg::ref_ptr<osg::Geode> geode_3 = new osg::Geode;
144    osg::ref_ptr<osg::MatrixTransform> transform_3 = new osg::MatrixTransform;
145
146    osg::ref_ptr<osg::Geode> geode_4 = new osg::Geode;
147    osg::ref_ptr<osg::MatrixTransform> transform_4 = new osg::MatrixTransform;
148
149    osg::ref_ptr<osg::Geode> geode_5 = new osg::Geode;
150    osg::ref_ptr<osg::MatrixTransform> transform_5 = new osg::MatrixTransform;
151
152    osg::ref_ptr<osg::Geode> geode_6 = new osg::Geode;
153    osg::ref_ptr<osg::MatrixTransform> transform_6 = new osg::MatrixTransform;
154
155    osg::ref_ptr<osg::Geode> geode_7 = new osg::Geode;
156    osg::ref_ptr<osg::MatrixTransform> transform_7 = new osg::MatrixTransform;
157
158 
159
160
161
162    const float radius = 0.8f;
163    const float height = 1.0f;
164    osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
165    hints->setDetailRatio(2.0f);
166    osg::ref_ptr<osg::ShapeDrawable> shape;
167
168    shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, -2.0f), 10, 10.0f, 0.1f), hints.get());
169    shape->setColor(osg::Vec4(0.5f, 0.5f, 0.7f, 1.0f));
170    geode_1->addDrawable(shape.get());
171
172    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), radius * 2,radius), hints.get());
173    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f));
174    geode_2->addDrawable(shape.get());
175
176    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(-3.0f, 0.0f, 0.0f), radius,radius), hints.get());
177    shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f));
178    geode_3->addDrawable(shape.get());
179
180    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius,radius), hints.get());
181    shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f));
182    geode_4->addDrawable(shape.get());
183
184    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, -3.0f, 0.0f), radius, height), hints.get());
185    shape->setColor(osg::Vec4(0.2f, 0.5f, 0.7f, 1.0f));
186    geode_5->addDrawable(shape.get());
187
188    shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 3.0f, 0.0f), radius, height), hints.get());
189    shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f));
190    geode_6->addDrawable(shape.get());
191
192    shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, 0.0f, 3.0f), 2.0f, 2.0f), hints.get());
193    shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
194    geode_7->addDrawable(shape.get());
195
196
197
198
199
200
201    // material
202    osg::ref_ptr<osg::Material> matirial = new osg::Material;
203    matirial->setColorMode(osg::Material::DIFFUSE);
204    matirial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
205    matirial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
206    matirial->setShininess(osg::Material::FRONT_AND_BACK, 64.0f);
207    root->getOrCreateStateSet()->setAttributeAndModes(matirial.get(), osg::StateAttribute::ON);
208
209      transform_1.get()->addChild(addDraggerToScene(geode_1.get(),"TabBoxDragger"));
210    transform_2.get()->addChild(addDraggerToScene(geode_2.get(),"TabPlaneDragger"));
211    transform_3.get()->addChild(addDraggerToScene(geode_3.get(),"TabBoxTrackballDragger"));
212    transform_4.get()->addChild(addDraggerToScene(geode_4.get(),"TrackballDragger"));
213    transform_5.get()->addChild(addDraggerToScene(geode_5.get(),"Translate1DDragger"));
214    transform_6.get()->addChild(addDraggerToScene(geode_6.get(),"Translate2DDragger"));
215    transform_7.get()->addChild(addDraggerToScene(geode_7.get(),"TranslateAxisDragger"));
216
217    root->addChild(transform_1.get());
218    root->addChild(transform_2.get());
219    root->addChild(transform_3.get());
220    root->addChild(transform_4.get());
221    root->addChild(transform_5.get());
222    root->addChild(transform_6.get());
223    root->addChild(transform_7.get());
224
225 
226 
227    return root;
228}
229//
230int main( int argc, char **argv )
231{
232
233    // use an ArgumentParser object to manage the program arguments.
234    osg::ArgumentParser arguments(&argc,argv);
235   
236    // set up the usage document, in case we need to print out how to use this program.
237    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
238    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
239    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
240    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
241    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters");
242    arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available");
243    arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available");
244    arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings.");
245
246    arguments.getApplicationUsage()->addCommandLineOption("--dragger <draggername>","Use the specified dragger for manipulation [TabPlaneDragger,TabPlaneTrackballDragger,TrackballDragger,Translate1DDragger,Translate2DDragger,TranslateAxisDragger,TabBoxDragger]");
247   
248
249    // construct the viewer.
250    osgViewer::Viewer viewer;
251
252    // get details on keyboard and mouse bindings used by the viewer.
253    viewer.getUsage(*arguments.getApplicationUsage());
254
255    // if user request help write it out to cout.
256    bool helpAll = arguments.read("--help-all");
257    unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) |
258                            ((helpAll ||  arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) |
259                            ((helpAll ||  arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 );
260    if (helpType)
261    {
262        arguments.getApplicationUsage()->write(std::cout, helpType);
263        return 1;
264    }
265
266    // report any errors if they have occurred when parsing the program arguments.
267    if (arguments.errors())
268    {
269        arguments.writeErrorMessages(std::cout);
270        return 1;
271    }
272
273    std::string dragger_name = "TabBoxDragger";
274    arguments.read("--dragger", dragger_name);
275
276    osg::Timer_t start_tick = osg::Timer::instance()->tick();
277
278    // read the scene from the list of file specified command line args.
279    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
280
281    // if no model has been successfully loaded report failure.
282    bool tragger2Scene(true);
283    if (!loadedModel)
284    {
285        //std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
286        //return 1;
287        loadedModel = createDemoScene();
288        tragger2Scene=false;
289    }
290
291    // any option left unread are converted into errors to write out later.
292    arguments.reportRemainingOptionsAsUnrecognized();
293
294    // report any errors if they have occurred when parsing the program arguments.
295    if (arguments.errors())
296    {
297        arguments.writeErrorMessages(std::cout);
298    }
299
300    osg::Timer_t end_tick = osg::Timer::instance()->tick();
301
302    std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
303
304
305    // optimize the scene graph, remove redundant nodes and state etc.
306    osgUtil::Optimizer optimizer;
307    optimizer.optimize(loadedModel.get());
308
309   
310    // pass the loaded scene graph to the viewer.
311    if ( tragger2Scene ) {
312        viewer.setSceneData(addDraggerToScene(loadedModel.get(), dragger_name));
313    } else {
314        viewer.setSceneData(loadedModel.get());
315    }
316
317
318    return viewer.run();
319}
Note: See TracBrowser for help on using the browser.