OpenGL Image Fade-In / Out Routines?

Started by
10 comments, last by aphidtwix 22 years, 7 months ago
Would anyone be able to explain some simple OpenGL Fade-In / Out routines please? I''m assuming it''s something like store an image in the buffer, make the screen black, set the palette of the image in the buffer to black...swap buffers..and then slowly bring in the palette to it''s normal setting? this is just a rudimentary guess, remembering the old ways i used to do fades in turbo pascal anyway, any help would be appreciated. cheers,
at
Advertisement
Well, when you say "image" you really ought to be thinking "texture map" because that''s really the best way to get images to appear in openGL. With that in mind, load your image, make it a texture, bind it, draw a quad that fits the screen perfectly with this texture on it and you''re most of the way there. To fade it up, slowly ramp up the color of the quad from 0''s to 1''s.

Alternately, if you''re trying to fade up a whole scene, you can draw your scene and then draw a transparent black fullscreen poly over it and slowly reduce it''s alpha (using glBlendFunction(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA) to get the right blending)...

Those are the abstracts, if you''re looking for code, I''m sure somebody can oblige.
Even JPG''s are considered textures in OpenGL?

i did not know that. are they? =)

same deal theory for the fade even if using a jpg?

cheers,


at
at
Yup. If you figure that all images (regardless of file format) are a collection of pixels and the best way for openGL to deal with a collection of pixels is to texture map it, then it all makes sense. Sort of. I wish the old IRIS GL "rectwrite" functions were in openGL, but alas, I guess that''s asking too much or something...
I''m still new at this so I may be totall wrong, but can''t you adjust the gamma corection? I don''t know if openGL can do this, but this is the method i''ve always used in 3D programming.
[Editted: Oops, I confused fading with translucency. Ignore this message ]


You should look up alpha-blending in the red book. Alpha blending is just another word for transparency effects, sorta. Anyways, if you want to fade something, you enable blending and draw the object with a certain color:

// inside main game loop, executed once per frame
static int alpha = 1.0;
glEnable(GL_BLEND);
glColor4f(r,g,b,a);
alpha -= 0.05;

This lowers the objects alpha each frame by a small amount. If an object has zero alpha its completely transparent, if it has alpha=1, its completely opaque.

I'm not sure this is the most efficient way of doing this, though.


Edited by - Z01 on September 9, 2001 2:28:40 AM
basically all i wanted to do was take a couple simple texture maps and have a y''know

black screen
(fade in)
aphidtwix presents
(fade out)
black screen
(fade in)
a such and such production
(fade out)
black screen
(fade in)
programming: aphidtwix
music: aphidtwix
3d gfx: zulu
(fade out)
black screen

that''s basically all im going for..what''s the easiest way to go about that?

err not the easiet..but the best and most optimized way of going about that, excluding going to ASM cause i know squat about ASM.
at
I think the best way is to draw the bitmap to the screen however you like (if it''s a non power of 2 image, then glDrawPixels is probably a little slow, but good enough for something so simple) then just draw a black, alpha-blending quad over the whole screen. To fade out, start with a completely transparent quad (glColor4f( 0.0f, 0.0f, 0.0f, 0.0f )) and fade it slowly to a fully opaque one (glColor4f( 0.0f, 0.0f, 0.0f, 1.0f )). Then you switch images and fade back to transparent again.

codeka.com - Just click it.
Ok, i got all that black transparent / opaque quad setup covering my texture. I know because when is the alpha to varying degrees 0.0 -> 1.0 i see it or i dont.

my next question is the matter of actually fading it in.
i tried a really stupid timer setup that i used which totally did not work. it just flashed and disappeared..i''ve setup a new timing method and im curious if anyone sees anything wrong in my timing code (well obviously cause it does''t work) heh..

here is my timer code...this function was borrowed from somewhere. i forget exactly where so sorry. but it was a basic timer setup.

GLfloat GetTimePassed()
{
GLfloat timeoffset;
GLfloat currenttime;

if (lasttime == 0) lasttime = (GLfloat)GetTickCount();
// Get the current time
currenttime = (GLfloat)GetTickCount();
// Calculate the offset
timeoffset = currenttime - lasttime;
// Put the current time in the lasttime variable
lasttime = currenttime;
// return the time offset in seconds per frame
return timeoffset / 1000;
}


now in my actual code i have the follwing:

GetTimePassed();
if ( lasttime >= 0 )
{
this->ourTexture->Skin();

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// The Black Quad
glBegin(GL_QUADS);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();

glBegin(GL_QUADS);
// The Texture That we want to fade in
// (hidden behind the black quad)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();

do
{
glColor4f(1.0f,1.0f,1.0f,a);
a+=lasttime;
} while (lasttime <= ??? (what shold this be?) );
}

glFlush ();

btw before all this stuff comes in glInit() i set
glColor4f(1.0f,1.0f,1.0f,0.0f);


any help would be appreciated =)

cheers,


at
at
not sure if i am right here but what tha heck....


I think you should do it like this....



glColor4f (0,0,0,a);
//DRAW BLACK QUAD!!


glColor4f (1,1,1,1);
//DRAW TEXTURED QUAD!


if (a>0 && a<1)
[edit : ----> if (a>=0 && a<1) ] //This should be more right
a += Tick;
else
a -= Tick

if (a<0)
{
//SWITCH TEXTURES
a = 0.0f; //Reset fade value to start over again....
}


Hope this can help you out a little...
This almost the same way as i do it myself... (I took this from my head...but it's not the hardest topic around so it might be almost right from my own code...)!!

Take Care!




- -- ---[XaOs]--- -- -

[ project fy ]

Edited by - xaos on September 12, 2001 10:01:27 AM
- -- ---[XaOs]--- -- -[ Project fy ||| CyberTux ]

This topic is closed to new replies.

Advertisement