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 <osg/Notify> 00021 #include <osgGA/StateSetManipulator> 00022 #include <osgViewer/Viewer> 00023 #include <osgViewer/ViewerEventHandlers> 00024 #include <osgGA/GUIEventHandler> 00025 #include <osgEarth/Map> 00026 #include <osgEarth/MapNode> 00027 #include <osgEarthUtil/EarthManipulator> 00028 #include <osgEarth/Utils> 00029 #include <osgEarth/CompositeTileSource> 00030 00031 #include <osgDB/FileUtils> 00032 #include <osgDB/FileNameUtils> 00033 00034 #include <osgEarthDrivers/gdal/GDALOptions> 00035 00036 using namespace osgEarth; 00037 using namespace osgEarth::Drivers; 00038 using namespace osgEarth::Util; 00039 00040 static void 00041 getFiles(const std::string &file, const std::vector<std::string> &exts, std::vector<std::string> &files) 00042 { 00043 if (osgDB::fileType(file) == osgDB::DIRECTORY) 00044 { 00045 osgDB::DirectoryContents contents = osgDB::getDirectoryContents(file); 00046 for (osgDB::DirectoryContents::iterator itr = contents.begin(); itr != contents.end(); ++itr) 00047 { 00048 if (*itr == "." || *itr == "..") continue; 00049 std::string f = osgDB::concatPaths(file, *itr); 00050 getFiles(f, exts, files); 00051 } 00052 } 00053 else 00054 { 00055 bool fileValid = false; 00056 //If we have no _extensions specified, assume we should try everything 00057 if (exts.size() == 0) 00058 { 00059 fileValid = true; 00060 } 00061 else 00062 { 00063 //Only accept files with the given _extensions 00064 std::string ext = osgDB::getFileExtension(file); 00065 for (unsigned int i = 0; i < exts.size(); ++i) 00066 { 00067 if (osgDB::equalCaseInsensitive(ext, exts[i])) 00068 { 00069 fileValid = true; 00070 break; 00071 } 00072 } 00073 } 00074 00075 if (fileValid) 00076 { 00077 files.push_back(osgDB::convertFileNameToNativeStyle(file)); 00078 } 00079 } 00080 } 00081 00082 // 00083 // Loads a directory of images, demonstrates the programatic use of the CompositeTileSource 00084 // usage: osgearth_composite --dir DIRECTORY_OF_IMAGES --ext EXTENSION [--ext EXTENSION] 00085 // example: osgearth_composite --dir c:\myimages --ext jpg --ext png 00086 // NOTE: run this sample from the repo/tests directory. 00087 // 00088 int main(int argc, char** argv) 00089 { 00090 osg::ArgumentParser arguments(&argc,argv); 00091 00092 osgViewer::Viewer viewer(arguments); 00093 00094 std::vector<std::string> files; 00095 std::vector< std::string > exts; 00096 00097 //Specify a directory 00098 std::string directory = "."; 00099 while (arguments.read("--dir", directory) ); 00100 00101 //Specify the extensions that are valid 00102 std::string ext; 00103 while (arguments.read("--ext", ext)) { exts.push_back( ext ); }; 00104 00105 OE_NOTICE << "directory=" << directory << std::endl; 00106 00107 getFiles(directory, exts, files); 00108 00109 // Start by creating the map: 00110 Map* map = new Map(); 00111 00112 //Add a base layer 00113 GDALOptions basemapOpt; 00114 basemapOpt.url() = URI("../data/world.tif"); 00115 map->addImageLayer( new ImageLayer( ImageLayerOptions("basemap", basemapOpt) ) ); 00116 00117 osgEarth::CompositeTileSourceOptions compositeOpt; 00118 for (unsigned int i = 0; i < files.size(); i++) 00119 { 00120 GDALOptions gdalOpt; 00121 gdalOpt.url() = files[i]; 00122 ImageLayerOptions ilo(files[i], gdalOpt); 00123 //Set the transparent color on each image 00124 //ilo.transparentColor() = osg::Vec4ub(255, 255, 206, 0); 00125 compositeOpt.add( ilo ); 00126 OE_NOTICE << "Added file " << files[i] << std::endl; 00127 } 00128 00129 map->addImageLayer( new ImageLayer( ImageLayerOptions("composite", compositeOpt) ) ); 00130 00131 MapNode* mapNode = new MapNode( map ); 00132 00133 viewer.setSceneData( mapNode ); 00134 viewer.setCameraManipulator( new EarthManipulator() ); 00135 00136 00137 // add some stock OSG handlers: 00138 viewer.addEventHandler(new osgViewer::StatsHandler()); 00139 viewer.addEventHandler(new osgViewer::WindowSizeHandler()); 00140 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); 00141 00142 return viewer.run(); 00143 }