OpenGL-结合两个Shader高斯模糊和对比度

时间:2014-12-15 04:45:26

标签: android opengl-es

我尝试将两个着色器组合在一起,通过Android中的新版本OpenGL(2014年12月更新)创建流畅的颜色但不起作用。以下是Shader:

高斯模糊

顶点着色器:

public static final String VERTEX_SHADER =
        "attribute vec4 position;\n" +
                "attribute vec4 inputTextureCoordinate;\n" +
                "\n" +
                "const int GAUSSIAN_SAMPLES = 9;\n" +
                "\n" +
                "uniform float texelWidthOffset;\n" +
                "uniform float texelHeightOffset;\n" +
                "\n" +
                "varying vec2 textureCoordinate;\n" +
                "varying vec2 blurCoordinates[GAUSSIAN_SAMPLES];\n" +
                "\n" +
                "void main()\n" +
                "{\n" +
                "   gl_Position = position;\n" +
                "   textureCoordinate = inputTextureCoordinate.xy;\n" +
                "   \n" +
                "   // Calculate the positions for the blur\n" +
                "   int multiplier = 0;\n" +
                "   vec2 blurStep;\n" +
                "   vec2 singleStepOffset = vec2(texelHeightOffset, texelWidthOffset);\n" +
                "    \n" +
                "   for (int i = 0; i < GAUSSIAN_SAMPLES; i++)\n" +
                "   {\n" +
                "       multiplier = (i - ((GAUSSIAN_SAMPLES - 1) / 2));\n" +
                "       // Blur in x (horizontal)\n" +
                "       blurStep = float(multiplier) * singleStepOffset;\n" +
                "       blurCoordinates[i] = inputTextureCoordinate.xy + blurStep;\n" +
                "   }\n" +
                "}\n";

Fragment Shader:

public static final String FRAGMENT_SHADER =
        "uniform sampler2D inputImageTexture;\n" +
                "\n" +
                "const lowp int GAUSSIAN_SAMPLES = 9;\n" +
                "\n" +
                "varying highp vec2 textureCoordinate;\n" +
                "varying highp vec2 blurCoordinates[GAUSSIAN_SAMPLES];\n" +
                "\n" +
                "void main()\n" +
                "{\n" +
                "   lowp vec3 sum = vec3(0.0);\n" +
                "   lowp vec4 fragColor=texture2D(inputImageTexture,textureCoordinate);\n" +
                "   \n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[0]).rgb * 0.05;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[1]).rgb * 0.09;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[2]).rgb * 0.12;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[3]).rgb * 0.15;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[4]).rgb * 0.18;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[5]).rgb * 0.15;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[6]).rgb * 0.12;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[7]).rgb * 0.09;\n" +
                "    sum += texture2D(inputImageTexture, blurCoordinates[8]).rgb * 0.05;\n" +
                "\n" +
                "   gl_FragColor = vec4(sum,fragColor.a);\n" +
                "}";

对比

NO_Vertext_Shader

和片段着色器:

public static final String CONTRAST_FRAGMENT_SHADER = ""
        + "varying highp vec2 textureCoordinate;\n"
        + " \n"
        + " uniform sampler2D inputImageTexture;\n"
        + " uniform lowp float contrast;\n"
        + " \n"
        + " void main()\n"
        + " {\n"
        + "     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n"
        + "     \n"
        + "     gl_FragColor = vec4(((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColor.w);\n"
        + " }";

我怎么能这样做?,我可以结合一些着色器,但在这种情况下它很难

抱歉我的英语不好。

更新

如果使用两个锐化着色器(使用高斯模糊着色器合并锐化着色器),我该如何合并:

两个锐化着色器

顶点着色器:

public static final String SHARPEN_VERTEX_SHADER = "" +
        "attribute vec4 position;\n" +
        "attribute vec4 inputTextureCoordinate;\n" +
        "\n" +
        "uniform float imageWidthFactor; \n" +
        "uniform float imageHeightFactor; \n" +
        "uniform float sharpness;\n" +
        "\n" +
        "varying vec2 textureCoordinate;\n" +
        "varying vec2 leftTextureCoordinate;\n" +
        "varying vec2 rightTextureCoordinate; \n" +
        "varying vec2 topTextureCoordinate;\n" +
        "varying vec2 bottomTextureCoordinate;\n" +
        "\n" +
        "varying float centerMultiplier;\n" +
        "varying float edgeMultiplier;\n" +
        "\n" +
        "void main()\n" +
        "{\n" +
        "    gl_Position = position;\n" +
        "    \n" +
        "    mediump vec2 widthStep = vec2(imageWidthFactor, 0.0);\n" +
        "    mediump vec2 heightStep = vec2(0.0, imageHeightFactor);\n" +
        "    \n" +
        "    textureCoordinate = inputTextureCoordinate.xy;\n" +
        "    leftTextureCoordinate = inputTextureCoordinate.xy - widthStep;\n" +
        "    rightTextureCoordinate = inputTextureCoordinate.xy + widthStep;\n" +
        "    topTextureCoordinate = inputTextureCoordinate.xy + heightStep;     \n" +
        "    bottomTextureCoordinate = inputTextureCoordinate.xy - heightStep;\n" +
        "    \n" +
        "    centerMultiplier = 1.0 + 4.0 * sharpness;\n" +
        "    edgeMultiplier = sharpness;\n" +
        "}";

片段着色器:

public static final String SHARPEN_FRAGMENT_SHADER = "" +
        "precision highp float;\n" + 
        "\n" + 
        "varying highp vec2 textureCoordinate;\n" + 
        "varying highp vec2 leftTextureCoordinate;\n" + 
        "varying highp vec2 rightTextureCoordinate; \n" + 
        "varying highp vec2 topTextureCoordinate;\n" + 
        "varying highp vec2 bottomTextureCoordinate;\n" + 
        "\n" + 
        "varying highp float centerMultiplier;\n" + 
        "varying highp float edgeMultiplier;\n" + 
        "\n" + 
        "uniform sampler2D inputImageTexture;\n" + 
        "\n" + 
        "void main()\n" + 
        "{\n" + 
        "    mediump vec3 textureColor = texture2D(inputImageTexture, textureCoordinate).rgb;\n" + 
        "    mediump vec3 leftTextureColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb;\n" + 
        "    mediump vec3 rightTextureColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb;\n" + 
        "    mediump vec3 topTextureColor = texture2D(inputImageTexture, topTextureCoordinate).rgb;\n" + 
        "    mediump vec3 bottomTextureColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;\n" + 
        "\n" + 
        "    gl_FragColor = vec4((textureColor * centerMultiplier - (leftTextureColor * edgeMultiplier + rightTextureColor * edgeMultiplier + topTextureColor * edgeMultiplier + bottomTextureColor * edgeMultiplier)), texture2D(inputImageTexture, bottomTextureCoordinate).w);\n" + 
        "}";

1 个答案:

答案 0 :(得分:0)

为什么这种情况很难?在我看来,你需要做的就是在高斯模糊输出上使用对比度。这意味着您需要将高斯模糊片段着色器的最后一行(在主体中)与对比度着色器中的第一行合并为:

lowp vec4 textureColor = vec4(sum,fragColor.a);

在着色器顶部添加uniform lowp float contrast;。除此之外,你将所有这些都保留在此行上方的模糊着色器中,并且全部来自此行下方的对比度着色器:

   uniform lowp float contrast;

   //.. insert Gaussian shader till gl_FragColor..

   lowp vec4 textureColor = vec4(sum,fragColor.a);
   gl_FragColor = vec4(((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColor.w);
}
相关问题