I've simplified my problem to drawing a square to illustrate. I want to draw a texture, which fades the alpha channel from 0 to 255 and back to 0 vertically, so that it blends with the background. Right now it darkens the background, and I can't figure out why. This is using OpenGL ES on iOS 9.
First, I enable the blending that I think I want:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Then I fill the buffer with green:
glClearColor(0, 0.5, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
I have this image that I'm using as a texture:
And I draw the following vertices ({x, y, z},{r, g, b, a},{tx, ty}) with GL_TRIANGLES:
{{0.4, 0.3, 0}, {1, 0, 0, 1}, {0, 0}},
{{0.6, 0.3, 0}, {1, 0, 0, 1}, {1, 0}},
{{0.4, 0.5, 0}, {1, 0, 0, 1}, {0, 1}},
{{0.6, 0.3, 0}, {1, 0, 0, 1}, {1, 0}},
{{0.4, 0.5, 0}, {1, 0, 0, 1}, {0, 1}},
{{0.6, 0.5, 0}, {1, 0, 0, 1}, {1, 1}},
I expect the red component of the vertices to modulate the texture, as this is my fragment shader:
varying lowp vec4 DestinationColor;
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
void main(void) {
gl_FragColor = DestinationColor * texture2D(Texture, TexCoordOut);
}
It does, but the result is not simply red over green. It gets dark at the edges, where I would expect just the green to be drawn:
The answer to the similar question here says I should be using glTexEnv(GL_BLEND). However, I think that's an OpenGL ES 1 API.
Anyone know how I can achieve the desired blending? I want the red to fade across the green. The tops and bottoms should be green, not darker. There should not be obvious horizontal edges.
EDIT: I'm adding more pictures illustrating my problem. Here are two pink dots in GIMP:
When I move one on top of the other (in GIMP), they blend nicely. They also blend nicely with the green background:
When I recreate this in OpenGL ES, I get the darkening, which is especially visible where the pink dots are overlayed. I made one very narrow this time so you could see the effect better:
I tried both glBlendFunc and glBlendFuncSeparate, which resulted in the same effect. Note that the background alpha is 1.