OpenGL transparent images have black in them

后端 未结 4 555
醉酒成梦
醉酒成梦 2021-01-13 03:31

I am working on a game for Android and I was wondering why whenever I draw images with transparency there seems to always be some black added to the transparent parts. This

相关标签:
4条回答
  • 2021-01-13 04:18

    I figured out how to fix it.

    I changed

    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    

    to

    GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    

    It turns out that Android pre-multiplies the alpha of textures of PNG images when you load it (making white turn gray).

    I added

    vColor.r = vColor.r * vColor.a;
    vColor.g = vColor.g * vColor.a;
    vColor.b = vColor.b * vColor.a;
    

    to my vertex shader to do the multiplying for my other colors.

    0 讨论(0)
  • 2021-01-13 04:18

    Are you sure your content is correct? If you don't want the circles to produce any black color the color values in the image should be completely white, and the alpha channel should define the shape of the circle. Now it looks like the image has a white circle with both alpha channel and the color value fading to zero, which leads to a halo.

    0 讨论(0)
  • 2021-01-13 04:23

    Are you using linear sampling? My thought could be if you have a very small image (less than say 30-40 pixels in dimension), you could be getting interpolated alpha values between the inside of the circle (alpha = 1) to the outside of the circle (alpha = 0). This would give you intermediate alpha values that result in the kind of blur effect.

    Try the following code when you have the circle texture bound:

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    

    Maybe you can host the actual texture that you're using such that we can inspect it? Also please post your drawing code if this doesn't help.

    0 讨论(0)
  • 2021-01-13 04:26

    You can also divide by alpha, because, the problem is, when you export your textures, Image processing software may pre-multiply your Alpha channel. I ended up with alpha division into my fragment shader. The following code is HLSL, but you can easily convert it to GLSL:

    float4 main(float4 tc: TEXCOORD0): COLOR0
    {
        float4 ts = tex2D(Tex0,tc);
    
        //Divide out this pre-multiplied alpha
        float3 outColor = ts.rgb / ts.a;
    
        return float4(outColor, ts.a);
    }
    

    Note that this operation is lossy, very lossy and even if it will most likely suffice in cases such as these, it's not a general solution. In your case you can totally ignore the COLOR and serve original alpha AND white in your fragment shader, e.g. return float4(1,1,1,ts.a); (convert to GLSL)

    0 讨论(0)
提交回复
热议问题