OpenGL透明图像中有黑色 [英] OpenGL transparent images have black in them
问题描述
我正在为Android游戏开发游戏,我想知道为什么每当我绘制具有透明性的图像时,似乎总是在透明部分中添加了一些黑色.这种情况到处都是,使我的某些效果看起来很奇怪.
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 happens all over and makes some of my effects look strange.
这里是一个例子.这两个圆圈只是带有模糊效果的白色图像,但是您可以看到一个重叠的图像带有阴影.如果我在Inkscape中重叠了两个圆圈,则重叠的地方会变成纯白色.
Here is an example. The two circles are only white images with a blur but you can see when one overlaps the other it has a shadow. If I overlap two of the circles say in Inkscape I get pure white where they overlap.
我正在使用
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
用于我的混合.
您知道为什么会发生这种情况以及如何避免这种情况吗?
Any idea why this happens and how I can avoid it?
我唯一能想到的是两个图像具有相同的z,所以也许它们仅与背景混合而不是彼此混合?
the only thing I can think of is that the two images have the same z so maybe they are blending only with the background instead of each other?
我改变了
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
到
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_DST_ALPHA);
这是我想要的结果.
现在唯一的是我所拥有的透明图像中有黑色的透明图像被忽略了,这是有道理的,因为我认为目标alpha为1.为什么一个负源会添加该灰色?
The only thing now is that the transparent images that I had that have a transparent black in them are ignored, which makes sense because I think the destination alpha is 1. Why would One minus source add that gray?
推荐答案
我想出了解决方法.
我改变了
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
到
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
事实证明,Android在加载时会预先乘以PNG图像的纹理Alpha(使白色变为灰色).
It turns out that Android pre-multiplies the alpha of textures of PNG images when you load it (making white turn gray).
我添加了
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.
这篇关于OpenGL透明图像中有黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!