| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <osg/Notify> |
|---|
| 21 | |
|---|
| 22 | #include <osg/Texture2D> |
|---|
| 23 | #include <osg/io_utils> |
|---|
| 24 | |
|---|
| 25 | #include <osgDB/Registry> |
|---|
| 26 | #include <osgDB/ReadFile> |
|---|
| 27 | |
|---|
| 28 | #include <osgFX/MultiTextureControl> |
|---|
| 29 | |
|---|
| 30 | #include <osgGA/TerrainManipulator> |
|---|
| 31 | #include <osgGA/StateSetManipulator> |
|---|
| 32 | #include <osgGA/AnimationPathManipulator> |
|---|
| 33 | #include <osgGA/TrackballManipulator> |
|---|
| 34 | #include <osgGA/FlightManipulator> |
|---|
| 35 | #include <osgGA/DriveManipulator> |
|---|
| 36 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 37 | #include <osgGA/StateSetManipulator> |
|---|
| 38 | #include <osgGA/AnimationPathManipulator> |
|---|
| 39 | #include <osgGA/TerrainManipulator> |
|---|
| 40 | |
|---|
| 41 | #include <osgTerrain/TerrainSystem> |
|---|
| 42 | |
|---|
| 43 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 44 | #include <osgViewer/Viewer> |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | #include <iostream> |
|---|
| 48 | |
|---|
| 49 | template<class T> |
|---|
| 50 | class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor |
|---|
| 51 | { |
|---|
| 52 | public: |
|---|
| 53 | FindTopMostNodeOfTypeVisitor(): |
|---|
| 54 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 55 | _foundNode(0) |
|---|
| 56 | {} |
|---|
| 57 | |
|---|
| 58 | void apply(osg::Node& node) |
|---|
| 59 | { |
|---|
| 60 | T* result = dynamic_cast<T*>(&node); |
|---|
| 61 | if (result) |
|---|
| 62 | { |
|---|
| 63 | _foundNode = result; |
|---|
| 64 | } |
|---|
| 65 | else |
|---|
| 66 | { |
|---|
| 67 | traverse(node); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | T* _foundNode; |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | template<class T> |
|---|
| 75 | T* findTopMostNodeOfType(osg::Node* node) |
|---|
| 76 | { |
|---|
| 77 | if (!node) return 0; |
|---|
| 78 | |
|---|
| 79 | FindTopMostNodeOfTypeVisitor<T> fnotv; |
|---|
| 80 | node->accept(fnotv); |
|---|
| 81 | |
|---|
| 82 | return fnotv._foundNode; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | class ElevationLayerBlendingCallback : public osg::NodeCallback |
|---|
| 87 | { |
|---|
| 88 | public: |
|---|
| 89 | |
|---|
| 90 | typedef std::vector<double> Elevations; |
|---|
| 91 | |
|---|
| 92 | ElevationLayerBlendingCallback(osgFX::MultiTextureControl* mtc, const Elevations& elevations, float animationTime=4.0f): |
|---|
| 93 | _previousFrame(-1), |
|---|
| 94 | _previousTime(0.0), |
|---|
| 95 | _mtc(mtc), |
|---|
| 96 | _elevations(elevations), |
|---|
| 97 | _animationTime(animationTime) {} |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 101 | { |
|---|
| 102 | if (!nv->getFrameStamp() || _previousFrame==nv->getFrameStamp()->getFrameNumber()) |
|---|
| 103 | { |
|---|
| 104 | |
|---|
| 105 | traverse(node,nv); |
|---|
| 106 | return; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 110 | |
|---|
| 111 | float deltaTime = 0.01f; |
|---|
| 112 | if (_previousFrame!=-1) |
|---|
| 113 | { |
|---|
| 114 | deltaTime = float(nv->getFrameStamp()->getReferenceTime() - _previousTime); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | _previousTime = nv->getFrameStamp()->getReferenceTime(); |
|---|
| 118 | _previousFrame = nv->getFrameStamp()->getFrameNumber(); |
|---|
| 119 | |
|---|
| 120 | double elevation = nv->getViewPoint().z(); |
|---|
| 121 | |
|---|
| 122 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(node); |
|---|
| 123 | if (csn) |
|---|
| 124 | { |
|---|
| 125 | osg::EllipsoidModel* em = csn->getEllipsoidModel(); |
|---|
| 126 | if (em) |
|---|
| 127 | { |
|---|
| 128 | double X = nv->getViewPoint().x(); |
|---|
| 129 | double Y = nv->getViewPoint().y(); |
|---|
| 130 | double Z = nv->getViewPoint().z(); |
|---|
| 131 | double latitude, longitude; |
|---|
| 132 | em->convertXYZToLatLongHeight(X,Y,Z,latitude, longitude, elevation); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | if (_mtc.valid() && !_elevations.empty()) |
|---|
| 137 | { |
|---|
| 138 | unsigned int index = _mtc->getNumTextureWeights()-1; |
|---|
| 139 | for(unsigned int i=0; i<_elevations.size(); ++i) |
|---|
| 140 | { |
|---|
| 141 | if (elevation>_elevations[i]) |
|---|
| 142 | { |
|---|
| 143 | index = i; |
|---|
| 144 | break; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | float delta = std::min(deltaTime/_animationTime, 1.0f); |
|---|
| 149 | |
|---|
| 150 | for(unsigned int i=0; i<_mtc->getNumTextureWeights(); ++i) |
|---|
| 151 | { |
|---|
| 152 | float currentValue = _mtc->getTextureWeight(i); |
|---|
| 153 | float desiredValue = (i==index) ? 1.0f : 0.0f; |
|---|
| 154 | if (desiredValue != currentValue) |
|---|
| 155 | { |
|---|
| 156 | if (currentValue<desiredValue) |
|---|
| 157 | { |
|---|
| 158 | desiredValue = std::min(currentValue + delta, desiredValue); |
|---|
| 159 | } |
|---|
| 160 | else |
|---|
| 161 | { |
|---|
| 162 | desiredValue = std::max(currentValue - delta, desiredValue); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | _mtc->setTextureWeight(i, desiredValue); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | traverse(node,nv); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | int _previousFrame; |
|---|
| 175 | double _previousTime; |
|---|
| 176 | float _animationTime; |
|---|
| 177 | osg::observer_ptr<osgFX::MultiTextureControl> _mtc; |
|---|
| 178 | Elevations _elevations; |
|---|
| 179 | |
|---|
| 180 | OpenThreads::Mutex _mutex; |
|---|
| 181 | }; |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | int main( int argc, char **argv ) |
|---|
| 185 | { |
|---|
| 186 | |
|---|
| 187 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | osgViewer::Viewer viewer(arguments); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 194 | |
|---|
| 195 | if (!rootnode) |
|---|
| 196 | { |
|---|
| 197 | osg::notify(osg::NOTICE)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl; |
|---|
| 198 | return 1; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | osgTerrain::TerrainSystem* terrain = findTopMostNodeOfType<osgTerrain::TerrainSystem>(rootnode); |
|---|
| 202 | if (!terrain) |
|---|
| 203 | { |
|---|
| 204 | terrain = new osgTerrain::TerrainSystem; |
|---|
| 205 | terrain->addChild(rootnode); |
|---|
| 206 | |
|---|
| 207 | rootnode = terrain; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | osg::CoordinateSystemNode* csn = findTopMostNodeOfType<osg::CoordinateSystemNode>(rootnode); |
|---|
| 211 | |
|---|
| 212 | unsigned int numLayers = 1; |
|---|
| 213 | osgFX::MultiTextureControl* mtc = findTopMostNodeOfType<osgFX::MultiTextureControl>(rootnode); |
|---|
| 214 | if (mtc) |
|---|
| 215 | { |
|---|
| 216 | numLayers = mtc->getNumTextureWeights(); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | if (numLayers<2) |
|---|
| 220 | { |
|---|
| 221 | osg::notify(osg::NOTICE)<<"Warning: scene must have MultiTextureControl node with at least 2 texture units defined."<<std::endl; |
|---|
| 222 | return 1; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | double maxElevationTransition = 1e6; |
|---|
| 226 | ElevationLayerBlendingCallback::Elevations elevations; |
|---|
| 227 | for(unsigned int i=0; i<numLayers; ++i) |
|---|
| 228 | { |
|---|
| 229 | elevations.push_back(maxElevationTransition); |
|---|
| 230 | maxElevationTransition /= 2.0; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | ElevationLayerBlendingCallback* elbc = new ElevationLayerBlendingCallback(mtc, elevations); |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | if (csn) csn->setCullCallback(elbc); |
|---|
| 237 | else if (mtc) mtc->setCullCallback(elbc); |
|---|
| 238 | else rootnode->setCullCallback(elbc); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | { |
|---|
| 242 | |
|---|
| 243 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | viewer.addEventHandler(new osgViewer::LODScaleHandler); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | { |
|---|
| 265 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 266 | |
|---|
| 267 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 268 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 269 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 270 | |
|---|
| 271 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 272 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 273 | |
|---|
| 274 | std::string pathfile; |
|---|
| 275 | char keyForAnimationPath = '5'; |
|---|
| 276 | while (arguments.read("-p",pathfile)) |
|---|
| 277 | { |
|---|
| 278 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 279 | if (apm || !apm->valid()) |
|---|
| 280 | { |
|---|
| 281 | num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 282 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 283 | ++keyForAnimationPath; |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 288 | |
|---|
| 289 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | viewer.setSceneData( rootnode ); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | viewer.realize(); |
|---|
| 297 | |
|---|
| 298 | return viewer.run(); |
|---|
| 299 | } |
|---|