glBlendFunc - fading frame buffer to color

Started by
5 comments, last by V-man 16 years ago
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.
Advertisement
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?
"Game Maker For Life, probably never professional thou." =)
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);
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.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by V-man
You need to use
glBlendFunc(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.

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)

Quote:Original post by KaelisAsur
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.


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.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement