manatttta
manatttta

Reputation: 3124

OpenGL blending: texture on top overlaps its pixels which should be transparent (alpha = 0)

I am drawing a map texture and, on top of it, a colorbar texture. Both have alpha channel and I am using blending, set as

// Turn on blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

However, the following happens:

texture on top with alpha channel imposes its black pixels

The texture on top (colorbar) with alpha channel imposes its black pixels, which I don't want to happen. The map texture should appear behind where the colorbar alpha = 0.

Is this related to the blending definitions? How should I change it?

Upvotes: 2

Views: 1513

Answers (1)

jozxyqk
jozxyqk

Reputation: 17266

Assuming the texture has an alpha channel and it's transparent in the right places, I suspect the issue is with the rendering order and depth testing.

Lets say you render the scale texture first. It blends correctly with a black background. Then you render the orange texture behind it, but the pixels from the scale texture have a higher depth value and cause the orange texture there to be discarded.

So, make sure you render all your transparent stuff in back to front order, or farthest to nearest.

Without getting into order independent transparency, a common approach to alpha transparency is as follows:

  1. Enable the depth buffer
  2. Render all your opaque geometry
  3. Disable depth writes (glDepthMask)
  4. Enable alpha blending (as in the OP's code)
  5. Render your transparent geometry in farthest to nearest order

For particles you can sometimes get away without sorting and it'll still look OK. Another approach is using the alpha test or using alpha to coverage with a multisample framebuffer.

Upvotes: 4

Related Questions