# glBlendFunc - fading frame buffer to color

## Recommended Posts

KaelisAsur    122
I want to fade what i already have accumulated in my frame buffer to specific color. For starters, simple fade to black. So each frame i draw a quad big enough to fill the screen, with glColor4f(0.0f,0.0f,0.0f, fade ), where fade is set to a low value (0.001, for example), and i do not clear the frame buffer. My understanding is that the blending is performed with the following formula: result = sfactor * source + dfactor * destination Where source is pixel RGB value of incoming data, destination is pixel RGB value of data present in framebuffer and sfactor and dfactor are defined in glBlendFunc( GLenum sfactor, GLenum dfactor ). Since what i really want is to just reduce all pixel RGB values to 0, i thought i could just set glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA), which would give me 0 * source + 0.999 * destination (i know the fading process would actually 'slow down' with such setting, but nevermind that for now). Well, it doesnt work. The whole screen becomes black immediately. However, it kind of works with glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA). Kind of, because for pixel of RGB 255,255,255, for some odd reason it stops at 63,63,63. This confuses me even more. Why would GL_ONE matter, since the RGB values of source are all 0 anyway? And why does it stop at 63,63,63? Thanks.

##### Share on other sites
Have you enabled blending (GL_BLEND) and disabled alpha testing (GL_ALPHA_TESTING) before drawing your quad? Also, is it drawn after all the other graphics has been drawn?

##### Share on other sites
KaelisAsur    122
Yes, blending is enabled, alpha testing is disabled, and the quad is drawn after all the other graphics.

Here are my init settings:

	glEnable(GL_BLEND);        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);	glClearDepth(1.0f);	glEnable(GL_DEPTH_TEST);	glDepthFunc(GL_LEQUAL);							glDisable(GL_ALPHA_TEST);

##### Share on other sites
V-man    813
You need to use
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Also, make sure to clear then screen and render the frame every time.

##### Share on other sites
KaelisAsur    122
Quote:
 Original post by V-manYou need to useglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Same results as glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)

Quote:
 Also, make sure to clear then screen and render the frame every time.

But the whole point is to gradually fade the frame buffer each frame. So i need to keep the buffer, at least until it has faded out. If that wasnt the case, i would render a quad with varying transparency or just render everything to a texture beforehand and then fade the texture. But thats not what i want to do.

##### Share on other sites
zedz    291
try

<BLEND src="GL_CONSTANT_COLOR" dst="GL_ONE_MINUS_CONSTANT_COLOR"/>
glBlendColor( X,X,X,X );

though it mightnt completely clear the screen (like with what youre seeing)

##### Share on other sites
V-man    813
Quote:
 Original post by KaelisAsurBut the whole point is to gradually fade the frame buffer each frame. So i need to keep the buffer, at least until it has faded out. If that wasnt the case, i would render a quad with varying transparency or just render everything to a texture beforehand and then fade the texture. But thats not what i want to do.

Typically, the back buffer is not preserved when you do a swapbuffer
We call that undefined behavior. On some GPU/drivers it works and on others it does not.
Anyway, you won't get any benefit by avoiding the re-render.