osgEarth 2.1.1
Public Member Functions

osgEarth::ShaderFactory Class Reference

Inheritance diagram for osgEarth::ShaderFactory:
Collaboration diagram for osgEarth::ShaderFactory:

List of all members.

Public Member Functions

virtual osg::Shader * createVertexShaderMain (const ShaderComp::FunctionLocationMap &functions=ShaderComp::FunctionLocationMap()) const
virtual osg::Shader * createFragmentShaderMain (const ShaderComp::FunctionLocationMap &functions=ShaderComp::FunctionLocationMap()) const
virtual osg::Shader * createDefaultTextureVertexShader (int numTexCoordSets) const
virtual osg::Shader * createDefaultTextureFragmentShader (int numTexCoordSets) const
virtual osg::Shader * createDefaultLightingVertexShader () const
virtual osg::Shader * createDefaultLightingFragmentShader () const

Detailed Description

A factory class that generates shader functions for the osgEarth engine. The default ShaderFactory is stored in the osgEarth registry. You can replace it if you want to replace osgEarth's default shader templates.

Definition at line 58 of file ShaderComposition.


Member Function Documentation

osg::Shader * ShaderFactory::createDefaultLightingFragmentShader ( ) const [virtual]

Creates the function that applies lighting coloring in the fragment shader. The name/prototype is: void osgearth_frag_applyLighting( inout vec4 color );

Definition at line 512 of file ShaderComposition.cpp.

{
    static char s_PerVertexLighting_FragmentShaderSource[] =
        "void osgearth_frag_applyLighting( inout vec4 color )                       \n"
        "{                                                                          \n"
        "    float alpha = color.a;                                                 \n"
        "    color = color * gl_Color + gl_SecondaryColor;                          \n"
        "    color.a = alpha;                                                       \n"
        "}                                                                          \n";

    return new osg::Shader( osg::Shader::FRAGMENT, s_PerVertexLighting_FragmentShaderSource );
}

Here is the caller graph for this function:

osg::Shader * ShaderFactory::createDefaultLightingVertexShader ( ) const [virtual]

Creates the function that applies lighting calculations in the vertex shader. The name/prototype is: void osgearth_vert_setupLighting();

Definition at line 482 of file ShaderComposition.cpp.

{
    static char s_PerVertexLighting_VertexShaderSource[] = 
        "void osgearth_vert_setupLighting()                                         \n"
        "{                                                                          \n"
        "    vec3 normal = normalize( gl_NormalMatrix * gl_Normal );                \n"
        "    float NdotL = dot( normal, normalize(gl_LightSource[0].position.xyz) );\n"
        "    NdotL = max( 0.0, NdotL );                                             \n"
        "    float NdotHV = dot( normal, gl_LightSource[0].halfVector.xyz );        \n"
        "    NdotHV = max( 0.0, NdotHV );                                           \n"
        "                                                                           \n"
        "    gl_FrontColor = gl_FrontLightModelProduct.sceneColor +                 \n"
        "                    gl_FrontLightProduct[0].ambient +                      \n"
        "                    gl_FrontLightProduct[0].diffuse * NdotL;               \n"
        "                                                                           \n"
        "    gl_FrontSecondaryColor = vec4(0.0);                                    \n"
        "                                                                           \n"
        "    if ( NdotL * NdotHV > 0.0 )                                            \n"
        "        gl_FrontSecondaryColor = gl_FrontLightProduct[0].specular *        \n"
        "                                 pow( NdotHV, gl_FrontMaterial.shininess );\n"
        "                                                                           \n"
        "    gl_BackColor = gl_FrontColor;                                          \n"
        "    gl_BackSecondaryColor = gl_FrontSecondaryColor;                        \n"
        "}                                                                          \n";

    return new osg::Shader( osg::Shader::VERTEX, s_PerVertexLighting_VertexShaderSource );
}

Here is the caller graph for this function:

osg::Shader * ShaderFactory::createDefaultTextureFragmentShader ( int  numTexCoordSets) const [virtual]

Creates the function that applies texture data in the fragment shader. The name/prototype is: osgearth_frag_applyTexturing( inout vec4 color );

