osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthFeatures/MarkerFactory.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 #if 0
00020 #include <osgEarthFeatures/MarkerFactory>
00021 #include <osgEarth/Utils>
00022 #include <osg/TextureRectangle>
00023 #include <osg/Depth>
00024 #include <osg/AutoTransform>
00025 
00026 using namespace osgEarth;
00027 using namespace osgEarth::Features;
00028 using namespace osgEarth::Symbology;
00029 
00030 //----------------------------------------------------------------------------
00031 
00032 namespace
00033 {
00034     osg::Node* buildImageModel(osg::Image* image)
00035     {
00036         float width = image->s();
00037         float height = image->t();
00038 
00039         osg::Geometry* geometry = new osg::Geometry;
00040 
00041         osg::Vec3Array* verts = new osg::Vec3Array(4);
00042         (*verts)[0] = osg::Vec3(-width/2.0f, -height/2.0, 0.0f);
00043         (*verts)[1] = osg::Vec3(width/2.0f, -height/2.0, 0.0f);
00044         (*verts)[2] = osg::Vec3(width/2.0f, height/2.0, 0.0f);
00045         (*verts)[3] = osg::Vec3(-width/2.0f,height/2.0, 0.0f);
00046         geometry->setVertexArray( verts );
00047 
00048         bool flip = image->getOrigin()==osg::Image::TOP_LEFT;
00049 
00050         osg::Vec2Array* texcoords = new osg::Vec2Array(4);
00051         (*texcoords)[0].set(0.0f,flip ? height-1.0f : 0.0f);
00052         (*texcoords)[1].set(width-1.0f,flip ? height-1.0f : 0.0f);
00053         (*texcoords)[2].set(width-1.0f,flip ? 0.0 : height-1.0f);
00054         (*texcoords)[3].set(0.0f,flip ? 0.0 : height-1.0f);
00055         geometry->setTexCoordArray(0, texcoords);
00056 
00057         osg::Vec4Array* colors = new osg::Vec4Array(1);
00058         (*colors)[0].set(1,1,1,1);
00059         geometry->setColorArray( colors );
00060         geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
00061 
00062         geometry->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4));
00063 
00064         osg::StateSet* stateSet = geometry->getOrCreateStateSet();
00065 
00066         osg::TextureRectangle* texture = new osg::TextureRectangle( image );
00067         stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
00068 
00069         stateSet->setMode( GL_BLEND, 1 );
00070         stateSet->setRenderBinDetails( 95, "RenderBin" );
00071         stateSet->setAttributeAndModes( new osg::Depth(osg::Depth::ALWAYS,false), 1 );
00072 
00073         osg::Geode* geode = new osg::Geode;
00074         geode->addDrawable( geometry );
00075 
00076         //PixelAutoTransform* at = new PixelAutoTransform;
00077         //at->setMinPixelWidth( width );
00078         osg::AutoTransform* at = new osg::AutoTransform;
00079         at->setAutoScaleToScreen( true );
00080         at->setAutoRotateMode( osg::AutoTransform::ROTATE_TO_SCREEN );
00081         at->addChild( geode );
00082         return at;
00083     }
00084 }
00085 
00086 //----------------------------------------------------------------------------
00087 
00088 MarkerFactory::MarkerFactory( Session* session ) :
00089 _session( session )
00090 {
00091     //nop
00092 }
00093 
00094 osg::Node*
00095 MarkerFactory::getOrCreateNode( const Feature* feature, const MarkerSymbol* symbol, bool useCache )
00096 {
00097     osg::Node* result =0L;
00098 
00099     if ( symbol )
00100     {
00101         if ( symbol->getNode() )
00102         {
00103             result = symbol->getNode();
00104         }
00105         else if ( symbol->getImage() )
00106         {
00107             const std::string& fileName = symbol->getImage()->getFileName();
00108             if ( !fileName.empty() && _session.valid() && useCache )
00109             {
00110                 result = _session->getResource<osg::Node>( fileName );
00111             }
00112 
00113             if ( !result )
00114             {
00115                 result = buildImageModel( symbol->getImage() );
00116             }
00117 
00118             if ( result && _session.valid() && useCache && !fileName.empty() )
00119             {
00120                 _session->putResource(fileName, result);
00121             }
00122         }
00123         else if ( symbol->url().isSet() && !symbol->url()->empty() )
00124         {            
00125             StringExpression expr = symbol->url().get();
00126             std::string val = feature->eval( expr  );//symbol->url()->full();
00127             URI uri( val, expr.uriContext() );
00128 
00129             OE_DEBUG << "Using model URL " << uri.full() << std::endl;
00130             if ( _session.valid() && useCache )
00131             {
00132                 result = _session->getResource<osg::Node>( uri.full() );
00133             }
00134 
00135             if ( !result )
00136             {                
00137                 result = createFromURI( uri );
00138             }
00139 
00140             if ( result && _session.valid() && useCache )
00141             {
00142                 _session->putResource( uri.full(), result );
00143             }
00144         }
00145     }
00146 
00147     return result;
00148 }
00149 
00150 osg::Image*
00151 MarkerFactory::getOrCreateImage( const MarkerSymbol* symbol, bool useCache )
00152 {
00153     if ( symbol->getImage() )
00154     {
00155         return symbol->getImage();
00156     }
00157     else if ( symbol->url().isSet() && !symbol->url()->empty() )
00158     {
00159         return createImageFromURI( symbol->url()->expr() );
00160     }
00161     return 0L;
00162 }
00163 
00164 osg::Node*
00165 MarkerFactory::createFromURI( const URI& uri ) const
00166 {
00167     osg::ref_ptr<osg::Object> obj = uri.readObject();
00168     if ( obj.valid() )
00169     {
00170         if ( dynamic_cast<osg::Image*>( obj.get() ) )
00171         {
00172             return buildImageModel( dynamic_cast<osg::Image*>( obj.get() ) );
00173         }
00174         else if ( dynamic_cast<osg::Node*>( obj.get() ) )
00175         {
00176             return dynamic_cast<osg::Node*>( obj.release() );
00177         }
00178     }
00179 
00180     else // failing that, fall back on the old encoding format..
00181     {
00182         StringVector tok;
00183         StringTokenizer( *uri, tok, "()" );
00184         if (tok.size() >= 2)
00185             return createFromURI( URI(tok[1]) );
00186     }
00187 
00188     // fail
00189     return 0L;
00190 }
00191 
00192 
00193 osg::Image*
00194 MarkerFactory::createImageFromURI( const URI& uri ) const
00195 {
00196     StringVector tok;
00197     StringTokenizer( *uri, tok, "()" );
00198 
00199     if ( tok.size() > 0 )
00200     {
00201         URI imageURI( tok[tok.size()-1], uri.context() );
00202         return imageURI.readImage();
00203     }
00204 
00205     return 0L;
00206 }
00207 
00208 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines