| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgDB/ReadFile> |
|---|
| 20 | #include <osgUtil/Optimizer> |
|---|
| 21 | #include <osg/CoordinateSystemNode> |
|---|
| 22 | |
|---|
| 23 | #include <osg/Switch> |
|---|
| 24 | #include <osgText/Text> |
|---|
| 25 | |
|---|
| 26 | #include <osgViewer/Viewer> |
|---|
| 27 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 28 | |
|---|
| 29 | #include <osgGA/TrackballManipulator> |
|---|
| 30 | #include <osgGA/FlightManipulator> |
|---|
| 31 | #include <osgGA/DriveManipulator> |
|---|
| 32 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 33 | #include <osgGA/StateSetManipulator> |
|---|
| 34 | #include <osgGA/AnimationPathManipulator> |
|---|
| 35 | #include <osgGA/TerrainManipulator> |
|---|
| 36 | |
|---|
| 37 | #include <iostream> |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | USE_OSGPLUGIN(ive); |
|---|
| 41 | USE_OSGPLUGIN(freetype); |
|---|
| 42 | USE_OSGPLUGIN(osg); |
|---|
| 43 | USE_OSGPLUGIN(rgb); |
|---|
| 44 | USE_OSGPLUGIN(OpenFlight); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | USE_GRAPHICSWINDOW(); |
|---|
| 48 | |
|---|
| 49 | int main(int argc, char** argv) |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 53 | |
|---|
| 54 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 55 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); |
|---|
| 56 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 57 | arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad"); |
|---|
| 58 | arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField"); |
|---|
| 59 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); |
|---|
| 60 | arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available"); |
|---|
| 61 | arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available"); |
|---|
| 62 | arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindings."); |
|---|
| 63 | arguments.getApplicationUsage()->addCommandLineOption("--SingleThreaded","Select SingleThreaded threading model for viewer."); |
|---|
| 64 | arguments.getApplicationUsage()->addCommandLineOption("--CullDrawThreadPerContext","Select CullDrawThreadPerContext threading model for viewer."); |
|---|
| 65 | arguments.getApplicationUsage()->addCommandLineOption("--DrawThreadPerContext","Select DrawThreadPerContext threading model for viewer."); |
|---|
| 66 | arguments.getApplicationUsage()->addCommandLineOption("--CullThreadPerCameraDrawThreadPerContext","Select CullThreadPerCameraDrawThreadPerContext threading model for viewer."); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | bool helpAll = arguments.read("--help-all"); |
|---|
| 70 | unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) | |
|---|
| 71 | ((helpAll || arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) | |
|---|
| 72 | ((helpAll || arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 ); |
|---|
| 73 | if (helpType) |
|---|
| 74 | { |
|---|
| 75 | arguments.getApplicationUsage()->write(std::cout, helpType); |
|---|
| 76 | return 1; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | if (arguments.errors()) |
|---|
| 81 | { |
|---|
| 82 | arguments.writeErrorMessages(std::cout); |
|---|
| 83 | return 1; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if (arguments.argc()<=1) |
|---|
| 87 | { |
|---|
| 88 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 89 | return 1; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | osgViewer::Viewer viewer; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | { |
|---|
| 96 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 97 | |
|---|
| 98 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 99 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 100 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 101 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 102 | |
|---|
| 103 | std::string pathfile; |
|---|
| 104 | char keyForAnimationPath = '5'; |
|---|
| 105 | while (arguments.read("-p",pathfile)) |
|---|
| 106 | { |
|---|
| 107 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 108 | if (apm || !apm->valid()) |
|---|
| 109 | { |
|---|
| 110 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 111 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 112 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 113 | ++keyForAnimationPath; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
|---|
| 134 | |
|---|
| 135 | while (arguments.read("--SingleThreaded")) viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 136 | while (arguments.read("--CullDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); |
|---|
| 137 | while (arguments.read("--DrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); |
|---|
| 138 | while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); |
|---|
| 139 | |
|---|
| 140 | unsigned int screenNum; |
|---|
| 141 | while (arguments.read("--screen",screenNum)) |
|---|
| 142 | { |
|---|
| 143 | viewer.setUpViewOnSingleScreen(screenNum); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 148 | if (!loadedModel) |
|---|
| 149 | { |
|---|
| 150 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 151 | return 1; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | if (arguments.errors()) |
|---|
| 159 | { |
|---|
| 160 | arguments.writeErrorMessages(std::cout); |
|---|
| 161 | return 1; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | osgUtil::Optimizer optimizer; |
|---|
| 167 | optimizer.optimize(loadedModel.get()); |
|---|
| 168 | |
|---|
| 169 | viewer.setSceneData( loadedModel.get() ); |
|---|
| 170 | |
|---|
| 171 | return viewer.run(); |
|---|
| 172 | } |
|---|