Question

I build a "postprocessing pipeline" which collects rendercalls and puts them into a texture. After all postprocessing effects are rendered, I put all the results together. And here is my problem:

At the beginning, I clear the buffer with glClearColor(0,0,0,0). Then I call the draw calls. The comes when an object has a texture with 0.5 alpha. This results a mix between the backgroundcolor and the texture. I would like to avoid the mix. For example, if the pixel of the texture is 0.5, 0.5, 0.5, 0.5 the result is 0.25, 0.25, 0.25, 0.5, because the black background with zero transparency is affected.

I use the glCopyTexImage2D function the put my render result into a texture. I read articles about using a framebuffer, but I would like to be sure if I can solve my problem with using of multiple framebuffers.

Does anyone has an advice for me?

Was it helpful?

Solution

You could try using premultiplied alpha when drawing. Basically, ensure that all your colour values are already multiplied by the alpha value. Then, instead of

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

you would use

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

The pixel of the texture would then be 0.25, 0.25, 0.25, 0.5. The result would be 0.25, 0.25, 0.25, 0.5 because this is all multiplied by 1. Of course, you must ensure that the resulting output texture is again interpreted correctly as having premultiplied alpha.

There's a pretty good explanation in a dusty corner of Wikipedia.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top