结合两个着色器程序

时间:2014-06-12 16:13:00

标签: android opengl-es shader andengine fragment-shader

对于我的Android应用程序,我想在同一图像上应用亮度和对比度着色器。

目前我正在使用gpuimage插件。在那里,我发现了两个单独的亮度和对比度程序,如下所示。

 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 BRIGHTNESS_FRAGMENT_SHADER = ""
        + "varying highp vec2 textureCoordinate;\n"
        + " \n"
        + " uniform sampler2D inputImageTexture;\n"
        + " uniform lowp float brightness;\n"
        + " \n"
        + " void main()\n"
        + " {\n"
        + "     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n"
        + "     \n"
        + "     gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w);\n"
        + " }";

现在应用我在代码

之后编写的两种效果
public static final String CONTRAST_BRIGHTNESS_FRAGMENT_SHADER = ""
        + "varying highp vec2 textureCoordinate;\n"
        + " uniform sampler2D inputImageTexture;\n"
        + "varying highp vec2 textureCoordinate2;\n"
        + " uniform sampler2D inputImageTexture2;\n"
        + " uniform lowp float contrast;\n"
        + " uniform lowp float brightness;\n"
        + " \n"
        + " void main()\n"
        + " {\n"
        + "     lowp vec4 textureColorForContrast = texture2D(inputImageTexture, textureCoordinate);\n"
        + "     \n"
        + "     lowp vec4 contastVec4 = vec4(((textureColorForContrast.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColorForContrast.w);\n"
        + "     lowp vec4 textureColorForBrightness = texture2D(inputImageTexture2, textureCoordinate2);\n"
        + "     \n"
        + "     lowp vec4 brightnessVec4 = vec4((textureColorForBrightness.rgb + vec3(brightness)), textureColorForBrightness.w);\n"
        + "     gl_FragColor = contastVec4 + brightnessVec4;\n" + " }";

无法获得欲望结果。我无法弄清楚接下来要做什么? 我必须写什么程序?

1 个答案:

答案 0 :(得分:3)

为什么要在最后添加2个结果?你需要做的是获取一个纹素并对其进行2次数学运算。试试这个:

lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
lowp vec3 contrastedColor = ((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5));
gl_FragColor = vec4((contrastedColor + vec3(brightness)), textureColor.w);

然后使用2个单独的着色器可以产生不同的结果,所以让我们分析一下发生了什么:

  • 抓取纹理颜色
  • 应用对比方法
  • 将颜色写入缓冲区(此时颜色值被限制在[.0,1.0]范围内)
  • 抓取纹理颜色
  • 应用亮度
  • 将颜色写入缓冲区

所以你要添加的是将对比色contrastedColor夹到[0,1]。至于M. Coleman所说的确是如此,你需要考虑结果不同的可能性,因为在你的特定情况下获取多个纹素但是不能产生不同的结果而且你都很好。至于混合,假设被禁用或(GL_ONE, GL_ZERO)这是相同的事情(其他一些混合类型可能也有效)。