osgEarth的Rex引擎原理分析(一二五)着色器源代码加工处理过程

本文详细分析osgEarth的着色器处理过程,包括默认着色器的加载与编译时处理,以及对已加载着色器代码的修改。着重阐述了着色器的加载时和编译时处理,如变量替换、添加,以及osgEarth着色器框架如何整合外部着色器源代码。
摘要由CSDN通过智能技术生成

目标:(一二四)中问题211

1、添加默认着色器

这类着色器源代码是内置在程序里的,不会经过osgEarth着色器框架的整合。

1.1加载时处理

osg/StateSet.cpp

static const char* gl3_VertexShader = {
    "#version " GLSL_VERSION_STR "\n"
    "// gl3_VertexShader\n"
    "#ifdef GL_ES\n"
    "    precision highp float;\n"
    "#endif\n"
    "in vec4 osg_Vertex;\n"
    "in vec4 osg_Color;\n"
    "in vec4 osg_MultiTexCoord0;\n"
    "uniform mat4 osg_ModelViewProjectionMatrix;\n"
    "out vec2 texCoord;\n"
    "out vec4 vertexColor;\n"
    "void main(void)\n"
    "{\n"
    "    gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;\n"
    "    texCoord = osg_MultiTexCoord0.xy;\n"
    "    vertexColor = osg_Color; \n"
    "}\n"
};

static const char* gl3_FragmentShader = {
    "#version " GLSL_VERSION_STR "\n"
    "// gl3_FragmentShader\n"
    "#ifdef GL_ES\n"
    "    precision highp float;\n"
    "#endif\n"
    "uniform sampler2D baseTexture;\n"
    "in vec2 texCoord;\n"
    "in vec4 vertexColor;\n"
    "out vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "    color = vertexColor * texture(baseTexture, texCoord);\n"
    "}\n"
};


static const char* gl2_VertexShader = {
    "// gl2_VertexShader\n"
    "#ifdef GL_ES\n"
    "    precision highp float;\n"
    "#endif\n"
    "varying vec2 texCoord;\n"
    "varying vec4 vertexColor;\n"
    "void main(void)\n"
    "{\n"
    "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    "    texCoord = gl_MultiTexCoord0.xy;\n"
    "    vertexColor = gl_Color; \n"
    "}\n"
};

static const char* gl2_FragmentShader = {
    "// gl2_FragmentShader\n"
    "#ifdef GL_ES\n"
    "    precision highp float;\n"
    "#endif\n"
    "uniform sampler2D baseTexture;\n"
    "varying vec2 texCoord;\n"
    "varying vec4 vertexColor;\n"
    "void main(void)\n"
    "{\n"
    "    gl_FragColor = vertexColor * texture2D(baseTexture, texCoord);\n"
    "}\n"
};


void StateSet::setGlobalDefaults()
{
    _renderingHint = DEFAULT_BIN;

    setRenderBinToInherit();


    setMode(GL_DEPTH_TEST,StateAttribute::ON);
    setAttributeAndModes(new BlendFunc,StateAttribute::OFF);

    #if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)

        // setAttributeAndModes(new AlphaFunc,StateAttribute::OFF);

        Material *material       = new Material;
        material->setColorMode(Material::AMBIENT_AND_DIFFUSE);
        setAttributeAndModes(material,StateAttribute::ON);

    #endif


    OSG_INFO<<"void StateSet::setGlobalDefaults()"<<std::endl;

    osg::DisplaySettings::ShaderHint shaderHint = osg::DisplaySettings::instance()->getShaderHint();
    if (shaderHint==osg::DisplaySettings::SHADER_GL3 || shaderHint==osg::DisplaySettings::SHADER_GLES3)
    {
        OSG_INFO<<"   StateSet::setGlobalDefaults() Setting up GL3 compatible shaders"<<std::endl;

        osg::ref_ptr<osg::Program> program = new osg::Program;
        program->addShader(new osg::Shader(osg::Shader::VERTEX, gl3_VertexShader));
        program->addShader(new osg::Shader(osg::Shader::FRAGMENT, gl3_FragmentShader));
        setAttributeAndModes(program.get());
        setTextureAttribute(0, createDefaultTexture());
        addUniform(new osg::Uniform("baseTexture", 0));
    }
    else if (shaderHint==osg::DisplaySettings::SHADER_GL2 || shaderHint==osg::DisplaySettings::SHADER_GLES2)
    {

        OSG_INFO<<"   StateSet::setGlobalDefaults() Setting up GL2 compatible shaders"<<std::endl;

        osg::ref_ptr<osg::Program> program = new osg::Program;
        program->addShader(new osg::Shader(osg::Shader::VERTEX, gl2_VertexShader));
        program->addShader(new osg::Shader(osg::Shader::FRAGMENT, gl2_FragmentShader));
        setAttributeAndModes(program.get());
        setTextureAttribute(0, createDefaultTexture());
        addUniform(new osg::Uniform("baseTexture", 0));
    }
}

1.2编译时处理

替换一些变量,比如gl_ModelViewMatrix、gl_ProjectionMatrix、gl_ModelViewProjectionMatrix、gl_NormalMatrix,并且会增加定义,比如将gl_ModelViewMatrix替换osg_ModelViewMatrix,并增加uniform mat4 osg_ModelViewMatrix。

osg/State.cpp
bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
{
    OSG_DEBUG<<"State::convertShaderSourceToOsgBuiltIns()"<<std::endl;

    OSG_DEBUG<<"++Before Converted source "<<std::endl<<source<<std::endl<<"++++++++"<<std::endl;


    State_Utils::substitudeEnvVars(*this, source);


    std::string attributeQualifier("attribute ");

    // find the first legal insertion point for replacement declarations. GLSL requires that nothing
    // precede a "#version" compiler directive, so we must insert new declarations after it.
    std::string::size_type declPos = source.rfind( "#version " );
    if ( declPos != std::string::npos )
    {
        declPos = source.find(" ", declPos); // move to the first space after "#version"
        declPos = source.find_first_not_of(std::string(" "), declPos); // skip all the spaces until you reach the version number
        std::string versionNumber(source, declPos, 3);
        int glslVersion = atoi(versionNumber.c_str());
        OSG_INFO<<"shader version found: "<< glslVersion <<std::endl;
        if (glslVersion >= 130) attributeQualifier = "in ";
        // found the string, now find the next linefe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值