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

Revision 10439, 11.5 kB (checked in by robert, 5 years ago)

Removed old references to CommandManager?

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