| 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 | |
|---|
| 33 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 34 | #include <osgViewer/Viewer> |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | #include <iostream> |
|---|
| 38 | |
|---|
| 39 | template<class T> |
|---|
| 40 | class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor |
|---|
| 41 | { |
|---|
| 42 | public: |
|---|
| 43 | FindTopMostNodeOfTypeVisitor(): |
|---|
| 44 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 45 | _foundNode(0) |
|---|
| 46 | {} |
|---|
| 47 | |
|---|
| 48 | void apply(osg::Node& node) |
|---|
| 49 | { |
|---|
| 50 | T* result = dynamic_cast<T*>(&node); |
|---|
| 51 | if (result) |
|---|
| 52 | { |
|---|
| 53 | _foundNode = result; |
|---|
| 54 | } |
|---|
| 55 | else |
|---|
| 56 | { |
|---|
| 57 | traverse(node); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | T* _foundNode; |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | template<class T> |
|---|
| 65 | T* findTopMostNodeOfType(osg::Node* node) |
|---|
| 66 | { |
|---|
| 67 | if (!node) return 0; |
|---|
| 68 | |
|---|
| 69 | FindTopMostNodeOfTypeVisitor<T> fnotv; |
|---|
| 70 | node->accept(fnotv); |
|---|
| 71 | |
|---|
| 72 | return fnotv._foundNode; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | class ElevationLayerBlendingCallback : public osg::NodeCallback |
|---|
| 77 | { |
|---|
| 78 | public: |
|---|
| 79 | |
|---|
| 80 | typedef std::vector<double> Elevations; |
|---|
| 81 | |
|---|
| 82 | ElevationLayerBlendingCallback(osgFX::MultiTextureControl* mtc, const Elevations& elevations, float animationTime=4.0f): |
|---|
| 83 | _previousFrame(-1), |
|---|
| 84 | _previousTime(0.0), |
|---|
| 85 | _mtc(mtc), |
|---|
| 86 | _elevations(elevations), |
|---|
| 87 | _animationTime(animationTime) {} |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 91 | { |
|---|
| 92 | if (!nv->getFrameStamp() || _previousFrame==nv->getFrameStamp()->getFrameNumber()) |
|---|
| 93 | { |
|---|
| 94 | |
|---|
| 95 | traverse(node,nv); |
|---|
| 96 | return; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | float deltaTime = 0.01f; |
|---|
| 100 | if (_previousFrame!=-1) |
|---|
| 101 | { |
|---|
| 102 | deltaTime = float(nv->getFrameStamp()->getReferenceTime() - _previousTime); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | _previousTime = nv->getFrameStamp()->getReferenceTime(); |
|---|
| 106 | _previousFrame = nv->getFrameStamp()->getFrameNumber(); |
|---|
| 107 | |
|---|
| 108 | double elevation = nv->getViewPoint().z(); |
|---|
| 109 | |
|---|
| 110 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(node); |
|---|
| 111 | if (csn) |
|---|
| 112 | { |
|---|
| 113 | osg::EllipsoidModel* em = csn->getEllipsoidModel(); |
|---|
| 114 | if (em) |
|---|
| 115 | { |
|---|
| 116 | double X = nv->getViewPoint().x(); |
|---|
| 117 | double Y = nv->getViewPoint().y(); |
|---|
| 118 | double Z = nv->getViewPoint().z(); |
|---|
| 119 | double latitude, longitude; |
|---|
| 120 | em->convertXYZToLatLongHeight(X,Y,Z,latitude, longitude, elevation); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | if (_mtc.valid() && !_elevations.empty()) |
|---|
| 125 | { |
|---|
| 126 | unsigned int index = _mtc->getNumTextureWeights()-1; |
|---|
| 127 | for(unsigned int i=0; i<_elevations.size(); ++i) |
|---|
| 128 | { |
|---|
| 129 | if (elevation>_elevations[i]) |
|---|
| 130 | { |
|---|
| 131 | index = i; |
|---|
| 132 | break; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | float delta = std::min(deltaTime/_animationTime, 1.0f); |
|---|
| 137 | |
|---|
| 138 | for(unsigned int i=0; i<_mtc->getNumTextureWeights(); ++i) |
|---|
| 139 | { |
|---|
| 140 | float currentValue = _mtc->getTextureWeight(i); |
|---|
| 141 | float desiredValue = (i==index) ? 1.0f : 0.0f; |
|---|
| 142 | if (desiredValue != currentValue) |
|---|
| 143 | { |
|---|
| 144 | if (currentValue<desiredValue) |
|---|
| 145 | { |
|---|
| 146 | desiredValue = std::min(currentValue + delta, desiredValue); |
|---|
| 147 | } |
|---|
| 148 | else |
|---|
| 149 | { |
|---|
| 150 | desiredValue = std::max(currentValue - delta, desiredValue); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | _mtc->setTextureWeight(i, desiredValue); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | traverse(node,nv); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | int _previousFrame; |
|---|
| 163 | double _previousTime; |
|---|
| 164 | float _animationTime; |
|---|
| 165 | osg::observer_ptr<osgFX::MultiTextureControl> _mtc; |
|---|
| 166 | Elevations _elevations; |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | int main( int argc, char **argv ) |
|---|
| 171 | { |
|---|
| 172 | |
|---|
| 173 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | osgViewer::Viewer viewer(arguments); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 180 | |
|---|
| 181 | if (!rootnode) |
|---|
| 182 | { |
|---|
| 183 | osg::notify(osg::NOTICE)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl; |
|---|
| 184 | return 1; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | osg::CoordinateSystemNode* csn = findTopMostNodeOfType<osg::CoordinateSystemNode>(rootnode); |
|---|
| 188 | |
|---|
| 189 | unsigned int numLayers = 1; |
|---|
| 190 | osgFX::MultiTextureControl* mtc = findTopMostNodeOfType<osgFX::MultiTextureControl>(rootnode); |
|---|
| 191 | if (mtc) |
|---|
| 192 | { |
|---|
| 193 | numLayers = mtc->getNumTextureWeights(); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | if (numLayers<2) |
|---|
| 197 | { |
|---|
| 198 | osg::notify(osg::NOTICE)<<"Warning: scene must have MultiTextureControl node with at least 2 texture units defined."<<std::endl; |
|---|
| 199 | return 1; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | double maxElevationTransition = 1e6; |
|---|
| 203 | ElevationLayerBlendingCallback::Elevations elevations; |
|---|
| 204 | for(unsigned int i=0; i<numLayers; ++i) |
|---|
| 205 | { |
|---|
| 206 | elevations.push_back(maxElevationTransition); |
|---|
| 207 | maxElevationTransition /= 2.0; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | ElevationLayerBlendingCallback* elbc = new ElevationLayerBlendingCallback(mtc, elevations); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | if (csn) csn->setCullCallback(elbc); |
|---|
| 214 | else if (mtc) mtc->setCullCallback(elbc); |
|---|
| 215 | else rootnode->setCullCallback(elbc); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | { |
|---|
| 219 | |
|---|
| 220 | viewer.setCameraManipulator(new osgGA::TerrainManipulator); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | viewer.addEventHandler(new osgViewer::LODScaleHandler); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | viewer.setSceneData( rootnode ); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | viewer.realize(); |
|---|
| 249 | |
|---|
| 250 | return viewer.run(); |
|---|
| 251 | } |
|---|