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 #include <osg/Image> 00020 #include <osgGA/StateSetManipulator> 00021 #include <osgViewer/Viewer> 00022 #include <osgViewer/ViewerEventHandlers> 00023 #include <osgEarth/Map> 00024 #include <osgEarth/MapNode> 00025 #include <osgEarth/Registry> 00026 #include <osgEarthSymbology/Geometry> 00027 #include <osgEarthSymbology/GeometryRasterizer> 00028 #include <osgEarthUtil/EarthManipulator> 00029 #include <osgEarthUtil/AutoClipPlaneHandler> 00030 00031 using namespace osgEarth; 00032 using namespace osgEarth::Util; 00033 using namespace osgEarth::Symbology; 00034 00039 static osg::Vec4 colors[4] = { 00040 osg::Vec4(1,0,0,1), 00041 osg::Vec4(0,1,0,1), 00042 osg::Vec4(0,0,1,1), 00043 osg::Vec4(0,0,0,1) 00044 }; 00045 00046 class CustomTileSource : public TileSource 00047 { 00048 public: 00049 CustomTileSource( const TileSourceOptions& options =TileSourceOptions() ) : TileSource(options) 00050 { 00051 _geom = new Ring(); 00052 _geom->push_back( osg::Vec3(5, 5, 0) ); 00053 _geom->push_back( osg::Vec3(250, 5, 0) ); 00054 _geom->push_back( osg::Vec3(250, 250, 0) ); 00055 _geom->push_back( osg::Vec3(5, 250, 0) ); 00056 } 00057 00058 void initialize( const std::string& referenceURI, const Profile* overrideProfile ) 00059 { 00060 if ( overrideProfile ) 00061 setProfile( overrideProfile ); 00062 else 00063 setProfile( Registry::instance()->getGlobalGeodeticProfile() ); 00064 } 00065 00066 osg::Image* createImage( const TileKey& key, ProgressCallback* progress ) 00067 { 00068 GeometryRasterizer rasterizer( 256, 256 ); 00069 rasterizer.draw( _geom.get(), colors[key.getLevelOfDetail() % 4] ); 00070 return rasterizer.finalize(); 00071 } 00072 00073 osg::ref_ptr<Ring> _geom; 00074 }; 00075 00076 00077 int main(int argc, char** argv) 00078 { 00079 osg::ArgumentParser arguments(&argc,argv); 00080 00081 osgViewer::Viewer viewer(arguments); 00082 00083 // Start by creating the map: 00084 Map* map = new Map(); 00085 00086 // Create out image layer with a custom tile source. 00087 ImageLayerOptions options( "custom" ); 00088 map->addImageLayer( new ImageLayer(options, new CustomTileSource()) ); 00089 00090 // That's it, the map is ready; now create a MapNode to render the Map: 00091 MapNode* mapNode = new MapNode( map ); 00092 00093 viewer.setSceneData( mapNode ); 00094 viewer.setCameraManipulator( new EarthManipulator() ); 00095 00096 // add some stock OSG handlers: 00097 viewer.addEventHandler(new osgViewer::StatsHandler()); 00098 viewer.addEventHandler(new osgViewer::WindowSizeHandler()); 00099 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); 00100 00101 return viewer.run(); 00102 }