Blending mode

I’ve been playing with openGL (ES) blending in my new iPhone app, and I’m having a hard time wrapping my head around it. I have some particles, that overlap. They have a transparency. I want it so even if they are overlapping, the alpha stays the same. However all I can get it to do is increase the alpha when they overlap.

It seems most people recommend glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). But that gives the result on the left, I want the result on the right.

Thanks for any help.

Make sure you draw the particles with alpha = 255.

Can you compute the blended color yourself and issue that as the geometry color instead of using blending? Just setting alpha to 255 will suppress blending with the background.

  • Chris

Yes, that is exactly my problem chris. If I use full alpha, the particles are no longer transparent. I suppose I could try calculating the particles color, but how would I use that for the blending?

If you use the precomputed blend color (e.g., for particles with 25% opacity, you’d have 0.25 * yellow + 0.75 * background), then you wouldn’t need blending at all. Nonetheless, you’d achieve the exact same effect. This would only work if the background is uniform and all particles are the same color. From the user’s standpoint in your scheme, one cannot tell the difference between transparent particles and darker particles.

Perhaps the saturate option to glBlendFunc help you achieve what you want for non-uniform backgrounds. I don’t know much about this. You need a framebuffer with an alpha plane and I’m not sure how supported these are in OpenGL ES contexts.

  • Chris

If the background is uniform, you can do what kaerimasu suggested. If you’re only targeting iPhone 3GS you could use the stencil buffer to reject more than one layer at each pixel. If you also want to support the older models, it gets a bit tricky.

  1. Clear the framebuffer with (r,g,b, 1-x), where x is the alpha value you want to use for your particles. Render opaque background objects with blending disabled, but writing an alpha value 1-x, (use the texture combine environment to replace the alpha component with this constant).

  2. Enable blending and set the blend factors to GL_SRC_ALPHA_SATURATE, GL_DST_ALPHA. Render the particles with alpha = 1.