| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 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 | |
|---|
| 32 | int main(int argc, char** argv) |
|---|
| 33 | { |
|---|
| 34 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 110 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 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 | |
|---|
| 136 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | if (arguments.errors()) |
|---|
| 140 | { |
|---|
| 141 | arguments.writeErrorMessages(std::cout); |
|---|
| 142 | return 1; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 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 | for(unsigned int i=0; i<osg::GraphicsContext::getMaxContextID(); ++i) |
|---|
| 161 | { |
|---|
| 162 | osg::GraphicsContext* gc = osg::GraphicsContext::getOrCreateCompileContext(i); |
|---|
| 163 | |
|---|
| 164 | if (gc && createBackgroundThreadsForCompiling) |
|---|
| 165 | { |
|---|
| 166 | gc->createGraphicsThread(); |
|---|
| 167 | gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); |
|---|
| 168 | gc->getGraphicsThread()->startThread(); |
|---|
| 169 | |
|---|
| 170 | ++processNum; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | viewer.run(); |
|---|
| 176 | |
|---|
| 177 | } |
|---|