Definition at line 449 of file ShaderComposition.cpp.

{
    std::stringstream buf;

    buf << "#version 120 \n";

    if ( numTexImageUnits > 0 )
    {
        buf << "uniform sampler2D ";
        for( int i=0; i<numTexImageUnits; ++i )
            buf << "tex" << i << (i+1 < numTexImageUnits? "," : "; \n");
    }

    buf << "void osgearth_frag_applyTexturing( inout vec4 color ) \n"
        << "{ \n"
        << "    vec3 color3 = color.rgb; \n"
        << "    vec4 texel; \n";

    for(int i=0; i<numTexImageUnits; ++i )
    {
        buf << "    texel = texture2D(tex" << i << ", gl_TexCoord["<< i <<"].st); \n"
            << "    color3 = mix( color3, texel.rgb, texel.a ); \n";
    }
        
    buf << "    color = vec4(color3,color.a); \n"
        << "} \n";

    std::string str = buf.str();
    return new osg::Shader( osg::Shader::FRAGMENT, str );
}

Here is the caller graph for this function:

osg::Shader * ShaderFactory::createDefaultTextureVertexShader ( int  numTexCoordSets) const [virtual]

Creates the function that sets up default texcoords in the vertex shader. The name/prototype is: void osgearth_vert_setupTexturing();

Definition at line 428 of file ShaderComposition.cpp.

{
    std::stringstream buf;

    buf << "void osgearth_vert_setupTexturing() \n"
        << "{ \n";

    //TODO: gl_TexCoord et.al. are depcrecated so we should replace them ...
    for(int i=0; i<numTexCoordSets; ++i )
    {
        buf << "    gl_TexCoord["<< i <<"] = gl_MultiTexCoord"<< i << "; \n";
    }
        
    buf << "} \n";

    std::string str = buf.str();
    return new osg::Shader( osg::Shader::VERTEX, str );
}

Here is the caller graph for this function:

osg::Shader * ShaderFactory::createFragmentShaderMain ( const ShaderComp::FunctionLocationMap functions = ShaderComp::FunctionLocationMap()) const [virtual]

Creates a fragment shader main().

Definition at line 369 of file ShaderComposition.cpp.

{
    FunctionLocationMap::const_iterator i = functions.find( LOCATION_FRAGMENT_PRE_TEXTURING );
    const OrderedFunctionMap* preTexture = i != functions.end() ? &i->second : 0L;

    FunctionLocationMap::const_iterator j = functions.find( LOCATION_FRAGMENT_PRE_LIGHTING );
    const OrderedFunctionMap* preLighting = j != functions.end() ? &j->second : 0L;

    FunctionLocationMap::const_iterator k = functions.find( LOCATION_FRAGMENT_POST_LIGHTING );
    const OrderedFunctionMap* postLighting = k != functions.end() ? &k->second : 0L;

    std::stringstream buf;
    buf << "void osgearth_frag_applyTexturing( inout vec4 color ); \n"
        << "void osgearth_frag_applyLighting( inout vec4 color ); \n";

    if ( preTexture )
        for( OrderedFunctionMap::const_iterator i = preTexture->begin(); i != preTexture->end(); ++i )
            buf << "void " << i->second << "( inout vec4 color ); \n";

    if ( preLighting )
        for( OrderedFunctionMap::const_iterator i = preLighting->begin(); i != preLighting->end(); ++i )
            buf << "void " << i->second << "( inout vec4 color ); \n";

    if ( postLighting )
        for( OrderedFunctionMap::const_iterator i = postLighting->begin(); i != postLighting->end(); ++i )
            buf << "void " << i->second << "( inout vec4 color ); \n";

    buf << "uniform bool osgearth_LightingEnabled; \n"
        << "void main(void) \n"
        << "{ \n"
        << "    vec4 color = vec4(1,1,1,1); \n";

    if ( preTexture )
        for( OrderedFunctionMap::const_iterator i = preTexture->begin(); i != preTexture->end(); ++i )
            buf << "    " << i->second << "( color ); \n";

    buf << "    osgearth_frag_applyTexturing( color ); \n";

    if ( preLighting )
        for( OrderedFunctionMap::const_iterator i = preLighting->begin(); i != preLighting->end(); ++i )
            buf << "    " << i->second << "( color ); \n";
    
    buf << "    if (osgearth_LightingEnabled) \n"
        << "        osgearth_frag_applyLighting( color ); \n";

    if ( postLighting )
        for( OrderedFunctionMap::const_iterator i = postLighting->begin(); i != postLighting->end(); ++i )
            buf << "    " << i->second << "( color ); \n";

    buf << "    gl_FragColor = color; \n"
        << "} \n";  

    std::string str = buf.str();
    //OE_INFO << str;
    return new osg::Shader( osg::Shader::FRAGMENT, str );
}

