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

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

Added osgstaticviewer example to demonstrate how to put a static lib built viewer

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