简体   繁体   English

OpenGL渲染到纹理透明度问题

[英]OpenGL Render To Texture Transparency Problem

I build a "postprocessing pipeline" which collects rendercalls and puts them into a texture. 我建立了一个“后处理管道”,该管道收集rendercall并将其放入纹理中。 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). 首先,我使用glClearColor(0,0,0,0)清除缓冲区。 Then I call the draw calls. 然后我打电话给抽奖电话。 The comes when an object has a texture with 0.5 alpha. 当对象具有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. 例如,如果纹理的像素为0.5、0.5、0.5、0.5,则结果为0.25、0.25、0.25、0.5,因为会影响透明度为零的黑色背景。

I use the glCopyTexImage2D function the put my render result into a texture. 我使用glCopyTexImage2D函数将渲染结果放入纹理中。 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? 有人对我有建议吗?

You could try using premultiplied alpha when drawing. 您可以在绘制时尝试使用预乘alpha。 Basically, ensure that all your colour values are already multiplied by the alpha value. 基本上,确保所有颜色值都已经乘以alpha值。 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. 然后,纹理的像素将为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. 结果将是0.25、0.25、0.25、0.5,因为它们都被乘以1。当然,您必须确保将结果输出纹理再次正确解释为具有预乘alpha。

There's a pretty good explanation in a dusty corner of Wikipedia. 在Wikipedia的尘土飞扬的角落里有一个很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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