GBuffer (color texture) with NORMAL and SPECULAR: Update only alpha

Hello,

I use a color texture as GBuffer to store in the RGB part a normal and in the A part a specular value.
The GBuffer is bound as 2nd render target (MRT).

Now I am wondering if it is possible to update only the alpha value using a specific glBlendFunc() setup?

For example when my shader outputs this to the 2nd render target (DST)…

f_Color1 = vec4(0.0, 0.0, 0.0, Specular);

…then I would like to achieve two cases:

1. In this case only the A part in DST should be overwritten with the value in “Specular”. The RGB values in DST should stay untouched.

2. In this case the value in “Specular” should be added to the A part in DST. The RGB values in DST should stay untouched.

Help is really appreciated!

Now I am wondering if it is possible to update only the alpha value using a specific glBlendFunc() setup?

You can use blending, but generally the way to handle case 1 is with an appropriate write mask:

glColorMaski​(X, GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);

Where “X” is the color index of the draw buffer that represents your specular image buffer. Since you’re doing deferred rendering, your FBO probably has multiple color buffers, so you need to specify in the write mask that you’re referring to that particular color buffer.

For case 2, you must use blending, since you’re adding the values. Though using the write mask above is not a bad idea in addition to blending:

glEnablei(GL_BLEND, X);
glBlendEquationSeparate​i(X, GL_FUNC_ADD​);
glBlendFuncSeparatei(X, GL_ZERO​, GL_ONE, GL_ONE, GL_ONE);

Where “X” has the same definition as above. Note that, while the indexed color mask (glColorMaski) has been core OpenGL functionality since GL 3.0, the indexed blend enable/equation/func requires 4.0 or ARB_draw_buffers_blend. If it’s not available, you’ll have to use the non-indexed forms.

Thanks a lot! This is exactly what I need.