Advertisement

Fading to black

Started by January 26, 2003 08:20 AM
4 comments, last by Etus 22 years, 1 month ago
Hey, I''ve almost finished writing the demo for NeHe''s contest, but I bumped into a problem. I want, in the end of the demo, to fade the screen to black. This is what I have so far:

glMatrixMode(GL_PROJECTION);	
		glPushMatrix();
		glLoadIdentity();						
		
		glOrtho(0.0f, ConMan.GetResolutionX(), ConMan.GetResolutionY(), 0.0f, -1.0f, 1.0f);	
		
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();
		
		glEnable(GL_BLEND);	
		glDepthMask(GL_FALSE);	
		glBlendFunc(GL_ONE, GL_ONE);
		
		glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
		
		glBegin(GL_QUADS);
		
		glVertex2d(ConMan.GetResolutionX(), 0);
		glVertex2d(0, 0);
		glVertex2d(0, ConMan.GetResolutionY());
		glVertex2d(ConMan.GetResolutionX(), ConMan.GetResolutionY());	
		
		glEnd();
		
		glDisable(GL_BLEND);	
		glDepthMask(GL_TRUE);	
		
		glPopMatrix();
		
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
 
Apparently, my blending equation doesn''t work well with fading-to-black. Does any one knows what is the correct blending equation(where 0.0f is completely transparent, 0.5f is semi-transparent and 1.0f is completely opaque)? Thanks, Yuval
Sorry - didn''t read your post. Here''s what you can do to implement a fade:


  void DrawGLScene(){//draw everythingif(bFadeOut)  {  GoToOrthoMode();  glColor4f(0, 0, 0, FadeOutAlpha);  glDisable(GL_TEXTURE_2D);  glDisable(GL_LIGHTING);  glEnable(GL_BLEND);  //standard blending  glBelndFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  glBegin(GL_QUADS);    glVertex2f(0, 0);    glVertex2f(0, ScreenWidth);    glVertex2f(ScreenHeight, ScreenWidth);    glVertex2f(ScreenHeight, 0);  glEnd();  FadeOutAlpha += 0.01; //or whatever factor you''d like  GoToPerspectiveMode();  }}  


Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
Thanks for replying.
Your soultion partly works. Notice that the alpha value from 0.0f to 0.5f causes the quad to be completely transparent(and not a bit opaque), thus, when I do the fade-to-black effect, the screen immediately comes quite dark(when it reaches 0.5f) and then it fades to black as it should.

Do you know how to fix this problem?

Thanks,
Yuval
quote:
Original post by Etus
Notice that the alpha value from 0.0f to 0.5f causes the quad to be completely transparent(and not a bit opaque)


Not true. Check your code. Did you initialize the FadeOutAlpha variable to zero before starting to add to it?

Crispy



"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Right, I fixed the problem. I had Alpha Test on so I needed to disable it. Thanks a lot for your help.

- Yuval

[edited by - Etus on January 26, 2003 12:21:31 PM]
the problem you had was that your blending equation was additive.
ie,

glBlendFunc(GL_ONE, GL_ONE);

what that means is that the two pixels being considered (the source and destination, - think of that as the pixel you are drawing and the pixel already on screen) will be multiplied by the values passed to glBlendFunc, then added together, and put on screen.

This means your simply adding the black square your drawing, with the pixels already on screen...

ie,

blackSquare*GL_ONE + existingPixel*GL_ONE

that obviously should simply equal the existing pixel, hence you don''t fade to black.

to fade to black correctly, you can use the alpha value, hence

glBelndFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

it means that the black square is multiplied by the alpha value, while the existing pixel is multiplied by 1- the alpha value. then added.

so...

if the alpha value is 0.1, ie, slightly faded,
you get:

blackSquare*0.1 + existingPixel*(1-0.1)

or

blackSquare*0.1 + existingPixel*0.9

and since it''s black, that effectivly becomes

existingPixel*0.9

hence you get a fade effect.

This all makes blending very very useful indeed, when you know how to exploit it.

| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |

This topic is closed to new replies.

Advertisement