root/OpenSceneGraph/trunk/examples/osgstaticviewer/osgstaticviewer.cpp @ 7076

Revision 7076, 7.6 kB (checked in by robert, 7 years ago)

From Rudolf Wiedemann, "I need to link OpenSceneGraph statically to my application, so I
completed the new registration of the plugin-readerwriters
("REGISTER_OSGPLUGIN") according to your osgstaticviewer-example (see
attachment, based on today's svn)."

Line 
1/* OpenSceneGraph example, osgstaticviewer.
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 <osg/CoordinateSystemNode>
22
23#include <osg/Switch>
24#include <osgText/Text>
25
26#include <osgViewer/Viewer>
27#include <osgViewer/ViewerEventHandlers>
28
29#include <osgGA/TrackballManipulator>
30#include <osgGA/FlightManipulator>
31#include <osgGA/DriveManipulator>
32#include <osgGA/KeySwitchMatrixManipulator>
33#include <osgGA/StateSetManipulator>
34#include <osgGA/AnimationPathManipulator>
35#include <osgGA/TerrainManipulator>
36
37#include <iostream>
38
39// include the plugins we need
40USE_OSGPLUGIN(ive);
41USE_OSGPLUGIN(freetype);
42USE_OSGPLUGIN(osg);
43USE_OSGPLUGIN(OpenFlight);
44
45// include the platform specific GraphicsWindow implementation.
46USE_GRAPHICSWINDOW();
47
48int main(int argc, char** argv)
49{
50    // use an ArgumentParser object to manage the program arguments.
51    osg::ArgumentParser arguments(&argc,argv);
52
53    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
54    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
55    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
56    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
57    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
58    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters");
59    arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available");
60    arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available");
61    arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings.");
62    arguments.getApplicationUsage()->addCommandLineOption("--SingleThreaded","Select SingleThreaded threading model for viewer.");
63    arguments.getApplicationUsage()->addCommandLineOption("--CullDrawThreadPerContext","Select CullDrawThreadPerContext threading model for viewer.");
64    arguments.getApplicationUsage()->addCommandLineOption("--DrawThreadPerContext","Select DrawThreadPerContext threading model for viewer.");
65    arguments.getApplicationUsage()->addCommandLineOption("--CullThreadPerCameraDrawThreadPerContext","Select CullThreadPerCameraDrawThreadPerContext threading model for viewer.");
66
67    // if user request help write it out to cout.
68    bool helpAll = arguments.read("--help-all");
69    unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) |
70                            ((helpAll ||  arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) |
71                            ((helpAll ||  arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 );
72    if (helpType)
73    {
74        arguments.getApplicationUsage()->write(std::cout, helpType);
75        return 1;
76    }
77
78    // report any errors if they have occurred when parsing the program arguments.
79    if (arguments.errors())
80    {
81        arguments.writeErrorMessages(std::cout);
82        return 1;
83    }
84   
85    if (arguments.argc()<=1)
86    {
87        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
88        return 1;
89    }
90
91    osgViewer::Viewer viewer;
92   
93    // set up the camera manipulators.
94    {
95        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
96
97        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
98        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
99        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
100        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
101
102        std::string pathfile;
103        char keyForAnimationPath = '5';
104        while (arguments.read("-p",pathfile))
105        {
106            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
107            if (apm || !apm->valid())
108            {
109                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
110                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
111                keyswitchManipulator->selectMatrixManipulator(num);
112                ++keyForAnimationPath;
113            }
114        }
115
116        viewer.setCameraManipulator( keyswitchManipulator.get() );
117    }
118
119    // add the state manipulator
120    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
121   
122    // add the thread model handler
123    viewer.addEventHandler(new osgViewer::ThreadingHandler);
124
125    // add the window size toggle handler
126    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
127       
128    // add the stats handler
129    viewer.addEventHandler(new osgViewer::StatsHandler);
130
131    // add the help handler
132    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
133
134    while (arguments.read("--SingleThreaded")) viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
135    while (arguments.read("--CullDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
136    while (arguments.read("--DrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
137    while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
138
139    unsigned int screenNum;
140    while (arguments.read("--screen",screenNum))
141    {
142        viewer.setUpViewOnSingleScreen(screenNum);
143    }
144
145    // load the data
146    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
147    if (!loadedModel)
148    {
149        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
150        return 1;
151    }
152
153    // any option left unread are converted into errors to write out later.
154    arguments.reportRemainingOptionsAsUnrecognized();
155
156    // report any errors if they have occurred when parsing the program arguments.
157    if (arguments.errors())
158    {
159        arguments.writeErrorMessages(std::cout);
160        return 1;
161    }
162
163
164    // optimize the scene graph, remove redundant nodes and state etc.
165    osgUtil::Optimizer optimizer;
166    optimizer.optimize(loadedModel.get());
167
168    viewer.setSceneData( loadedModel.get() );
169
170    return viewer.run();
171}
Note: See TracBrowser for help on using the browser.