PDA

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



RealtimeSlave
04-05-2015, 04:53 AM
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!

Alfonse Reinheart
04-05-2015, 05:35 AM
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 (https://www.opengl.org/wiki/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 (https://www.opengl.org/wiki/Framebuffer#Draw_color_buffers). 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 (https://www.opengl.org/wiki/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.

RealtimeSlave
04-06-2015, 02:44 AM
Thanks a lot! This is exactly what I need.