osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/ShaderComposition

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_SHADER_COMPOSITION_H
00020 #define OSGEARTH_SHADER_COMPOSITION_H 1
00021 
00022 #include <osgEarth/Common>
00023 #include <osgEarth/Revisioning>
00024 #include <osgEarth/ThreadingUtils>
00025 #include <string>
00026 #include <map>
00027 #include <osg/Shader>
00028 #include <osg/Program>
00029 #include <osg/StateAttribute>
00030 
00031 namespace osgEarth
00032 {        
00033     namespace ShaderComp
00034     {
00035         // User function injection points.
00036         enum FunctionLocation
00037         {
00038             LOCATION_VERTEX_PRE_TEXTURING   =0,
00039             LOCATION_VERTEX_PRE_LIGHTING    =1,
00040             LOCATION_VERTEX_POST_LIGHTING   =2,
00041             LOCATION_FRAGMENT_PRE_TEXTURING =3,
00042             LOCATION_FRAGMENT_PRE_LIGHTING  =4,
00043             LOCATION_FRAGMENT_POST_LIGHTING =5
00044         };
00045 
00046         // set of user functions, ordered by priority.
00047         typedef std::multimap<float, std::string> OrderedFunctionMap; // duplicate keys allowed
00048 
00049         // user function sets, categorized by function location.
00050         typedef std::map<FunctionLocation, OrderedFunctionMap> FunctionLocationMap;
00051     }
00052 
00058     class OSGEARTH_EXPORT ShaderFactory : public osg::Referenced
00059     {
00060     public:
00062         virtual osg::Shader* createVertexShaderMain(
00063             const ShaderComp::FunctionLocationMap& functions =ShaderComp::FunctionLocationMap() ) const;
00064 
00066         virtual osg::Shader* createFragmentShaderMain( 
00067             const ShaderComp::FunctionLocationMap& functions =ShaderComp::FunctionLocationMap() ) const;
00068         
00074         virtual osg::Shader* createDefaultTextureVertexShader( int numTexCoordSets ) const;
00075 
00081         virtual osg::Shader* createDefaultTextureFragmentShader( int numTexCoordSets ) const;
00082 
00088         virtual osg::Shader* createDefaultLightingVertexShader() const;
00089 
00095         virtual osg::Shader* createDefaultLightingFragmentShader() const;
00096     };
00097 
00105     class OSGEARTH_EXPORT VirtualProgram : public osg::Program
00106     {
00107     public:
00118         void setFunction( 
00119             const std::string& name,
00120             const std::string& source, 
00121             ShaderComp::FunctionLocation loc,
00122             float priority =1.0f );
00123 
00124     public: 
00125         VirtualProgram( unsigned int mask = 0xFFFFFFFFUL );
00126 
00127         VirtualProgram( const VirtualProgram& VirtualProgram, 
00128                         const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY );
00129 
00130         META_StateAttribute( osgEarth, VirtualProgram, Type( PROGRAM ) )
00131 
00132         
00133         virtual int compare(const StateAttribute& sa) const
00134         {
00135            // check the types are equal and then create the rhs variable
00136            // used by the COMPARE_StateAttribute_Parameter macros below.
00137            COMPARE_StateAttribute_Types(VirtualProgram,sa)
00138 
00139            // compare each parameter in turn against the rhs.
00140            COMPARE_StateAttribute_Parameter(_mask)
00141            COMPARE_StateAttribute_Parameter(_shaderMap)
00142            return 0; // passed all the above comparison macros, must be equal.
00143         }
00144 
00147         virtual void apply(osg::State& state) const;
00148 
00149         osg::Shader* getShader( const std::string& shaderSemantic, osg::Shader::Type type );   
00150 
00151         osg::Shader* setShader( const std::string& shaderSemantic, osg::Shader* shader );
00152 
00153         void removeShader( const std::string& shaderSemantic, osg::Shader::Type type );
00154 
00155 
00156     protected:
00157         typedef std::vector< osg::ref_ptr< osg::Shader > >            ShaderList;
00158         typedef std::pair< std::string, osg::Shader::Type >           ShaderSemantic;
00159         typedef std::map< ShaderSemantic, osg::ref_ptr<osg::Shader> > ShaderMap;
00160         typedef std::map< ShaderList, osg::ref_ptr<osg::Program> >    ProgramMap;
00161 
00162         mutable ProgramMap                   _programMap;
00163         ShaderMap                            _shaderMap;
00164         unsigned int                         _mask;
00165 
00166         ShaderComp::FunctionLocationMap _functions;
00167         ShaderComp::FunctionLocationMap _accumulatedFunctions;
00168 
00169         Threading::Mutex _functionsMutex;
00170 
00171         bool hasLocalFunctions() const;
00172         void refreshAccumulatedFunctions( const osg::State& state );
00173 
00174     public:
00175         void getFunctions( ShaderComp::FunctionLocationMap& out ) const;
00176     };
00177 
00178 } // namespace osgEarth
00179 
00180 #endif // OSGEARTH_SHADER_COMPOSITION_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines