| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/ArgumentParser> |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| 21 | |
|---|
| 22 | #include <osgViewer/Viewer> |
|---|
| 23 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 24 | |
|---|
| 25 | #include <osgGA/TrackballManipulator> |
|---|
| 26 | #include <osgGA/FlightManipulator> |
|---|
| 27 | #include <osgGA/DriveManipulator> |
|---|
| 28 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 29 | #include <osgGA/StateSetManipulator> |
|---|
| 30 | #include <osgGA/AnimationPathManipulator> |
|---|
| 31 | #include <osgGA/TerrainManipulator> |
|---|
| 32 | |
|---|
| 33 | #include <osgTerrain/Terrain> |
|---|
| 34 | #include <osgTerrain/TerrainTile> |
|---|
| 35 | #include <osgTerrain/GeometryTechnique> |
|---|
| 36 | #include <osgTerrain/Layer> |
|---|
| 37 | |
|---|
| 38 | #include <iostream> |
|---|
| 39 | |
|---|
| 40 | template<class T> |
|---|
| 41 | class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | FindTopMostNodeOfTypeVisitor(): |
|---|
| 45 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 46 | _foundNode(0) |
|---|
| 47 | {} |
|---|
| 48 | |
|---|
| 49 | void apply(osg::Node& node) |
|---|
| 50 | { |
|---|
| 51 | T* result = dynamic_cast<T*>(&node); |
|---|
| 52 | if (result) |
|---|
| 53 | { |
|---|
| 54 | _foundNode = result; |
|---|
| 55 | } |
|---|
| 56 | else |
|---|
| 57 | { |
|---|
| 58 | traverse(node); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | T* _foundNode; |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | template<class T> |
|---|
| 66 | T* findTopMostNodeOfType(osg::Node* node) |
|---|
| 67 | { |
|---|
| 68 | if (!node) return 0; |
|---|
| 69 | |
|---|
| 70 | FindTopMostNodeOfTypeVisitor<T> fnotv; |
|---|
| 71 | node->accept(fnotv); |
|---|
| 72 | |
|---|
| 73 | return fnotv._foundNode; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | int main(int argc, char** argv) |
|---|
| 77 | { |
|---|
| 78 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | osgViewer::Viewer viewer(arguments); |
|---|
| 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 | |
|---|
| 111 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | float verticalScale = 1.0f; |
|---|
| 121 | while(arguments.read("-v",verticalScale)) {} |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | float sampleRatio = 1.0f; |
|---|
| 125 | while(arguments.read("-r",sampleRatio)) {} |
|---|
| 126 | |
|---|
| 127 | osgTerrain::TerrainTile::BlendingPolicy blendingPolicy = osgTerrain::TerrainTile::INHERIT; |
|---|
| 128 | std::string strBlendingPolicy; |
|---|
| 129 | while(arguments.read("--blending-policy", strBlendingPolicy)) |
|---|
| 130 | { |
|---|
| 131 | if (strBlendingPolicy == "INHERIT") blendingPolicy = osgTerrain::TerrainTile::INHERIT; |
|---|
| 132 | else if (strBlendingPolicy == "DO_NOT_SET_BLENDING") blendingPolicy = osgTerrain::TerrainTile::DO_NOT_SET_BLENDING; |
|---|
| 133 | else if (strBlendingPolicy == "ENABLE_BLENDING") blendingPolicy = osgTerrain::TerrainTile::ENABLE_BLENDING; |
|---|
| 134 | else if (strBlendingPolicy == "ENABLE_BLENDING_WHEN_ALPHA_PRESENT") blendingPolicy = osgTerrain::TerrainTile::ENABLE_BLENDING_WHEN_ALPHA_PRESENT; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 139 | |
|---|
| 140 | if (!rootnode) |
|---|
| 141 | { |
|---|
| 142 | osg::notify(osg::NOTICE)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl; |
|---|
| 143 | return 1; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | osgTerrain::Terrain* terrain = findTopMostNodeOfType<osgTerrain::Terrain>(rootnode); |
|---|
| 147 | if (!terrain) |
|---|
| 148 | { |
|---|
| 149 | terrain = new osgTerrain::Terrain; |
|---|
| 150 | terrain->addChild(rootnode); |
|---|
| 151 | |
|---|
| 152 | rootnode = terrain; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | terrain->setSampleRatio(sampleRatio); |
|---|
| 156 | terrain->setVerticalScale(verticalScale); |
|---|
| 157 | terrain->setBlendingPolicy(blendingPolicy); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | viewer.setSceneData( rootnode ); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | return viewer.run(); |
|---|
| 165 | |
|---|
| 166 | } |
|---|