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

Revision 10454, 12.1 kB (checked in by robert, 5 years ago)

Added Dragger::s/getActivationModKeyMask(..) and Dragger::s/getActivationKeyEvent(...) methods to make it possible to have draggers that only respond when you press a specified modified key or standard key.

Changed the optional dragger in osgvolume to require the shift key to be pressed for the dragger to become active.

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