osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthDrivers/engine_osgterrain/Plugin.cpp

Go to the documentation of this file.
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 "OSGTerrainEngineNode"
00020 #include "OSGTerrainOptions"
00021 #include <osgEarth/Registry>
00022 #include <osgDB/FileNameUtils>
00023 #include <osgDB/FileUtils>
00024 #include <osgDB/Registry>
00025 #include <string>
00026 
00027 #define LC "[osgterrain_engine Plugin] "
00028 
00029 using namespace osgEarth::Drivers;
00030 
00031 class OSGTerrainEnginePlugin : public osgDB::ReaderWriter
00032 {
00033 public:
00034     OSGTerrainEnginePlugin() {}
00035 
00036     virtual const char* className()
00037     {
00038         return "osgEarth osgTerrain Engine";
00039     }
00040 
00041     virtual bool acceptsExtension(const std::string& extension) const
00042     {
00043         return
00044             osgDB::equalCaseInsensitive( extension, "osgearth_engine_osgterrain" ) ||
00045             osgDB::equalCaseInsensitive( extension, "osgearth_osgterrain_tile" );
00046     }
00047 
00048     virtual ReadResult readObject(const std::string& uri, const Options* options) const
00049     {
00050         if ( "osgearth_engine_osgterrain" == osgDB::getFileExtension( uri ) )
00051         {
00052             if ( "earth" == osgDB::getNameLessExtension( osgDB::getFileExtension( uri ) ) )
00053             {
00054                 return readNode( uri, options );
00055             }
00056             else
00057             {
00058                 OSGTerrainOptions terrainOpts;
00059                 return ReadResult( new OSGTerrainEngineNode() );
00060             }
00061         }
00062         else
00063         {
00064             return readNode( uri, options );
00065         }
00066     }    
00067 
00068     virtual ReadResult readNode(const std::string& uri, const Options* options) const
00069     {
00070         static int s_tileCount = 0;
00071         static double s_tileTime = 0.0;
00072         static osg::Timer_t s_startTime;
00073 
00074         if ( "osgearth_osgterrain_tile" == osgDB::getFileExtension(uri) )
00075         {
00076             if ( s_tileCount == 0 )
00077                 s_startTime = osg::Timer::instance()->tick();
00078 
00079             // See if the filename starts with server: and strip it off.  This will trick OSG
00080             // into passing in the filename to our plugin instead of using the CURL plugin if
00081             // the filename contains a URL.  So, if you want to read a URL, you can use the
00082             // following format: osgDB::readNodeFile("server:http://myserver/myearth.earth").
00083             // This should only be necessary for the first level as the other files will have
00084             // a tilekey prepended to them.
00085             if ((uri.length() > 7) && (uri.substr(0, 7) == "server:"))
00086                 return readNode(uri.substr(7), options);
00087 
00088             // parse the tile key and engine ID:
00089             std::string tileDef = osgDB::getNameLessExtension(uri);
00090             unsigned int lod, x, y, engineID;
00091             sscanf(tileDef.c_str(), "%d_%d_%d.%d", &lod, &x, &y, &engineID);
00092 
00093             // find the appropriate engine:
00094             osg::ref_ptr<OSGTerrainEngineNode> engineNode;
00095             OSGTerrainEngineNode::getEngineByUID( (UID)engineID, engineNode );
00096             if ( engineNode.valid() )
00097             {
00098                 osg::Timer_t start = osg::Timer::instance()->tick();
00099 
00100                 // assemble the key and create the node:
00101                 const Profile* profile = engineNode->getMap()->getProfile();
00102                 TileKey key( lod, x, y, profile );
00103                 osg::Node* node = engineNode->createNode( key );
00104                 
00105                 // Blacklist the tile if we couldn't load it
00106                 if ( !node )
00107                 {
00108                     OE_DEBUG << LC << "Blacklisting " << uri << std::endl;
00109                     osgEarth::Registry::instance()->blacklist( uri );
00110                     return ReadResult::FILE_NOT_FOUND;
00111                 }
00112 
00113                 return ReadResult( node, ReadResult::FILE_LOADED );
00114             }
00115             else
00116             {
00117                 return ReadResult::FILE_NOT_FOUND;
00118             }
00119         }
00120         else
00121         {
00122             return ReadResult::FILE_NOT_HANDLED;
00123         }
00124     }
00125 };
00126 
00127 REGISTER_OSGPLUGIN(osgearth_engine_osgterrain, OSGTerrainEnginePlugin)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines