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

Revision 10455, 5.9 kB (checked in by robert, 5 years ago)

Reverted erronous check-in (which was r10454)

  • 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#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 (arguments.argc()<=1)
62    {
63        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
64        return 1;
65    }
66
67    std::string url, username, password;
68    while(arguments.read("--login",url, username, password))
69    {
70        if (!osgDB::Registry::instance()->getAuthenticationMap())
71        {
72            osgDB::Registry::instance()->setAuthenticationMap(new osgDB::AuthenticationMap);
73            osgDB::Registry::instance()->getAuthenticationMap()->addAuthenticationDetails(
74                url,
75                new osgDB::AuthenticationDetails(username, password)
76            );
77        }
78    }
79
80    // set up the camera manipulators.
81    {
82        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
83
84        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
85        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
86        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
87        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
88        keyswitchManipulator->addMatrixManipulator( '5', "Spherical", new osgGA::SphericalManipulator() );
89
90        std::string pathfile;
91        char keyForAnimationPath = '6';
92        while (arguments.read("-p",pathfile))
93        {
94            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
95            if (apm || !apm->valid())
96            {
97                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
98                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
99                keyswitchManipulator->selectMatrixManipulator(num);
100                ++keyForAnimationPath;
101            }
102        }
103
104        viewer.setCameraManipulator( keyswitchManipulator.get() );
105    }
106
107    // add the state manipulator
108    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
109   
110    // add the thread model handler
111    viewer.addEventHandler(new osgViewer::ThreadingHandler);
112
113    // add the window size toggle handler
114    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
115       
116    // add the stats handler
117    viewer.addEventHandler(new osgViewer::StatsHandler);
118
119    // add the help handler
120    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
121
122    // add the record camera path handler
123    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
124
125    // add the LOD Scale handler
126    viewer.addEventHandler(new osgViewer::LODScaleHandler);
127
128    // add the screen capture handler
129    viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
130
131    // load the data
132    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
133    if (!loadedModel)
134    {
135        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
136        return 1;
137    }
138
139    // any option left unread are converted into errors to write out later.
140    arguments.reportRemainingOptionsAsUnrecognized();
141
142    // report any errors if they have occurred when parsing the program arguments.
143    if (arguments.errors())
144    {
145        arguments.writeErrorMessages(std::cout);
146        return 1;
147    }
148
149
150    // optimize the scene graph, remove redundant nodes and state etc.
151    osgUtil::Optimizer optimizer;
152    optimizer.optimize(loadedModel.get());
153
154    viewer.setSceneData( loadedModel.get() );
155
156    viewer.realize();
157
158    return viewer.run();
159
160}
Note: See TracBrowser for help on using the browser.