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

Revision 6927, 7.1 kB (checked in by robert, 8 years ago)

Improved the set up of static build, added freetype plugin to plugins that support static usage

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