Here is the caller graph for this function:

osg::Shader * ShaderFactory::createVertexShaderMain ( const ShaderComp::FunctionLocationMap functions = ShaderComp::FunctionLocationMap()) const [virtual]

Creates a vertex shader main().

Definition at line 301 of file ShaderComposition.cpp.

{
    FunctionLocationMap::const_iterator i = functions.find( LOCATION_VERTEX_PRE_TEXTURING );
    const OrderedFunctionMap* preTexture = i != functions.end() ? &i->second : 0L;

    FunctionLocationMap::const_iterator j = functions.find( LOCATION_VERTEX_PRE_LIGHTING );
    const OrderedFunctionMap* preLighting = j != functions.end() ? &j->second : 0L;

    FunctionLocationMap::const_iterator k = functions.find( LOCATION_VERTEX_POST_LIGHTING );
    const OrderedFunctionMap* postLighting = k != functions.end() ? &k->second : 0L;

    std::stringstream buf;
    buf << "void osgearth_vert_setupTexturing(); \n"
        << "void osgearth_vert_setupLighting(); \n"
        << "uniform bool osgearth_LightingEnabled; \n"
        << "uniform float osgearth_CameraElevation; \n"
        << "varying float osgearth_CameraRange; \n";

    if ( preTexture )
        for( OrderedFunctionMap::const_iterator i = preTexture->begin(); i != preTexture->end(); ++i )
            buf << "void " << i->second << "(); \n";

    if ( preLighting )
        for( OrderedFunctionMap::const_iterator i = preLighting->begin(); i != preLighting->end(); ++i )
            buf << "void " << i->second << "(); \n";

    if ( postLighting )
        for( OrderedFunctionMap::const_iterator i = postLighting->begin(); i != postLighting->end(); ++i )
            buf << "void " << i->second << "(); \n";

    buf << "void main(void) \n"
        << "{ \n"
        << "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"

        << "    vec4 position4 = gl_ModelViewMatrix * gl_Vertex; \n"
        << "    osgearth_CameraRange = length( position4.xyz ); \n"

//        << "    vec3 cameraPos = normalize(vec3( osg_ViewMatrixInverse[3][0], osg_ViewMatrixInverse[3][1], osg_ViewMatrixInverse[3][2] ));

        << "    vec3 position = position4.xyz / position4.w; \n"
        << "    vec3 normal = normalize( gl_NormalMatrix * gl_Normal ); \n";

    if ( preTexture )
        for( OrderedFunctionMap::const_iterator i = preTexture->begin(); i != preTexture->end(); ++i )
            buf << "    " << i->second << "(); \n";

    buf << "    osgearth_vert_setupTexturing(); \n";
    
    if ( preLighting )
        for( OrderedFunctionMap::const_iterator i = preLighting->begin(); i != preLighting->end(); ++i )
            buf << "    " << i->second << "(); \n";

    buf << "    if ( osgearth_LightingEnabled ) \n"
        << "        osgearth_vert_setupLighting(); \n";
    
    if ( postLighting )
        for( OrderedFunctionMap::const_iterator i = postLighting->begin(); i != postLighting->end(); ++i )
            buf << "    " << i->second << "(); \n";

    buf << "} \n";

    std::string str = buf.str();
    //OE_INFO << str << std::endl;
    return new osg::Shader( osg::Shader::VERTEX, str );
}

Here is the caller graph for this function:


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines