Aadi
Aadi

Reputation: 77

How to implement both brightness and contrast filter together in OpenGL Android?

I have 2 codes, one for brightness and one for contrast as follows.

brightness

"precision mediump float;" +
            " varying vec2 vTextureCoord;\n" +
            " \n" +
            " uniform lowp sampler2D sTexture;\n" +
            " uniform lowp float brightness;\n" +
            " \n" +
            " void main()\n" +
            " {\n" +
            "     lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
            "     \n" +
            "     gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w);\n" +
            " }";

 @Override
    public void onDraw() {
        GLES20.glUniform1f(getHandle("brightness"), this.brightness);
    }

contrast

"precision mediump float;" +
            " varying vec2 vTextureCoord;\n" +
            " \n" +
            " uniform lowp sampler2D sTexture;\n" +
            " uniform lowp float contrast;\n" +
            " \n" +
            " void main()\n" +
            " {\n" +
            "     lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
            "     \n" +
            "     gl_FragColor = vec4(((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColor.w);\n" +
            " }";

   @Override
    public void onDraw() {
        GLES20.glUniform1f(getHandle("contrast"), this.contrast);
    }

Complete for one filter

public class GlBrightnessFilter extends GlFilter {
    private static final String BRIGHTNESS_FRAGMENT_SHADER = "" +
            "precision mediump float;" +
            " varying vec2 vTextureCoord;\n" +
            " \n" +
            " uniform lowp sampler2D sTexture;\n" +
            " uniform lowp float brightness;\n" +
            " \n" +
            " void main()\n" +
            " {\n" +
            "     lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
            "     \n" +
            "     gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w);\n" +
            " }";

    public GlBrightnessFilter() {
        super(DEFAULT_VERTEX_SHADER, BRIGHTNESS_FRAGMENT_SHADER);
    }

    private float brightness = 0f;

    public void setBrightness(float brightness) {
        this.brightness = brightness;
    }
    public void setContrast(float contrast) {
        this.contrast = contrast;
    }
    @Override
    public void onDraw() {
        GLES20.glUniform1f(getHandle("brightness"), brightness);
    }
}

How I can merge them to change both without clearing other filter. I want both to be changed without changing other filter value.

Basically I want single filter to be used to change both values.

Upvotes: 1

Views: 450

Answers (1)

Hihikomori
Hihikomori

Reputation: 990

"precision mediump float;" +
            " varying vec2 vTextureCoord;\n" +
            " \n" +
            " uniform lowp sampler2D sTexture;\n" +
            " uniform lowp float brightness;\n" +
            " uniform lowp float contrast;\n" +
            " \n" +
            " void main()\n" +
            " {\n" +
            "     lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
            "     \n" +
            "     gl_FragColor = vec4(((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5)) + vec3(brightness), textureColor.w);\n" +
            " }";

Upvotes: 3

Related Questions