OpenGL fade effect
Posted: Mon Sep 13, 2010 6:10 pm
How would you do an opengl fade effect? I've seen methods that suggest creating a huge rectangle but I don't know how to do the fade. Any help is appreciated.
Thanks
Thanks
The Next Generation of 2D Roleplaying Games
http://elysianshadows.com/phpBB3/
Details, please.ajtgarber wrote:Fade to black? Fade to another level/scene?
Code: Select all
// NOT exact syntax
float alpha = 0.f; // i think 0 is full transparent but it may be 1 lol my dyslexia
glPushMatrix();
glColor4f(0.f, 0.f, 0.f, alpha)
alpha += 0.1f // or -= if it is 1 for full transparent
// DRAW QUAD HERE
can you restate that in different words lol i dont understand you're problem 100%jjackdev wrote:Well I have implemented it but some stuff isn't working like when you have the fade it will just be pure black but when you press any of the keys it will actually do the fade. Any tips. I am using SDL and OpenGL with C++ with Xcode on a Mac.
Code: Select all
void init()
{
glEnable(GL_BLEND);
glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // the alpha stuff
// if in 2d, disable GL_DEPTH so the images behind will still show but if its 3d enable it
/// OTHER INITS
}
void fade(float alpha)
{
/// EDIT: ok it may look like this SDL_Delay(200); so it dosent faid so fast
if(alpha < 1.f)
{
glPushMatrix();
glColor4f(0.f, 0.f, 0.f, alpha);
// DRAW QUAD
glPopMatrix();
fade(alpha + 0.1f);
}
// recursive method to fade the screen
}
Code: Select all
for (int i = 0; i < TILE_SOLID_COUNT; i++) {
if (IsCollision(tileRect[i], playerRect) == true) {
collision = true;
if (i == 33) {
x = 303;
y = 223;
alpha = 0.0f;
fading = true;
fade();
}
...
}
//In the main loop
if (fading == true) {
if (alpha < 1.0f) {
alpha += 0.1f/350.0f;
fade();
}
if (alpha > 1.0f)
fading = false;
}
//Here is my fade function the alpha and fading variables are global
void fade()
{
glBegin(GL_QUADS);
glColor4f(0.0f, 0.0f, 0.0f, alpha);
glVertex2f(0.0f, 0.0f);
glVertex2f(640.0f, 0.0f);
glVertex2f(640.0f, 480.0f);
glVertex2f(0.0f, 480.0f);
glEnd();
}
//Here is the wierd part when I do a keypress it works perfectly
if (event.key.keysym.sym == SDLK_d) {
fading = true;
alpha = 0.0f;
}
jjackdev wrote:I tried your method WSPSNIPER but my computer froze. But here is the problem so I am testing a warp like thing like this.Now for some reason I cannot get it to work when it collides then the fading happens it just is a continous black with no fade.Code: Select all
for (int i = 0; i < TILE_SOLID_COUNT; i++) { if (IsCollision(tileRect[i], playerRect) == true) { collision = true; if (i == 33) { x = 303; y = 223; alpha = 0.0f; fading = true; fade(); } ... } //In the main loop if (fading == true) { if (alpha < 1.0f) { alpha += 0.1f/350.0f; fade(); } if (alpha > 1.0f) fading = false; } //Here is my fade function the alpha and fading variables are global void fade() { glBegin(GL_QUADS); glColor4f(0.0f, 0.0f, 0.0f, alpha); glVertex2f(0.0f, 0.0f); glVertex2f(640.0f, 0.0f); glVertex2f(640.0f, 480.0f); glVertex2f(0.0f, 480.0f); glEnd(); } //Here is the wierd part when I do a keypress it works perfectly if (event.key.keysym.sym == SDLK_d) { fading = true; alpha = 0.0f; }
Thank you guys for all your help.
jjackdev wrote:I tried your method WSPSNIPER but my computer froze. But here is the problem so I am testing a warp like thing like this.Now for some reason I cannot get it to work when it collides then the fading happens it just is a continous black with no fade.Code: Select all
for (int i = 0; i < TILE_SOLID_COUNT; i++) { if (IsCollision(tileRect[i], playerRect) == true) { collision = true; if (i == 33) { x = 303; y = 223; alpha = 0.0f; fading = true; fade(); } ... } //In the main loop if (fading == true) { if (alpha < 1.0f) { alpha += 0.1f/350.0f; fade(); } if (alpha > 1.0f) fading = false; } //Here is my fade function the alpha and fading variables are global void fade() { glBegin(GL_QUADS); glColor4f(0.0f, 0.0f, 0.0f, alpha); glVertex2f(0.0f, 0.0f); glVertex2f(640.0f, 0.0f); glVertex2f(640.0f, 480.0f); glVertex2f(0.0f, 480.0f); glEnd(); } //Here is the wierd part when I do a keypress it works perfectly if (event.key.keysym.sym == SDLK_d) { fading = true; alpha = 0.0f; }
Thank you guys for all your help.
jjackdev wrote:I fixed it in my code but it did not work? I just need a solution for my current solution.