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 <osgEarthSymbology/Skins> 00020 #include <osgEarth/StringUtils> 00021 #include <osgEarth/ImageUtils> 00022 #include <osgEarth/HTTPClient> 00023 00024 #include <osg/BlendFunc> 00025 #include <osg/Texture2D> 00026 00027 using namespace osgEarth; 00028 using namespace osgEarth::Symbology; 00029 00030 //--------------------------------------------------------------------------- 00031 00032 SkinResource::SkinResource( const Config& conf ) : 00033 Resource ( conf ), 00034 _imageWidth ( 10.0f ), 00035 _imageHeight ( 3.0f ), 00036 _minObjHeight ( 0.0f ), 00037 _maxObjHeight ( FLT_MAX ), 00038 _isTiled( false ), 00039 _texEnvMode ( osg::TexEnv::MODULATE ), 00040 _maxTexSpan ( 1024 ) 00041 { 00042 mergeConfig( conf ); 00043 } 00044 00045 void 00046 SkinResource::mergeConfig( const Config& conf ) 00047 { 00048 conf.getIfSet( "url", _imageURI ); 00049 conf.getIfSet( "image_width", _imageWidth ); 00050 conf.getIfSet( "image_height", _imageHeight ); 00051 conf.getIfSet( "min_object_height", _minObjHeight ); 00052 conf.getIfSet( "max_object_height", _maxObjHeight ); 00053 conf.getIfSet( "tiled", _isTiled ); 00054 conf.getIfSet( "max_texture_span", _maxTexSpan ); 00055 00056 conf.getIfSet( "texture_mode", "decal", _texEnvMode, osg::TexEnv::DECAL ); 00057 conf.getIfSet( "texture_mode", "modulate", _texEnvMode, osg::TexEnv::MODULATE ); 00058 conf.getIfSet( "texture_mode", "replace", _texEnvMode, osg::TexEnv::REPLACE ); 00059 conf.getIfSet( "texture_mode", "blend", _texEnvMode, osg::TexEnv::BLEND ); 00060 } 00061 00062 Config 00063 SkinResource::getConfig() const 00064 { 00065 Config conf = Resource::getConfig(); 00066 conf.key() = "skin"; 00067 00068 conf.updateIfSet( "url", _imageURI ); 00069 conf.updateIfSet( "image_width", _imageWidth ); 00070 conf.updateIfSet( "image_height", _imageHeight ); 00071 conf.updateIfSet( "min_object_height", _minObjHeight ); 00072 conf.updateIfSet( "max_object_height", _maxObjHeight ); 00073 conf.updateIfSet( "tiled", _isTiled ); 00074 conf.updateIfSet( "max_texture_span", _maxTexSpan ); 00075 00076 conf.updateIfSet( "texture_mode", "decal", _texEnvMode, osg::TexEnv::DECAL ); 00077 conf.updateIfSet( "texture_mode", "modulate", _texEnvMode, osg::TexEnv::MODULATE ); 00078 conf.updateIfSet( "texture_mode", "replace", _texEnvMode, osg::TexEnv::REPLACE ); 00079 conf.updateIfSet( "texture_mode", "blend", _texEnvMode, osg::TexEnv::BLEND ); 00080 00081 return conf; 00082 } 00083 00084 osg::StateSet* 00085 SkinResource::createStateSet() const 00086 { 00087 return createStateSet( createImage() ); 00088 } 00089 00090 osg::StateSet* 00091 SkinResource::createStateSet( osg::Image* image ) const 00092 { 00093 osg::StateSet* stateSet = 0L; 00094 if ( image ) 00095 { 00096 stateSet = new osg::StateSet(); 00097 00098 osg::Texture* tex = new osg::Texture2D( image ); 00099 tex->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT ); 00100 tex->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT ); 00101 stateSet->setTextureAttributeAndModes( 0, tex, osg::StateAttribute::ON ); 00102 00103 if ( _texEnvMode.isSet() ) 00104 { 00105 osg::TexEnv* texenv = new osg::TexEnv(); 00106 texenv = new osg::TexEnv(); 00107 texenv->setMode( *_texEnvMode ); 00108 stateSet->setTextureAttribute( 0, texenv, osg::StateAttribute::ON ); 00109 } 00110 00111 if ( ImageUtils::hasAlphaChannel( image ) ) 00112 { 00113 osg::BlendFunc* blendFunc = new osg::BlendFunc(); 00114 blendFunc->setFunction( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); 00115 stateSet->setAttributeAndModes( blendFunc, osg::StateAttribute::ON ); 00116 stateSet->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); 00117 } 00118 } 00119 00120 return stateSet; 00121 } 00122 00123 osg::Image* 00124 SkinResource::createImage() const 00125 { 00126 osg::ref_ptr<osg::Image> image; 00127 if ( HTTPClient::readImageFile( _imageURI->full(), image ) != HTTPClient::RESULT_OK ) 00128 { 00129 //TODO: hmm, perhaps create an "error image" here? or just return NULL 00130 // and let the caller do so. 00131 } 00132 return image.release(); 00133 } 00134 00135 //--------------------------------------------------------------------------- 00136 00137 SkinSymbol::SkinSymbol( const Config& conf ) : 00138 _objHeight ( 0.0f ), 00139 _minObjHeight ( 0.0f ), 00140 _maxObjHeight ( FLT_MAX ), 00141 _isTiled ( false ), 00142 _randomSeed ( 0 ) 00143 { 00144 if ( !conf.empty() ) 00145 mergeConfig( conf ); 00146 } 00147 00148 void 00149 SkinSymbol::mergeConfig( const Config& conf ) 00150 { 00151 conf.getIfSet( "library", _libraryName ); 00152 conf.getIfSet( "object_height", _objHeight ); 00153 conf.getIfSet( "min_object_height", _minObjHeight ); 00154 conf.getIfSet( "max_object_height", _maxObjHeight ); 00155 conf.getIfSet( "tiled", _isTiled ); 00156 conf.getIfSet( "random_seed", _randomSeed ); 00157 00158 addTags( conf.value("tags" ) ); 00159 } 00160 00161 Config 00162 SkinSymbol::getConfig() const 00163 { 00164 Config conf = Symbol::getConfig(); 00165 conf.key() = "skin"; 00166 00167 conf.addIfSet( "library", _libraryName ); 00168 conf.addIfSet( "object_height", _objHeight ); 00169 conf.addIfSet( "min_object_height", _minObjHeight ); 00170 conf.addIfSet( "max_object_height", _maxObjHeight ); 00171 conf.addIfSet( "tiled", _isTiled ); 00172 conf.addIfSet( "random_seed", _randomSeed ); 00173 00174 std::string tagstring = this->tagString(); 00175 if ( !tagstring.empty() ) 00176 conf.attr("tags") = tagstring; 00177 00178 return conf; 00179 }