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

Revision 10454, 6.3 kB (checked in by robert, 5 years ago)

Added Dragger::s/getActivationModKeyMask(..) and Dragger::s/getActivationKeyEvent(...) methods to make it possible to have draggers that only respond when you press a specified modified key or standard key.

Changed the optional dragger in osgvolume to require the shift key to be pressed for the dragger to become active.

  • 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#include <osg/Group>
19
20#include <osgViewer/Viewer>
21#include <osgViewer/ViewerEventHandlers>
22
23#include <osgGA/TrackballManipulator>
24#include <osgGA/FlightManipulator>
25#include <osgGA/DriveManipulator>
26#include <osgGA/KeySwitchMatrixManipulator>
27#include <osgGA/StateSetManipulator>
28#include <osgGA/AnimationPathManipulator>
29#include <osgGA/TerrainManipulator>
30
31#include <iostream>
32
33#ifdef _WINDOWS
34#include <Windows.h>
35#include <GL/glext.h>
36#include <GL/wglext.h>
37#include "vsynctoggle_custom.h"
38#endif
39
40int main(int argc, char** argv)
41{
42  // use an ArgumentParser object to manage the program arguments.
43  osg::ArgumentParser arguments(&argc,argv);
44
45  arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
46  arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
47  arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
48  arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
49  arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
50  arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","Provide authentication information for http file access.");
51
52  osgViewer::Viewer viewer(arguments);
53
54  unsigned int helpType = 0;
55  if ((helpType = arguments.readHelpType()))
56  {
57    arguments.getApplicationUsage()->write(std::cout, helpType);
58    return 1;
59  }
60
61  // report any errors if they have occurred when parsing the program arguments.
62  if (arguments.errors())
63  {
64    arguments.writeErrorMessages(std::cout);
65    return 1;
66  }
67
68  if (arguments.argc()<=1)
69  {
70    arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
71    return 1;
72  }
73
74  std::string url, username, password;
75  while(arguments.read("--login",url, username, password))
76  {
77    if (!osgDB::Registry::instance()->getAuthenticationMap())
78    {
79      osgDB::Registry::instance()->setAuthenticationMap(new osgDB::AuthenticationMap);
80      osgDB::Registry::instance()->getAuthenticationMap()->addAuthenticationDetails(
81        url,
82        new osgDB::AuthenticationDetails(username, password)
83        );
84    }
85  }
86
87  // set up the camera manipulators.
88  {
89    osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
90
91    keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
92    keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
93    keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
94    keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
95
96    std::string pathfile;
97    char keyForAnimationPath = '5';
98    while (arguments.read("-p",pathfile))
99    {
100      osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
101      if (apm || !apm->valid())
102      {
103        unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
104        keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
105        keyswitchManipulator->selectMatrixManipulator(num);
106        ++keyForAnimationPath;
107      }
108    }
109
110    viewer.setCameraManipulator( keyswitchManipulator.get() );
111  }
112
113  // add the state manipulator
114  viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
115
116  // add the thread model handler
117  viewer.addEventHandler(new osgViewer::ThreadingHandler);
118
119  // add the window size toggle handler
120  viewer.addEventHandler(new osgViewer::WindowSizeHandler);
121
122  // add the stats handler
123  viewer.addEventHandler(new osgViewer::StatsHandler);
124
125  // add the help handler
126  viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
127
128  // add the record camera path handler
129  viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
130
131  // add the LOD Scale handler
132  viewer.addEventHandler(new osgViewer::LODScaleHandler);
133
134  // add the screen capture handler
135  viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
136
137  // load the data
138  osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
139  if (!loadedModel)
140  {
141    std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
142    return 1;
143  }
144
145  // any option left unread are converted into errors to write out later.
146  arguments.reportRemainingOptionsAsUnrecognized();
147
148  // report any errors if they have occurred when parsing the program arguments.
149  if (arguments.errors())
150  {
151    arguments.writeErrorMessages(std::cout);
152    return 1;
153  }
154
155
156  // optimize the scene graph, remove redundant nodes and state etc.
157  osgUtil::Optimizer optimizer;
158  optimizer.optimize(loadedModel.get());
159
160  osg::ref_ptr<osg::Group> group = new osg::Group();
161  group->addChild(loadedModel.get());
162
163  viewer.setSceneData( group.get() );
164
165  {
166    char tmp[256];
167    tmp[sizeof(tmp) - 1] = 0;
168    getcwd(tmp, sizeof(tmp) - 1);
169
170    std::string path(tmp);
171    path += "\\system\\cache\\planet";
172
173    tmp[sizeof(tmp) - 1] = 0;
174    snprintf(tmp, sizeof(tmp) - 1, "%s", path.c_str());
175    //_putenv(tmp);
176    // osgDB::Registry::instance()->setFileCache(new osgDB::FileCache( tmp ) );
177    viewer.getDatabasePager()->setTargetMaximumNumberOfPageLOD(500);
178
179#ifdef _WINDOWS
180    osg::ref_ptr<baronvsync::CVsyncDrawable> vsync = new baronvsync::CVsyncDrawable();
181    osg::ref_ptr<osg::Geode> vsyncGeode = new osg::Geode();
182    vsyncGeode->addDrawable(vsync.get());
183    vsync->SetVSync(false);
184    group->addChild(vsyncGeode.get());
185#endif
186
187  }
188
189  viewer.realize();
190
191  return viewer.run();
192
193}
Note: See TracBrowser for help on using the browser.