PDA

View Full Version : Transparency without background



phdr_hroch
06-19-2013, 11:55 PM
Hi my problem:
I dont know what background will have result image of my transparent rendering.
glBlendFunc((GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) world fine with known background. But I cant render transparent image and send it, when I dont know bgcolor of viewer. For example if he has white background and I render image to white background, image will be blended twice and everything will be more white.

1. solution - get somehow bgcolor with request: render transparent image with transparent background (alpha = 0), and then set every pixel with alpha > 0 to alpha = 1

2. solution - somehow preserve color and set correct alpha channel, so it will look good with every bgcolor, but I dont have idea how.

2.1: When I have only one transparent triangle, its easy -> I need only to set correct alpha channel and write nonmultiplied color to color buffer. (i will not use blending at all). But when I have multiple triangles on top of each other I dont know what and how to blend and how to set result alpha value.

I really need and want to solve solution 2, how to do it in OpenGL ? :) IE. never blend with background, always only what I dra

Thx Filip

GClements
06-20-2013, 01:21 AM
2. solution - somehow preserve color and set correct alpha channel, so it will look good with every bgcolor, but I dont have idea how.

Create a RGBA framebuffer, clear to (0,0,0,0) (i.e. transparent), disable blending. The source alpha will be copied directly to the framebuffer.



2.1: When I have only one transparent triangle, its easy -> I need only to set correct alpha channel and write nonmultiplied color to color buffer. (i will not use blending at all). But when I have multiple triangles on top of each other I dont know what and how to blend and how to set result alpha value.

Create a RGBA framebuffer, clear to (0,0,0,0) (i.e. transparent), use glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA), source colours must use pre-multiplied alpha (e.g. yellow with 70% opacity is (0.7,0.7,0,0.7), not (1,1,0,0.7). That includes textures, so if you're loading images which don't use pre-multiplied alpha, you'll need to either multiply the R/G/B components by the alpha component after loading but before uploading the data to a texture, or perform this operation in a fragment shader.
The resulting image will have pre-multiplied alpha, so you'll need to "un-multiply" it (i.e. divide the R/G/B channels by the alpha channel) if you're saving the result to an image format which doesn't support pre-multiplied alpha.

phdr_hroch
06-20-2013, 03:12 AM
Thank you, I found another solution:

I solved it now by using background with 0.0f alpha channel and glBlendFunc GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA and changed draw order.