|
想通过着色器对已经加在球上的影像做边缘检测,融合,模糊等图像处理,不知道用怎样的机制实现。
目前想用模糊卷积算法做一个简单的处理,可是总是有问题,有哪位大神知道,谢谢啦!
以下是我自己尝试的代码,是不是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; |
|