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