| 1 | #include <osg/Group> |
|---|
| 2 | #include <osg/Geode> |
|---|
| 3 | #include <osg/ShapeDrawable> |
|---|
| 4 | #include <osg/Texture2D> |
|---|
| 5 | #include <osg/PositionAttitudeTransform> |
|---|
| 6 | #include <osg/MatrixTransform> |
|---|
| 7 | #include <osg/CoordinateSystemNode> |
|---|
| 8 | #include <osg/ClusterCullingCallback> |
|---|
| 9 | #include <osg/ArgumentParser> |
|---|
| 10 | |
|---|
| 11 | #include <osgDB/FileUtils> |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | |
|---|
| 14 | #include <osgText/FadeText> |
|---|
| 15 | |
|---|
| 16 | #include <osgViewer/Viewer> |
|---|
| 17 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 18 | |
|---|
| 19 | #include <osgGA/TrackballManipulator> |
|---|
| 20 | #include <osgGA/FlightManipulator> |
|---|
| 21 | #include <osgGA/DriveManipulator> |
|---|
| 22 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 23 | #include <osgGA/StateSetManipulator> |
|---|
| 24 | #include <osgGA/AnimationPathManipulator> |
|---|
| 25 | #include <osgGA/TerrainManipulator> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | #include <osgTerrain/TerrainNode> |
|---|
| 29 | #include <osgTerrain/GeometryTechnique> |
|---|
| 30 | #include <osgTerrain/Layer> |
|---|
| 31 | |
|---|
| 32 | #include <iostream> |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | int main(int argc, char** argv) |
|---|
| 38 | { |
|---|
| 39 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | osgViewer::Viewer viewer; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | { |
|---|
| 46 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 47 | |
|---|
| 48 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 49 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 50 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 51 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 52 | |
|---|
| 53 | std::string pathfile; |
|---|
| 54 | char keyForAnimationPath = '5'; |
|---|
| 55 | while (arguments.read("-p",pathfile)) |
|---|
| 56 | { |
|---|
| 57 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 58 | if (apm || !apm->valid()) |
|---|
| 59 | { |
|---|
| 60 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 61 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 62 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 63 | ++keyForAnimationPath; |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 76 | |
|---|
| 77 | double x = 0.0; |
|---|
| 78 | double y = 0.0; |
|---|
| 79 | double w = 1.0; |
|---|
| 80 | double h = 1.0; |
|---|
| 81 | |
|---|
| 82 | osg::ref_ptr<osgTerrain::TerrainNode> terrain = new osgTerrain::TerrainNode; |
|---|
| 83 | osg::ref_ptr<osgTerrain::Locator> locator = new osgTerrain::EllipsoidLocator(-osg::PI, -osg::PI*0.5, 2.0*osg::PI, osg::PI, 0.0); |
|---|
| 84 | osg::ref_ptr<osgTerrain::ValidDataOperator> validDataOperator = new osgTerrain::NoDataValue(0.0); |
|---|
| 85 | |
|---|
| 86 | unsigned int layerNum = 0; |
|---|
| 87 | |
|---|
| 88 | std::string filterName; |
|---|
| 89 | |
|---|
| 90 | bool readParameter = false; |
|---|
| 91 | float minValue, maxValue; |
|---|
| 92 | float scale = 1.0f; |
|---|
| 93 | float offset = 0.0f; |
|---|
| 94 | |
|---|
| 95 | int pos = 1; |
|---|
| 96 | while(pos<arguments.argc()) |
|---|
| 97 | { |
|---|
| 98 | readParameter = false; |
|---|
| 99 | std::string filename; |
|---|
| 100 | |
|---|
| 101 | if (arguments.read(pos, "--layer",layerNum)) |
|---|
| 102 | { |
|---|
| 103 | osg::notify(osg::NOTICE)<<"Set layer number to "<<layerNum<<std::endl; |
|---|
| 104 | readParameter = true; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | else if (arguments.read(pos, "-b")) |
|---|
| 108 | { |
|---|
| 109 | terrain->setTreatBoundariesToValidDataAsDefaultValue(true); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | else if (arguments.read(pos, "-e",x,y,w,h)) |
|---|
| 113 | { |
|---|
| 114 | |
|---|
| 115 | locator = new osgTerrain::EllipsoidLocator(x,y,w,h,0); |
|---|
| 116 | readParameter = true; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | else if (arguments.read(pos, "--transform",offset, scale) || arguments.read(pos, "-t",offset, scale)) |
|---|
| 120 | { |
|---|
| 121 | |
|---|
| 122 | readParameter = true; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | else if (arguments.read(pos, "--cartizian",x,y,w,h)) |
|---|
| 126 | { |
|---|
| 127 | |
|---|
| 128 | locator = new osgTerrain::CartizianLocator(x,y,w,h,0); |
|---|
| 129 | readParameter = true; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | else if (arguments.read(pos, "--hf",filename)) |
|---|
| 133 | { |
|---|
| 134 | readParameter = true; |
|---|
| 135 | |
|---|
| 136 | osg::notify(osg::NOTICE)<<"--hf "<<filename<<std::endl; |
|---|
| 137 | |
|---|
| 138 | osg::ref_ptr<osg::HeightField> hf = osgDB::readHeightFieldFile(filename); |
|---|
| 139 | if (hf.valid()) |
|---|
| 140 | { |
|---|
| 141 | osg::ref_ptr<osgTerrain::HeightFieldLayer> hfl = new osgTerrain::HeightFieldLayer; |
|---|
| 142 | hfl->setHeightField(hf.get()); |
|---|
| 143 | |
|---|
| 144 | hfl->setLocator(locator.get()); |
|---|
| 145 | hfl->setValidDataOperator(validDataOperator.get()); |
|---|
| 146 | |
|---|
| 147 | if (offset!=0.0f || scale!=1.0f) |
|---|
| 148 | { |
|---|
| 149 | hfl->transform(offset,scale); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | terrain->setElevationLayer(hfl.get()); |
|---|
| 153 | |
|---|
| 154 | osg::notify(osg::NOTICE)<<"created osgTerrain::HeightFieldLayer"<<std::endl; |
|---|
| 155 | } |
|---|
| 156 | else |
|---|
| 157 | { |
|---|
| 158 | osg::notify(osg::NOTICE)<<"failed to create osgTerrain::HeightFieldLayer"<<std::endl; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | scale = 1.0f; |
|---|
| 162 | offset = 0.0f; |
|---|
| 163 | |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | else if (arguments.read(pos, "-d",filename) || arguments.read(pos, "--elevation-image",filename)) |
|---|
| 167 | { |
|---|
| 168 | readParameter = true; |
|---|
| 169 | osg::notify(osg::NOTICE)<<"--elevation-image "<<filename<<std::endl; |
|---|
| 170 | |
|---|
| 171 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename); |
|---|
| 172 | if (image.valid()) |
|---|
| 173 | { |
|---|
| 174 | osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer; |
|---|
| 175 | imageLayer->setImage(image.get()); |
|---|
| 176 | imageLayer->setLocator(locator.get()); |
|---|
| 177 | imageLayer->setValidDataOperator(validDataOperator.get()); |
|---|
| 178 | |
|---|
| 179 | if (offset!=0.0f || scale!=1.0f) |
|---|
| 180 | { |
|---|
| 181 | imageLayer->transform(offset,scale); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | terrain->setElevationLayer(imageLayer.get()); |
|---|
| 185 | |
|---|
| 186 | osg::notify(osg::NOTICE)<<"created Elevation osgTerrain::ImageLayer"<<std::endl; |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | { |
|---|
| 190 | osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | scale = 1.0f; |
|---|
| 194 | offset = 0.0f; |
|---|
| 195 | |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | else if (arguments.read(pos, "-c",filename) || arguments.read(pos, "--image",filename)) |
|---|
| 199 | { |
|---|
| 200 | readParameter = true; |
|---|
| 201 | osg::notify(osg::NOTICE)<<"--image "<<filename<<" x="<<x<<" y="<<y<<" w="<<w<<" h="<<h<<std::endl; |
|---|
| 202 | |
|---|
| 203 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename); |
|---|
| 204 | if (image.valid()) |
|---|
| 205 | { |
|---|
| 206 | osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer; |
|---|
| 207 | imageLayer->setImage(image.get()); |
|---|
| 208 | imageLayer->setLocator(locator.get()); |
|---|
| 209 | imageLayer->setValidDataOperator(validDataOperator.get()); |
|---|
| 210 | |
|---|
| 211 | if (offset!=0.0f || scale!=1.0f) |
|---|
| 212 | { |
|---|
| 213 | imageLayer->transform(offset,scale); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | terrain->setColorLayer(layerNum, imageLayer.get()); |
|---|
| 217 | |
|---|
| 218 | osg::notify(osg::NOTICE)<<"created Color osgTerrain::ImageLayer"<<std::endl; |
|---|
| 219 | } |
|---|
| 220 | else |
|---|
| 221 | { |
|---|
| 222 | osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | scale = 1.0f; |
|---|
| 226 | offset = 0.0f; |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | else if (arguments.read(pos, "--filter",filterName)) |
|---|
| 231 | { |
|---|
| 232 | readParameter = true; |
|---|
| 233 | |
|---|
| 234 | if (filterName=="NEAREST") |
|---|
| 235 | { |
|---|
| 236 | osg::notify(osg::NOTICE)<<"--filter "<<filterName<<std::endl; |
|---|
| 237 | terrain->setColorFilter(layerNum, osgTerrain::TerrainNode::NEAREST); |
|---|
| 238 | } |
|---|
| 239 | else if (filterName=="LINEAER") |
|---|
| 240 | { |
|---|
| 241 | osg::notify(osg::NOTICE)<<"--filter "<<filterName<<std::endl; |
|---|
| 242 | terrain->setColorFilter(layerNum, osgTerrain::TerrainNode::LINEAR); |
|---|
| 243 | } |
|---|
| 244 | else |
|---|
| 245 | { |
|---|
| 246 | osg::notify(osg::NOTICE)<<"--filter "<<filterName<<" unrecognized filter name, please use LINEAER or NEAREST."<<std::endl; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | else if (arguments.read(pos, "--tf",minValue, maxValue)) |
|---|
| 252 | { |
|---|
| 253 | readParameter = true; |
|---|
| 254 | |
|---|
| 255 | osg::ref_ptr<osg::TransferFunction1D> tf = new osg::TransferFunction1D; |
|---|
| 256 | |
|---|
| 257 | tf->setInputRange(minValue, maxValue); |
|---|
| 258 | |
|---|
| 259 | tf->allocate(6); |
|---|
| 260 | tf->setValue(0, osg::Vec4(1.0,1.0,1.0,1.0)); |
|---|
| 261 | tf->setValue(1, osg::Vec4(1.0,0.0,1.0,1.0)); |
|---|
| 262 | tf->setValue(2, osg::Vec4(1.0,0.0,0.0,1.0)); |
|---|
| 263 | tf->setValue(3, osg::Vec4(1.0,1.0,0.0,1.0)); |
|---|
| 264 | tf->setValue(4, osg::Vec4(0.0,1.0,1.0,1.0)); |
|---|
| 265 | tf->setValue(5, osg::Vec4(0.0,1.0,0.0,1.0)); |
|---|
| 266 | |
|---|
| 267 | osg::notify(osg::NOTICE)<<"--tf "<<minValue<<" "<<maxValue<<std::endl; |
|---|
| 268 | |
|---|
| 269 | terrain->setColorTransferFunction(layerNum, tf.get()); |
|---|
| 270 | } |
|---|
| 271 | else |
|---|
| 272 | { |
|---|
| 273 | ++pos; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | osg::ref_ptr<osgTerrain::GeometryTechnique> geometryTechnique = new osgTerrain::GeometryTechnique; |
|---|
| 280 | |
|---|
| 281 | terrain->setTerrainTechnique(geometryTechnique.get()); |
|---|
| 282 | |
|---|
| 283 | if (!terrain) return 0; |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | viewer.setSceneData(terrain.get()); |
|---|
| 289 | |
|---|
| 290 | return viewer.run(); |
|---|
| 291 | } |
|---|