查看: 1240|回复: 0

osgearth中着色器图像处理问题

[复制链接]

该用户从未签到

发表于 2015-4-4 12:00:32 | 显示全部楼层 |阅读模式
想用着色器对已经加在球上的影像作图像处理,增强,融合,模糊。
下面是我自己写的模糊卷积的图像算法,是不是2D纹理采样有问题呢,谢谢大家了,求助!!
const char* vertShader = {
        "#version " GLSL_VERSION_STR "\n"
        GLSL_DEFAULT_PRECISION_FLOAT "\n"
        "varying vec2 oe_layer_tex; \n"
        "void main()\n"
        "{\n"
        "vec2 Position;\n"
        "osition.xy = sign(gl_Vertex.xy);\n"
        "gl_Position = vec4(Position.xy, 0.0, 1.0);\n"
        "oe_layer_tex = Position.xy*0.5+0.5;\n"
        "}\n"
};

//片元着色器
static const char* fragShader = {
        "uniform sampler2D Image;\n"
        // "varying vec2 vTexCoord;\n"
        "varying vec2 oe_layer_tex; \n"
        "float sampleDist = 1.0/256.0;\n"
        "void main(inout vec4 color)\n"
        "{\n"
        "        vec2 samples[8];\n"

        "        samples[0] = oe_layer_tex + sampleDist*vec2(-1.0, -1.0);\n"
        "        samples[1] = oe_layer_tex + sampleDist*vec2(0.0, -1.0);\n"
        "        samples[2] = oe_layer_tex + sampleDist*vec2(1.0, -1.0);\n"
        "        samples[3] = oe_layer_tex + sampleDist*vec2(-1.0, 0.0);\n"
        "        samples[4] = oe_layer_tex + sampleDist*vec2(1.0, 0.0);\n"
        "        samples[5] = oe_layer_tex + sampleDist*vec2(-1.0, 1.0);\n"
        "        samples[6] = oe_layer_tex + sampleDist*vec2(0.0, 1.0);\n"
        "        samples[7] = oe_layer_tex + sampleDist*vec2(1.0, 1.0);\n"

        "   vec4 sample = texture2D(Image, oe_layer_tex);\n"
        "        vec4 avg = sample;\n"
        "        avg += texture2D(Image, samples[0]);\n"
        "        avg += texture2D(Image, samples[1]);\n"
        "        avg += texture2D(Image, samples[2]);\n"
        "        avg += texture2D(Image, samples[3]);\n"
        "        avg += texture2D(Image, samples[4]);\n"
        "        avg += texture2D(Image, samples[5]);\n"
        "        avg += texture2D(Image, samples[6]);\n"
        "        avg += texture2D(Image, samples[7]);\n"
        "        avg /= 9.0;\n"
        "        color = avg;"
    "        }\n"
};

int main()
{
        //osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
        osgViewer::Viewer viewer;
        Map* map = new Map();
        GDALOptions gdal;
        gdal.url() = "C:/osgearth/osgearth/data/world_A1.tif";
        ImageLayer* layer2 = new ImageLayer("global", gdal);
        map->addImageLayer(layer2);
        MapNode* node = new MapNode(map);
       
        osg::StateSet* ss = node->getOrCreateStateSet();
        VirtualProgram* vp = VirtualProgram::getOrCreate(ss);
        osg::ref_ptr<osg::Uniform> _xferSampler;
        int _unit = 1;
        osg::ref_ptr<osg::Texture2DArray> _xferTexture;
        _xferTexture = new osg::Texture2DArray();
        _xferTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
        _xferTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
        _xferTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture:INEAR_MIPMAP_LINEAR);
        _xferTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
        _xferTexture->setResizeNonPowerOfTwoHint(false);
        _xferSampler = ss->getOrCreateUniform("Image", osg::Uniform::SAMPLER_2D_ARRAY);
        _xferSampler->set(_unit);
        ss->setTextureAttribute(_unit, _xferTexture.get(), osg::StateAttribute::ON);
        vp->setFunction("main", vertShader, osgEarth::ShaderComp::LOCATION_VERTEX_VIEW);
        vp->setFunction("main", fragShader, osgEarth::ShaderComp::LOCATION_FRAGMENT_LIGHTING);
        viewer.setSceneData(node);
        viewer.setCameraManipulator(new EarthManipulator());
        viewer.run();
        return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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