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

Revision 12929, 15.8 kB (checked in by robert, 3 years ago)

Added support for Scale1DDragger, Scale2DDragger and TranslatePlaneDragger? to --dragger command line option

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