简体   繁体   中英

Point Sprite Alpha Blending

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:

在此处输入图片说明

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:

在此处输入图片说明

I've also tried this blending function:

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

Which results in this:

在此处输入图片说明

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.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM