如何在 OpenGL Android 中同时实现亮度和对比度滤镜?
How to implement both brightness and contrast filter together in OpenGL Android?
我有2个代码,一个用于亮度,一个用于对比度如下。
亮度
"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);
}
对比度
"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);
}
完成一个过滤器
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);
}
}
如何在不清除其他过滤器的情况下合并它们以更改两者。我希望在不更改其他过滤器值的情况下更改两者。
基本上我希望使用单个过滤器来更改两个值。
"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" +
" }";
我有2个代码,一个用于亮度,一个用于对比度如下。
亮度
"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);
}
对比度
"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);
}
完成一个过滤器
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);
}
}
如何在不清除其他过滤器的情况下合并它们以更改两者。我希望在不更改其他过滤器值的情况下更改两者。
基本上我希望使用单个过滤器来更改两个值。
"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" +
" }";