Android 问题:当此相机上显示图像并包含辉光效果时,图像将变为黑色

Android 问题:当此相机上显示图像并包含辉光效果时,图像将变为黑色,android,opengl-es,opengl-es-2.0,fragment-shader,vertex-shader,Android,Opengl Es,Opengl Es 2.0,Fragment Shader,Vertex Shader,解决了。请看下面我的答案 --------------------------- 编辑3: 几乎解决了。最后一推。 我试过拉比76建议的比例和调制流,蝴蝶是透明的。请看椅子上的蝴蝶和地板上的大蝴蝶 以下是原始图像(其中一幅): --------------------------- 编辑2: 多亏了拉比76,它几乎解决了! 左图显示了Rabbi76解决方案的当前结果。右图显示了预期的解决方案。如你所见,蝴蝶现在变得更亮\透明了 编辑1: 我尝试添加建议的glBlendFunc,但没有帮助

解决了。请看下面我的答案

---------------------------
编辑3:

几乎解决了。最后一推。 我试过拉比76建议的比例和调制流,蝴蝶是透明的。请看椅子上的蝴蝶和地板上的大蝴蝶

以下是原始图像(其中一幅):

---------------------------

编辑2:

多亏了拉比76,它几乎解决了! 左图显示了Rabbi76解决方案的当前结果。右图显示了预期的解决方案。如你所见,蝴蝶现在变得更亮\透明了

编辑1:

我尝试添加建议的glBlendFunc,但没有帮助,尽管它稍微影响了辉光效果。我需要添加GLES20.glEnable(GLES20.GL_BLEND),因为默认情况下它是禁用的

---------------------------
我正在使用这个存储库

它包含可以在相机上显示的过滤器。其中一个过滤器名为“水印”。当我将此图像更改为具有辉光效果的图像时,辉光将变为黑色

图像位于:GPUVideo android master\sample\src\main\res\drawable nodpi\sample\u bitmap.png。为了触发该问题,应将其更改为具有辉光效果的图像。例如,我生成了以下图像:

您可以看到白光效果变为黑色的问题:

只需运行应用程序,点击“相机记录”,然后选择“肖像”,并从左侧列表中选择“水印”(从末尾算起的第5个)。别忘了改变我提到的形象

相关类的名称为:GlWatermarkFilter。它扩展了包含下一个着色器的GlOverlayFilter:

 private final static String FRAGMENT_SHADER =
        "precision mediump float;\n" +
                "varying vec2 vTextureCoord;\n" +
                "uniform lowp sampler2D sTexture;\n" +
                "uniform lowp sampler2D oTexture;\n" +
                "void main() {\n" +
                "   lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
                "   lowp vec4 textureColor2 = texture2D(oTexture, vTextureCoord);\n" +
                "   \n" +
                "   gl_FragColor = mix(textureColor, textureColor2, textureColor2.a);\n" +
                "}\n";
此类还包含以下设置:

GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
此类扩展了包含以下内容的GlFilter:

protected static final String DEFAULT_VERTEX_SHADER =
        "attribute highp vec4 aPosition;\n" +
                "attribute highp vec4 aTextureCoord;\n" +
                "varying highp vec2 vTextureCoord;\n" +
                "void main() {\n" +
                "gl_Position = aPosition;\n" +
                "vTextureCoord = aTextureCoord.xy;\n" +
                "}\n";

protected static final String DEFAULT_FRAGMENT_SHADER =
        "precision mediump float;\n" +
                "varying highp vec2 vTextureCoord;\n" +
                "uniform lowp sampler2D sTexture;\n" +
                "void main() {\n" +
                "gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
                "}\n";

请帮我弄清楚如何修理它。我知道这是关于半阿尔法的问题,而这个相机不知道如何显示它。我的目标是录制一个有发光蝴蝶的视频。

最有可能的是,发光效果纹理的颜色通道在发光区域的边缘淡出。这意味着发光区域边缘的纹理颜色通道具有从白色到黑色的渐变。如果是这种情况,则错误地混合纹理。您需要将辉光效果(textureColor 2)添加到基础纹理(
textureColor
):

gl_FragColor=textureColor+textureColor 2;
一个选项是通过其alpha通道缩放辉光效果纹理

gl_FragColor=textureColor+textureColor2*textureColor2.a;
如果辉光效果看起来太亮,可以通过缩放辉光纹理来减少辉光效果:

float scale=0.5;
gl_FragColor=纹理颜色+纹理颜色2*比例;
或者,您可以通过颜色纹理调制辉光效果:

vec4 modulateGlow=textureColor2*textureColor;
gl_FragColor=纹理颜色+调制流;

这解决了所有问题,因此请注意最后两行:

private final static String FRAGMENT_SHADER =
        "precision mediump float;\n" +
                "varying vec2 vTextureCoord;\n" +
                "uniform lowp sampler2D sTexture;\n" +
                "uniform lowp sampler2D oTexture;\n" +
                "void main() {\n" +
                "   lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
                "   lowp vec4 textureColor2 = texture2D(oTexture, vTextureCoord);\n" +
                "   lowp float alphaDivisor = textureColor2.a + step(textureColor2.a, 0.0);\n" +
                "   gl_FragColor = vec4(mix(textureColor.rgb, textureColor2.rgb / alphaDivisor, textureColor2.a), textureColor.a);\n" +
                "}\n";

我猜这和混合模式有关。搜索
glBlendFunc
并可能尝试添加
GLES20.glBlendFunc(GLES20.GL_-ONE,GLES20.GL_-ONE)或预乘
GLES20.glBlendFunc(GLES20.GL_ONE,GLES20.GL_ONE减去SRC_ALPHA)谢谢。我尝试添加:GLES20.glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去_SRC_ALPHA)和您提到的选项,但我没有注意到任何更改。我添加了GLES20.glEnable(GLES20.GL_BLEND),因为默认情况下它是禁用的,但尚未工作。它确实会改变辉光效果的颜色,因此有时颜色会更暗,有时看起来或多或少是相同的。最有可能的是,辉光效果纹理的颜色通道会在发光区域的边缘淡出。这意味着发光区域边缘的纹理颜色通道具有从白色到黑色的渐变。如果是这种情况,则错误地混合纹理。您需要将辉光效果(
textureColor2
)添加到基础纹理(
textureColor
):
gl\u FragColor=textureColor+textureColor2。请注意,这只是一个猜测,因为我看不到辉光效果纹理。另一个选项是
gl_FragColor=textureColor+textureColor2*textureColor2.a请参见我的编辑3。我附加了另外两个图像那里。现在的结果与png中蝴蝶的实际外观不同。也许应该进一步更改片段着色器或默认的顶点着色器?
private final static String FRAGMENT_SHADER =
        "precision mediump float;\n" +
                "varying vec2 vTextureCoord;\n" +
                "uniform lowp sampler2D sTexture;\n" +
                "uniform lowp sampler2D oTexture;\n" +
                "void main() {\n" +
                "   lowp vec4 textureColor = texture2D(sTexture, vTextureCoord);\n" +
                "   lowp vec4 textureColor2 = texture2D(oTexture, vTextureCoord);\n" +
                "   lowp float alphaDivisor = textureColor2.a + step(textureColor2.a, 0.0);\n" +
                "   gl_FragColor = vec4(mix(textureColor.rgb, textureColor2.rgb / alphaDivisor, textureColor2.a), textureColor.a);\n" +
                "}\n";