osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/TextureCompositor

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 #ifndef OSGEARTH_TEXTURE_COMPOSITOR_H
00020 #define OSGEARTH_TEXTURE_COMPOSITOR_H 1
00021 
00022 #include <osgEarth/Common>
00023 #include <osgEarth/GeoData>
00024 #include <osgEarth/Map>
00025 #include <osgEarth/ThreadingUtils>
00026 #include <osgEarth/TerrainOptions>
00027 #include <osg/StateSet>
00028 #include <osg/Program>
00029 
00030 namespace osgEarth
00031 {
00045     class OSGEARTH_EXPORT TextureLayout
00046     {        
00047     public:
00048         // vector of texture slots, each slot containing the UID of the image layer in that slot
00049         // or -1 if the slot is empty.
00050         typedef std::vector<UID> TextureSlotVector;
00051 
00052         // rendering order of texture slots- each element is an index into the TextureSlotVector.
00053         typedef std::vector<int> RenderOrderVector;
00054 
00055     public:
00056         TextureLayout();
00057 
00063         int getSlot( UID layerUID, unsigned which =0, unsigned maxSlotsToSearch =~0 ) const;
00064 
00066         int getOrder( UID layerUID ) const;
00067 
00069         int getMaxUsedSlot() const;
00070 
00072         bool isSlotAvailable( int slot ) const;
00073 
00075         const TextureSlotVector& getTextureSlots() const { return _slots; }
00076 
00078         const RenderOrderVector& getRenderOrder() const { return _order; }
00079 
00081         bool containsSecondarySlots( unsigned maxSlotsToSearch = ~0) const;
00082 
00084         bool isBlendingEnabled( UID layerUID ) const;
00085 
00086     protected:
00087         friend class TextureCompositor;
00088 
00089         void applyMapModelChange( const MapModelChange& change, bool reserveSeconarySlotIfNecessary );
00090 
00091         void setReservedSlots( const std::set<int>& reservedSlots );
00092 
00093     protected:
00094         TextureSlotVector  _slots;
00095         RenderOrderVector  _order;
00096         bool               _textureImageUnitPerSlot;
00097         std::set<int>      _reservedSlots;
00098         std::map<UID,bool> _lodBlending;
00099 
00100         void assignPrimarySlot( ImageLayer* layer, int index );
00101         void assignSecondarySlot( ImageLayer* layer );
00102     };
00103 
00104     //-----------------------------------------------------------------------
00105 
00111     class OSGEARTH_EXPORT TextureCompositorTechnique : public osg::Referenced
00112     {
00113     public:
00114         virtual bool requiresUnitTextureSpace() const =0;
00115 
00116         virtual bool usesShaderComposition() const =0;
00117 
00118         virtual bool blendingRequiresSecondarySlot() const { return false; }
00119 
00120         virtual bool supportsLayerUpdate() const { return false; }
00121 
00122         virtual void updateMasterStateSet( osg::StateSet* stateSet, const TextureLayout& layout ) const { }
00123 
00124         virtual GeoImage prepareImage( const GeoImage& image, const GeoExtent& tileExtent ) const { return image; }
00125 
00126         virtual GeoImage prepareSecondaryImage( const GeoImage& image, const GeoExtent& tileExtent ) const { return image; }
00127 
00128         virtual void applyLayerUpdate( osg::StateSet* stateSet, UID layerUID, const GeoImage& preparedImage, const TileKey& tileKey, const TextureLayout& layout, osg::StateSet* parentStateSet ) const { }
00129 
00130         virtual void applyLayerRemoval( osg::StateSet* stateSet, UID layerUID ) const { }
00131 
00132         virtual osg::Shader* createSamplerFunction( 
00133             UID layerUID, 
00134             const std::string& functionName, 
00135             osg::Shader::Type type, 
00136             const TextureLayout& layout ) const { return 0L; }
00137     };
00138 
00139     //-----------------------------------------------------------------------
00140 
00144     class OSGEARTH_EXPORT TextureCompositor : public osg::Referenced
00145     {
00146     public:
00150         TextureCompositor( const TerrainOptions& options );
00151 
00157         const TerrainOptions::CompositingTechnique& getTechnique() const { return _tech; }
00158 
00163         void applyMapModelChange( const MapModelChange& change );
00164 
00169         bool supportsLayerUpdate() const;
00170 
00179         GeoImage prepareImage( const GeoImage& image, const GeoExtent& tileExtent ) const;
00180 
00184         GeoImage prepareSecondaryImage( const GeoImage& image, const GeoExtent& tileExtent ) const;
00185 
00191         void applyLayerUpdate(
00192             osg::StateSet*  stateSet,
00193             UID             layerUID,
00194             const GeoImage& preparedImage,
00195             const TileKey&  tileKey,
00196             osg::StateSet*  parentStateSet ) const;
00197 
00202         void applyLayerRemoval( osg::StateSet* stateSet, UID layerUID ) const;
00203 
00208         void assignTexCoordArray(
00209             osg::Geometry*  geom,
00210             UID             layerUID,
00211             osg::Vec2Array* texCoords ) const;
00212 
00219         bool requiresUnitTextureSpace() const;
00220 
00224         bool usesShaderComposition() const;
00225 
00231         void updateMasterStateSet( osg::StateSet* stateSet ) const;
00232 
00237         bool reserveTextureImageUnit( int& out_unit );
00238 
00242         void releaseTextureImageUnit( int unit );
00243 
00247         int getRenderOrder( UID layerUID ) const;
00248 
00256         osg::Shader* createSamplerFunction( UID layerUID, const std::string& functionName, osg::Shader::Type type ) const;
00257 
00261         TerrainOptions& getOptions() { return _options;}
00262 
00263     private:
00264         void init();
00265 
00266         OpenThreads::Mutex                       _initMutex;
00267         TerrainOptions::CompositingTechnique     _tech;
00268         TerrainOptions                           _options;
00269         bool                                     _forceTech;
00270         osg::ref_ptr<osg::Program>               _program;
00271         osg::ref_ptr<TextureCompositorTechnique> _impl;
00272 
00273         TextureLayout _layout;
00274         Threading::ReadWriteMutex _layoutMutex;
00275 
00276         std::set<int> _reservedUnits;
00277     };
00278 }
00279 
00280 #endif // OSGEARTH_TEXTURE_COMPOSITOR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines