root/OpenSceneGraph/trunk/examples/osgdatabaserevisions/osgdatabaserevisions.cpp @ 10310

Revision 10310, 6.2 kB (checked in by robert, 5 years ago)

Added osgdatabaserevisions example to server as a testbed for new osgDB::DatabaseRevisions? functionality

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#include <osgGA/SphericalManipulator>
30
31#include <iostream>
32
33int main(int argc, char** argv)
34{
35    // use an ArgumentParser object to manage the program arguments.
36    osg::ArgumentParser arguments(&argc,argv);
37
38    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
39    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
40    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
41    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
42    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
43    arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","Provide authentication information for http file access.");
44
45    osgViewer::Viewer viewer(arguments);
46
47    unsigned int helpType = 0;
48    if ((helpType = arguments.readHelpType()))
49    {
50        arguments.getApplicationUsage()->write(std::cout, helpType);
51        return 1;
52    }
53   
54    // report any errors if they have occurred when parsing the program arguments.
55    if (arguments.errors())
56    {
57        arguments.writeErrorMessages(std::cout);
58        return 1;
59    }
60
61#if 0
62    if (arguments.argc()<=1)
63    {
64        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
65        return 1;
66    }
67#endif
68
69    std::string url, username, password;
70    while(arguments.read("--login",url, username, password))
71    {
72        if (!osgDB::Registry::instance()->getAuthenticationMap())
73        {
74            osgDB::Registry::instance()->setAuthenticationMap(new osgDB::AuthenticationMap);
75            osgDB::Registry::instance()->getAuthenticationMap()->addAuthenticationDetails(
76                url,
77                new osgDB::AuthenticationDetails(username, password)
78            );
79        }
80    }
81
82    // set up the camera manipulators.
83    {
84        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
85
86        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
87        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
88        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
89        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
90        keyswitchManipulator->addMatrixManipulator( '5', "Spherical", new osgGA::SphericalManipulator() );
91
92        std::string pathfile;
93        char keyForAnimationPath = '6';
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    // add the LOD Scale handler
128    viewer.addEventHandler(new osgViewer::LODScaleHandler);
129
130    // add the screen capture handler
131    viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
132
133    std::string file = "http://www.openscenegraph.org/data/earth_bayarea/earth.ive";
134
135    osgDB::FileCache* fileCache = osgDB::Registry::instance()->getFileCache();
136    if (fileCache)
137    {
138        osg::notify(osg::NOTICE)<<"We have FileCache "<<fileCache<<std::endl;
139        fileCache->loadDatabaseRevisionsForFile(file);
140    }
141
142    // load the data
143    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(file);
144    if (!loadedModel)
145    {
146        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
147        return 1;
148    }
149
150    // any option left unread are converted into errors to write out later.
151    arguments.reportRemainingOptionsAsUnrecognized();
152
153    // report any errors if they have occurred when parsing the program arguments.
154    if (arguments.errors())
155    {
156        arguments.writeErrorMessages(std::cout);
157        return 1;
158    }
159
160    // optimize the scene graph, remove redundant nodes and state etc.
161    osgUtil::Optimizer optimizer;
162    optimizer.optimize(loadedModel.get());
163
164    viewer.setSceneData( loadedModel.get() );
165
166    viewer.realize();
167
168    return viewer.run();
169
170}
Note: See TracBrowser for help on using the browser.