OpenGL: How to do RGBA->RGBA blitting without chan

2019-04-02 05:43发布

问题:

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.