OpenGL: How to do RGBA->RGBA blitting without changing destination alpha

你。 提交于 2019-12-21 06:06:52

问题


I have an OpenGL RGBA texture and I blit another RGBA texture onto it using a framebuffer object. The problem is that if I use the usual blend functions with glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), the resulting blit causes the destination texture alpha to change, making it slightly transparent for places where alpha previously was 1. I would like the destination surface alpha never to change, but otherwise the effect on RGB values should be exactly like with GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA. So the blend factor functions should be (As,As,As,0) and (1-As,1-As,1-As,1). How can I achieve that?


回答1:


You can set the blend-modes for RGB and alpha to different equations:

void glBlendFuncSeparate(
    GLenum srcRGB, 
    GLenum dstRGB, 
    GLenum srcAlpha, 
    GLenum dstAlpha);

In your case you want to use the following enums:

  glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);

Note that you may have to import the glBlendFuncSeparate function as an extension. It's safe to do so though. The function is around for a very long time. It's part of OpenGL 1.4

Another way to do the same is to disable writing to the alpha-channel using glColorMask:

void glColorMask( GLboolean red,
                  GLboolean green,
                  GLboolean blue,
                  GLboolean alpha )

It could be a lot slower than glBlendFuncSeparate because OpenGL-drivers optimize the most commonly used functions and glColorMask is one of the rarely used OpenGL-functions.

If you're unlucky you may even end up with software-rendering emulation by calling oddball functions :-)




回答2:


Maybe you could use glColorMask()? It let's you enable/disable writing to each of the four color components.



来源:https://stackoverflow.com/questions/146140/opengl-how-to-do-rgba-rgba-blitting-without-changing-destination-alpha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!