osgEarth 2.1.1
|
00001 /* -*-c++-*- */ 00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph 00003 * Copyright 2008-2010 Pelican Mapping 00004 * http://osgearth.org 00005 * 00006 * osgEarth is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/> 00018 */ 00019 00020 #include <osgEarth/MapNode> 00021 #include <osgEarthUtil/EarthManipulator> 00022 #include <osgEarthUtil/Annotation> 00023 #include <osgEarthUtil/ImageOverlay> 00024 #include <osgEarthUtil/ImageOverlayEditor> 00025 #include <osgEarthSymbology/GeometryFactory> 00026 #include <osgViewer/Viewer> 00027 #include <osgViewer/ViewerEventHandlers> 00028 #include <osgGA/StateSetManipulator> 00029 00030 using namespace osgEarth; 00031 using namespace osgEarth::Util; 00032 using namespace osgEarth::Util::Controls; 00033 using namespace osgEarth::Util::Annotation; 00034 00035 int 00036 usage( char** argv ) 00037 { 00038 OE_WARN << "Usage: " << argv[0] << " <earthfile>" << std::endl; 00039 return -1; 00040 } 00041 00042 struct ToggleNode : public ControlEventHandler { 00043 ToggleNode( osg::Node* node ) : _node( node ) { } 00044 void onValueChanged( Control* src, bool value ) { 00045 if ( _node.valid() ) 00046 _node->setNodeMask( value ? ~0 : 0 ); 00047 } 00048 osg::observer_ptr<osg::Node> _node; 00049 }; 00050 00051 int 00052 main(int argc, char** argv) 00053 { 00054 osg::Group* root = new osg::Group(); 00055 00056 // try to load an earth file. 00057 osg::ArgumentParser arguments(&argc,argv); 00058 osg::Node* node = osgDB::readNodeFiles( arguments ); 00059 if ( !node ) 00060 return usage(argv); 00061 00062 // find the map node that we loaded. 00063 MapNode* mapNode = MapNode::findMapNode(node); 00064 if ( !mapNode ) 00065 return usage(argv); 00066 00067 root->addChild( mapNode ); 00068 00069 // make some annotations. 00070 osg::Group* annoGroup = new osg::Group(); 00071 root->addChild( annoGroup ); 00072 00073 // a Placemark combines a 2D icon with a text label. 00074 annoGroup->addChild( new PlacemarkNode( 00075 mapNode, 00076 osg::Vec3d(-74, 40.714, 0), 00077 URI("../data/placemark32.png").readImage(), 00078 "New York") ); 00079 00080 // a Placemark combines a 2D icon with a text label. 00081 annoGroup->addChild( new PlacemarkNode( 00082 mapNode, 00083 osg::Vec3d(139.75, 35.685, 0), 00084 URI("../data/placemark32.png").readImage(), 00085 "Tokyo" ) ); 00086 00087 // a box that follows lines of latitude (rhumb line interpolation, the default) 00088 Geometry* geom = new Ring(); 00089 geom->push_back( osg::Vec3d(0, 40, 0) ); 00090 geom->push_back( osg::Vec3d(-60, 40, 0) ); 00091 geom->push_back( osg::Vec3d(-60, 60, 0) ); 00092 geom->push_back( osg::Vec3d(0, 60, 0) ); 00093 Style geomStyle; 00094 00095 geomStyle.getOrCreate<LineSymbol>()->stroke()->color() = Color::Yellow; 00096 geomStyle.getOrCreate<LineSymbol>()->stroke()->width() = 5.0f; 00097 00098 FeatureNode* gnode = new FeatureNode(mapNode, new Feature(geom, geomStyle), true); 00099 annoGroup->addChild( gnode ); 00100 00101 // another line, this time using great-circle interpolation (flight path from New York to Tokyo) 00102 Geometry* path = new LineString(); 00103 path->push_back( osg::Vec3d(-74, 40.714, 0) ); // New York 00104 path->push_back( osg::Vec3d(139.75, 35.685, 0) ); // Tokyo 00105 00106 Style pathStyle; 00107 pathStyle.getOrCreate<LineSymbol>()->stroke()->color() = Color::Red; 00108 pathStyle.getOrCreate<LineSymbol>()->stroke()->width() = 10.0f; 00109 00110 Feature* pathFeature = new Feature(path, pathStyle); 00111 pathFeature->geoInterp() = GEOINTERP_GREAT_CIRCLE; 00112 FeatureNode* pathNode = new FeatureNode(mapNode, pathFeature, true); 00113 annoGroup->addChild( pathNode ); 00114 00115 // a circle around New Orleans 00116 Style circleStyle; 00117 circleStyle.getOrCreate<PolygonSymbol>()->fill()->color() = Color(Color::Cyan, 0.5); 00118 CircleNode* circle = new CircleNode( 00119 mapNode, 00120 osg::Vec3d(-90.25, 29.98, 0), 00121 Linear(600, Units::KILOMETERS), 00122 circleStyle ); 00123 annoGroup->addChild( circle ); 00124 00125 // an ellipse around Miami 00126 Style ellipseStyle; 00127 ellipseStyle.getOrCreate<PolygonSymbol>()->fill()->color() = Color(Color::Orange, 0.75); 00128 EllipseNode* ellipse = new EllipseNode( 00129 mapNode, 00130 osg::Vec3d(-80.28,25.82,0), 00131 Linear(200, Units::MILES), 00132 Linear(100, Units::MILES), 00133 Angular(45, Units::DEGREES), 00134 ellipseStyle, 00135 true); 00136 annoGroup->addChild( ellipse ); 00137 00138 // an extruded polygon roughly the shape of Utah 00139 Geometry* utah = new Polygon(); 00140 utah->push_back( osg::Vec3d(-114.052, 37, 0) ); 00141 utah->push_back( osg::Vec3d(-109.054, 37, 0) ); 00142 utah->push_back( osg::Vec3d(-109.054, 41, 0) ); 00143 utah->push_back( osg::Vec3d(-111.04, 41, 0) ); 00144 utah->push_back( osg::Vec3d(-111.08, 42.059, 0) ); 00145 utah->push_back( osg::Vec3d(-114.08, 42.024, 0) ); 00146 00147 Style utahStyle; 00148 utahStyle.getOrCreate<ExtrusionSymbol>()->height() = 250000.0; // meters MSL 00149 utahStyle.getOrCreate<PolygonSymbol>()->fill()->color() = Color(Color::White, 0.8); 00150 00151 Feature* utahFeature = new Feature(utah, utahStyle); 00152 FeatureNode* utahNode = new FeatureNode(mapNode, utahFeature, false); 00153 annoGroup->addChild( utahNode ); 00154 00155 // an image overlay 00156 ImageOverlay* imageOverlay = 0L; 00157 osg::Image* image = osgDB::readImageFile( "../data/USFLAG.TGA" ); 00158 if ( image ) { 00159 imageOverlay = new ImageOverlay(mapNode, image); 00160 imageOverlay->setBounds( Bounds( -100.0, 50.0, -90.0, 55.0) ); 00161 00162 //Add an editor 00163 annoGroup->addChild( imageOverlay ); 00164 00165 osg::Node* editor = new ImageOverlayEditor( imageOverlay, mapNode->getMap()->getProfile()->getSRS()->getEllipsoid(), mapNode ); 00166 root->addChild( editor ); 00167 00168 00169 00170 } 00171 00172 00173 00174 00175 // initialize a viewer: 00176 osgViewer::Viewer viewer(arguments); 00177 viewer.setCameraManipulator( new EarthManipulator() ); 00178 viewer.setSceneData( root ); 00179 00180 // make a little HUD to toggle stuff: 00181 VBox* vbox = new VBox(); 00182 vbox->setBackColor( Color(Color::Black, 0.5) ); 00183 vbox->setVertAlign( Control::ALIGN_TOP ); 00184 vbox->addControl( new LabelControl("Annotation Example", 22.0f, Color::Yellow) ); 00185 Grid* grid = new Grid(); 00186 vbox->addControl( grid ); 00187 grid->setChildSpacing( 5 ); 00188 grid->setChildHorizAlign( Control::ALIGN_LEFT ); 00189 grid->setChildVertAlign( Control::ALIGN_CENTER ); 00190 grid->setControl( 0, 0, new CheckBoxControl(true, new ToggleNode(gnode)) ); 00191 grid->setControl( 1, 0, new LabelControl("Line geoemtry") ); 00192 grid->setControl( 0, 1, new CheckBoxControl(true, new ToggleNode(pathNode)) ); 00193 grid->setControl( 1, 1, new LabelControl("Red flight path") ); 00194 grid->setControl( 0, 2, new CheckBoxControl(true, new ToggleNode(circle)) ); 00195 grid->setControl( 1, 2, new LabelControl("Blue circle") ); 00196 grid->setControl( 0, 3, new CheckBoxControl(true, new ToggleNode(ellipse)) ); 00197 grid->setControl( 1, 3, new LabelControl("Orange ellipse") ); 00198 grid->setControl( 0, 4, new CheckBoxControl(true, new ToggleNode(utahNode)) ); 00199 grid->setControl( 1, 4, new LabelControl("Extruded state") ); 00200 if ( imageOverlay ) { 00201 grid->setControl( 0, 5, new CheckBoxControl(true, new ToggleNode(imageOverlay)) ); 00202 grid->setControl( 1, 5, new LabelControl("Image overlay") ); 00203 } 00204 ControlCanvas::get(&viewer,true)->addControl(vbox); 00205 00206 // add some stock OSG handlers: 00207 viewer.getDatabasePager()->setDoPreCompile( true ); 00208 viewer.addEventHandler(new osgViewer::StatsHandler()); 00209 viewer.addEventHandler(new osgViewer::WindowSizeHandler()); 00210 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); 00211 00212 return viewer.run(); 00213 }