root/OpenSceneGraph/trunk/applications/osgviewer/osgviewer.cpp @ 7071

Revision 7071, 7.5 kB (checked in by robert, 7 years ago)

Added support for managing a CompileContext?. Rearranged the DeleteHandler::flushAll call.

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