PDA

View Full Version : fading problem



Tycho
08-13-2009, 11:05 PM
Hi folks,

I'm doing something pretty basic, but being the beginner I am, I've run into a brick wall, head first.

I've got a 2D (orthographic) app. I have a "window" assembled by a series of quads and line loops, and I want to fade it out. Is there a way to do this without modifying the glColor alpha parameter for every single vertex?

Could I do something like this?
1. Enable lighting
2. Set the lighting alpha value to match the current alpha at this point in the fade
3. Render the "window"
4. Disable lighting

It seems like quite a hack, and when I've tried it, the quads and line loops turn white. The code I'm using for the fade effect is below:


void Fade::Begin()
{
glEnable(GL_LIGHTING);
GLfloat light0[] = {0.0f, 1.0f, 1.0f , 1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, light0);
glEnable(GL_LIGHT0);
Glfloat global_ambient[] = {0.5f, 0.5f, 0.5f, m_alpha};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
}

void Fade::End()
{
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);
}

I feel like I'm going entirely in the wrong direction. Any suggestions?

Tycho
08-14-2009, 10:41 AM
Hmm. No ideas?

ZbuffeR
08-15-2009, 07:40 AM
Indeed this seems quite fragile.
A simple and robust (but not very fast) method is to render a fullscreen quad on top of the unfaded frame, with varying alpha settings over time.

Something like :

-RENDERLOOP:
-draw scene (render the "window")
-enable blending :
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-alpha goes from 1.0f to 0.0f for a fade to black
-glColor4f(0f,0f,0f,alpha);
-draw full screen quad
-swapbuffer
-goto RENDERLOOP