查看: 3028|回复: 0

renderMonkey中的高级车漆shader代码

[复制链接]

该用户从未签到

发表于 2016-3-10 18:55:27 来自手机 | 显示全部楼层 |阅读模式
代码编译没有问题,但是是空的场景,初步判定是在回调函数中对顶点着色器的view矩阵没有算对,麻烦高手看下。
#include <osg/Program>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgViewer/Viewer>
#include <osg/TextureCubeMap>

static const char* vertSource = {
        "uniform mat4 view_proj_matrix;\n"
        "uniform mat4 inv_view_matrix;\n"
        "attribute vec4 rm_Vertex;\n"
        "attribute vec2 rm_TexCoord0;\n"
        "attribute vec3 rm_Normal;\n"
        "attribute vec3 rm_Tangent;\n"
        "attribute vec3 rm_Binormal;\n"
        "varying vec2 Tex;\n"
        "varying vec3 Tangent;\n"
        "varying vec3 Binormal;\n"
        "varying vec3 Normal;\n"
        "varying vec3 View;\n"
        "varying vec3 SparkleTex;\n"
        "void main ()\n"
        "{\n"
        "        gl_Position = view_proj_matrix * rm_Vertex;\n"
        "        View = vec3( normalize( (inv_view_matrix * vec4 (0.0, 0.0, 0.0, 1.0)) -  rm_Vertex ) );\n"
        "        Tex = vec2( rm_TexCoord0.x, 1.0 - rm_TexCoord0.y );\n"
        "        Normal   = rm_Normal;\n"
        "        Tangent  = rm_Tangent;\n"
        "        Binormal = rm_Binormal;\n"
        "        SparkleTex = vec3( rm_TexCoord0 * 20.0, 0.0 );\n"
        "}\n"
};

static const char* fragSource = {
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
        "precision highp float;\n"
"#else\n"
        "precision mediump float;\n"
"#endif\n"
        "uniform float microflakePerturbation;\n"
        "uniform float brightnessFactor;\n"
        "uniform float glossLevel;\n"
        "uniform vec4 paintColorMid;\n"
        "uniform vec4 paintColor2;\n"
        "uniform vec4 flakeLayerColor;\n"
        "uniform float normalPerturbation;\n"
        "uniform float microflakePerturbationA;\n"
        "uniform vec4 paintColor0;\n"
        "uniform sampler2D normalMap;\n"
        "uniform sampler2D microflakeNMap;\n"
        "uniform samplerCube showroomMap;\n"
        "varying vec2 Tex;\n"
        "varying vec3 Tangent;\n"
        "varying vec3 Binormal;\n"
        "varying vec3 Normal;\n"
        "varying vec3 View;\n"
        "varying vec3 SparkleTex;\n"
        "void main()\n"
        "{ \n"
        "        vec3 vNormal = texture2D( normalMap, Tex ).rgb;\n"
        "        vNormal = 2.0 * vNormal - 1.0;\n"
        "        vec3 vFlakesNormal = texture2D( microflakeNMap, SparkleTex.xy ).rgb;\n"
        "        vFlakesNormal = 2.0 * vFlakesNormal - 1.0;\n"
        "        vec3 vNp1 = microflakePerturbationA * vFlakesNormal + normalPerturbation * vNormal ;\n"
        "        vec3 vNp2 = microflakePerturbation * ( vFlakesNormal + vNormal ) ;\n"
        "        vec3 vView = normalize( View );\n"
        "        mat3 mTangentToWorld;\n"
        "        mTangentToWorld[0] = Tangent;\n"
        "        mTangentToWorld[1] = Binormal;\n"
        "        mTangentToWorld[2] = Normal;\n"
        "        vec3   vNormalWorld    = normalize( mTangentToWorld * vNormal );\n"
        "        float  fNdotV     = clamp(dot( vNormalWorld, vView), 0.0, 1.0);\n"
        "        vec3   vReflection = 2.0 * vNormalWorld * fNdotV - vView;\n"
        "        float fEnvBias = glossLevel;\n"
        "        vec4 envMap = textureCube( showroomMap, vReflection, fEnvBias );\n"
        "        envMap.rgb = envMap.rgb * envMap.a;\n"
        "        envMap.rgb *= brightnessFactor;\n"
        "        vec3   vNp1World = normalize( mTangentToWorld * vNp1 );\n"
        "        float  fFresnel1 = clamp( dot( vNp1World, vView ), 0.0, 1.0);\n"
        "        vec3   vNp2World = normalize( mTangentToWorld * vNp2 );\n"
        "        float  fFresnel2 = clamp( dot( vNp2World, vView ), 0.0, 1.0);\n"
        "        float  fFresnel1Sq = fFresnel1 * fFresnel1;\n"
        "        vec4 paintColor = fFresnel1   * paintColor0 + fFresnel1Sq * paintColorMid +fFresnel1Sq * fFresnel1Sq * paintColor2 +pow( fFresnel2, 16.0 ) * flakeLayerColor;\n"
        "        float  fEnvContribution = 1.0 - 0.5 * fNdotV;\n"
        "        vec4 finalColor;\n"
        "        finalColor.a = 1.0;\n"
        "        finalColor.rgb = envMap.rgb * fEnvContribution + paintColor.rgb;\n"
        "        gl_FragColor = finalColor;\n"
        "}\n"
};

class viewProjCallback: public osg::Uniform::Callback  
{
public:  
        viewProjCallback(osg::Camera * camera):mCamera(camera){  
        }  
        virtual void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv){  
                osg::Matrix modelView = mCamera->getViewMatrix();  
                osg::Matrix projectM = mCamera->getProjectionMatrix();  
                uniform->set(modelView * projectM);  
        }  
private:  
        osg::Camera * mCamera;  
};

class inverViewCallback: public osg::Uniform::Callback  
{
public:  
        inverViewCallback(osg::Camera * camera):mCamera(camera){  
        }  
        virtual void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv){  
                osg::Matrixd inverview= mCamera->getInverseViewMatrix();   
                uniform->set(inverview);  
        }  
private:  
        osg::Camera * mCamera;  
};


int main( int argc, char** argv )
{
        osgViewer::Viewer viewer;

        osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( "D:/Car.3ds" );
       
    osg::ref_ptr<osg:rogram> program = new osg::Program;
    program->addShader( new osg::Shader(osg::Shader::VERTEX, vertSource) );
    program->addShader( new osg::Shader(osg::Shader::FRAGMENT, fragSource) );
   
    osg::StateSet* stateset = model->getOrCreateStateSet();
    stateset->setAttributeAndModes( program.get() );

    stateset->addUniform( new osg::Uniform("microflakePerturbation",1.0f));
    stateset->addUniform( new osg::Uniform("brightnessFactor",8.0f));
    stateset->addUniform( new osg::Uniform("glossLevel",0.0f));
        stateset->addUniform( new osg::Uniform("paintColorMid", osg::Vec4(0.6f, 0.0f, 0.0f, 1.0f)));
        stateset->addUniform( new osg::Uniform("paintColor2", osg::Vec4(0.0f, 0.35f, -0.35f, 1.0f)) );
        stateset->addUniform( new osg::Uniform("flakeLayerColor",osg::Vec4(0.501961f, 0.5f, 0.0f, 1.0f)));
        stateset->addUniform( new osg::Uniform("normalPerturbation",1.0f));
        stateset->addUniform( new osg::Uniform("microflakePerturbationA",0.1f));
        stateset->addUniform( new osg::Uniform("paintColor0", osg::Vec4(0.4f, 0.0f, 0.35f, 1.0f)));

        osg::ref_ptr<osg::Texture2D> normalMap = new osg::Texture2D;
        osg::ref_ptr<osg::Texture2D> microflakeNMap = new osg::Texture2D;;
        osg::ref_ptr<osg::TextureCubeMap>showroomMap=new osg::TextureCubeMap;

        normalMap->setImage( osgDB::readImageFile("D:/P/Normal.tga"));
        microflakeNMap->setImage( osgDB::readImageFile("D:/P/Noise.tga"));
        showroomMap->setImage( 1,osgDB::readImageFile("D:/P/Showroom.dds"));

        stateset->addUniform( new osg::Uniform("normalMap", 0));
        stateset->addUniform( new osg::Uniform("microflakeNMap", 1) );
        stateset->addUniform( new osg::Uniform("showroomMap", 2) );


        osg::StateAttribute::GLModeValue value = osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE;
        stateset->setTextureAttributeAndModes( 0, normalMap.get(), value );
        stateset->setTextureAttributeAndModes( 1, microflakeNMap.get(), value );
        stateset->setTextureAttributeAndModes( 2, showroomMap.get(), value );

        osg::ref_ptr<osg::Uniform>viewproj = new osg::Uniform( "view_proj_matrix",osg::Matrixd());
        viewproj->setUpdateCallback(new viewProjCallback(viewer.getCamera()));
        stateset->addUniform(viewproj.get());

        osg::ref_ptr<osg::Uniform>inverview = new osg::Uniform( "inv_view_matrix",osg::Matrixd());
        inverview->setUpdateCallback(new inverViewCallback(viewer.getCamera()));
        stateset->addUniform(inverview.get());

    viewer.setSceneData( model.get() );
    return viewer.run();
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表