Point Sprite Alpha Blending

461 views Asked by At

I'm working on a paint app for Android that allows users to draw using their finger. It's built using OpenGL ES 2.0 using point sprite technique as well as an FBO for quick rendering. I am having an issue blending the individual point sprites together where the transparent regions are correctly rendered on the FBO but when the sprites overlap I can see the transparent areas being rendered on the previous sprite. Here's how it looks currently:

enter image description here

This is with this blending equation:

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

If I change the drawing color to white or black it works perfectly:

enter image description here

I've also tried this blending function:

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

Which results in this:

enter image description here

Which is almost perfect but instead to white the main color should darken. Any ideas on what the correct blending function should be to achieve this?

Note: I have an idea of how the blending function works by taking fractions of the source and destination color and in my case adding them together so it makes sense that the color would go towards white. So I'm wondering if what I would like to achieve can be done with blending function only or would I need something else? I can provide code from the fragment shader if necessary but it doesn't look like a fragment problem to me.

1

There are 1 answers

0
Georgi On BEST ANSWER

Okay so I found out what was wrong. It wasn't the blending function after all but the fragment shader.. I had some logic to unpremultiply the pixels which was redundant as the blending function

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

already handles premultiplied images. In addition to that, I had some code in the fragment shader to convert the image to grayscale and then to convert the grayscale into transparency and my color multiplications were wrong.

Finally I ended up leaving my original blending function unchanged (as above). It works properly and here's the result I was looking for:

enter image description here