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 <osgEarth/ModelLayer> 00020 #include <osgEarth/Map> 00021 #include <osg/Depth> 00022 00023 #define LC "[ModelLayer] " 00024 00025 using namespace osgEarth; 00026 //------------------------------------------------------------------------ 00027 00028 ModelLayerOptions::ModelLayerOptions( const ConfigOptions& options ) : 00029 ConfigOptions( options ) 00030 { 00031 setDefaults(); 00032 fromConfig( _conf ); 00033 } 00034 00035 ModelLayerOptions::ModelLayerOptions( const std::string& name, const ModelSourceOptions& driverOptions ) : 00036 ConfigOptions() 00037 { 00038 setDefaults(); 00039 fromConfig( _conf ); 00040 _name = name; 00041 _driver = driverOptions; 00042 } 00043 00044 void 00045 ModelLayerOptions::setDefaults() 00046 { 00047 _overlay.init( false ); 00048 _enabled.init( true ); 00049 _lighting.init( true ); 00050 } 00051 00052 Config 00053 ModelLayerOptions::getConfig() const 00054 { 00055 Config conf = ConfigOptions::getConfig(); 00056 00057 conf.updateIfSet( "name", _name ); 00058 conf.updateIfSet( "overlay", _overlay ); 00059 conf.updateIfSet( "enabled", _enabled ); 00060 conf.updateIfSet( "lighting", _lighting ); 00061 00062 return conf; 00063 } 00064 00065 void 00066 ModelLayerOptions::fromConfig( const Config& conf ) 00067 { 00068 conf.getIfSet( "name", _name ); 00069 conf.getIfSet( "overlay", _overlay ); 00070 conf.getIfSet( "enabled", _enabled ); 00071 conf.getIfSet( "lighting", _lighting ); 00072 } 00073 00074 void 00075 ModelLayerOptions::mergeConfig( const Config& conf ) 00076 { 00077 ConfigOptions::mergeConfig( conf ); 00078 fromConfig( conf ); 00079 } 00080 00081 //------------------------------------------------------------------------ 00082 00083 ModelLayer::ModelLayer( const ModelLayerOptions& options ) : 00084 _initOptions( options ) 00085 { 00086 copyOptions(); 00087 } 00088 00089 ModelLayer::ModelLayer( const std::string& name, const ModelSourceOptions& options ) : 00090 _initOptions( ModelLayerOptions( name, options ) ) 00091 { 00092 copyOptions(); 00093 } 00094 00095 ModelLayer::ModelLayer( const ModelLayerOptions& options, ModelSource* source ) : 00096 _modelSource( source ), 00097 _initOptions( options ) 00098 { 00099 copyOptions(); 00100 } 00101 00102 ModelLayer::ModelLayer(const std::string& name, osg::Node* node): 00103 _initOptions(ModelLayerOptions( name )), 00104 _node(node) 00105 { 00106 copyOptions(); 00107 } 00108 00109 void 00110 ModelLayer::copyOptions() 00111 { 00112 _runtimeOptions = _initOptions; 00113 } 00114 00115 void 00116 ModelLayer::initialize( const std::string& referenceURI, const Map* map ) 00117 { 00118 _referenceURI = referenceURI; 00119 00120 if ( !_modelSource.valid() && _initOptions.driver().isSet() ) 00121 { 00122 _modelSource = ModelSourceFactory::create( *_initOptions.driver() ); 00123 } 00124 00125 if ( _modelSource.valid() ) 00126 { 00127 _modelSource->initialize( _referenceURI, map ); 00128 } 00129 } 00130 00131 osg::Node* 00132 ModelLayer::getOrCreateNode( ProgressCallback* progress ) 00133 { 00134 if ( _modelSource.valid() ) 00135 { 00136 // if the model source has changed, regenerate the node. 00137 if ( _node.valid() && !_modelSource->inSyncWith(_modelSourceRev) ) 00138 { 00139 _node = 0L; 00140 } 00141 00142 if ( !_node.valid() ) 00143 { 00144 _node = _modelSource->createNode( progress ); 00145 00146 if ( _runtimeOptions.enabled().isSet() ) 00147 setEnabled( *_runtimeOptions.enabled() ); 00148 00149 if ( _runtimeOptions.lightingEnabled().isSet() ) 00150 setLightingEnabled( *_runtimeOptions.lightingEnabled() ); 00151 00152 if ( _modelSource->getOptions().depthTestEnabled() == false ) 00153 { 00154 if ( _node ) 00155 { 00156 osg::StateSet* ss = _node->getOrCreateStateSet(); 00157 ss->setAttributeAndModes( new osg::Depth( osg::Depth::ALWAYS ) ); 00158 ss->setRenderBinDetails( 99999, "RenderBin" ); //TODO: configure this bin ... 00159 } 00160 } 00161 00162 _modelSource->sync( _modelSourceRev ); 00163 } 00164 } 00165 00166 return _node.get(); 00167 } 00168 00169 bool 00170 ModelLayer::getEnabled() const 00171 { 00172 return *_runtimeOptions.enabled(); 00173 } 00174 00175 void 00176 ModelLayer::setEnabled(bool enabled) 00177 { 00178 _runtimeOptions.enabled() = enabled; 00179 if ( _node.valid() ) 00180 _node->setNodeMask( enabled ? ~0 : 0 ); 00181 } 00182 00183 void 00184 ModelLayer::setLightingEnabled( bool value ) 00185 { 00186 _runtimeOptions.lightingEnabled() = value; 00187 if ( _node.valid() ) 00188 { 00189 _node->getOrCreateStateSet()->setMode( 00190 GL_LIGHTING, value ? osg::StateAttribute::ON : 00191 (osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED) ); 00192 00193 } 00194 } 00195 00196 bool 00197 ModelLayer::isLightingEnabled() const 00198 { 00199 return *_runtimeOptions.lightingEnabled(); 00200 } 00201 00202 bool 00203 ModelLayer::getOverlay() const 00204 { 00205 return *_runtimeOptions.overlay(); 00206 } 00207 00208 void 00209 ModelLayer::setOverlay(bool overlay) 00210 { 00211 if ( _runtimeOptions.overlay() != overlay ) 00212 { 00213 _runtimeOptions.overlay() = overlay; 00214 fireCallback( &ModelLayerCallback::onOverlayChanged ); 00215 } 00216 } 00217 00218 void 00219 ModelLayer::addCallback( ModelLayerCallback* cb ) 00220 { 00221 _callbacks.push_back( cb ); 00222 } 00223 00224 void 00225 ModelLayer::removeCallback( ModelLayerCallback* cb ) 00226 { 00227 ModelLayerCallbackList::iterator i = std::find( _callbacks.begin(), _callbacks.end(), cb ); 00228 if ( i != _callbacks.end() ) 00229 _callbacks.erase( i ); 00230 } 00231 00232 00233 void 00234 ModelLayer::fireCallback( ModelLayerCallbackMethodPtr method ) 00235 { 00236 for( ModelLayerCallbackList::const_iterator i = _callbacks.begin(); i != _callbacks.end(); ++i ) 00237 { 00238 ModelLayerCallback* cb = i->get(); 00239 (cb->*method)( this ); 00240 } 00241 }