Page 1 of 2

OpenGL fade effect

Posted: Mon Sep 13, 2010 6:10 pm
by jjackdev
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

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 6:33 pm
by ajtgarber
Fade to black? Fade to another level/scene?

If its fade to black then you could make a rectangle that covers the screen then decrease the transparency.

Sorry ahead of time, I can't really help with code since I don't know OpenGL too well.

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 7:10 pm
by dandymcgee
ajtgarber wrote:Fade to black? Fade to another level/scene?
Details, please. ;)

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 7:52 pm
by jjackdev
Yes a fade to black effect in OpenGL.

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 8:01 pm
by WSPSNIPER
im unaware of exactly what you mean but if its what i think you could create a transparent quad over the window and make it less transparent until it was fully black

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



Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 8:02 pm
by davidthefat

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 8:18 pm
by jjackdev
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.

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 8:42 pm
by WSPSNIPER
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.
can you restate that in different words lol i dont understand you're problem 100%

here is how i think i understand it so it dosent fade it just turns black? why would it fade when you press a key? did you make it that way?

so here is a halfways function for it

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
}
Edit: you may want to add a timer to this so it dosent go so fast

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 9:18 pm
by jjackdev
I tried your method WSPSNIPER but my computer froze. But here is the problem so I am testing a warp like thing like this.

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;
					}


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.

Thank you guys for all your help.

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 9:26 pm
by WSPSNIPER
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.

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;
					}


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.

Thank you guys for all your help.

CRAP im so sorry, i ment to put fade(alpha + 0.1f) not - !!! infinite iteration!!!! im so SORRY

funny that + turned to - can mess somthing up so much :P im going to edit my post to fix it

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 9:29 pm
by WSPSNIPER
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.

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;
					}


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.

Thank you guys for all your help.

ok now that i fixed my infinate iteration ( i edited my post my - should have been a + ) i was looking at your code and was wondering if you enable GL_BLEND and call the glBlendFunc ... its important!!!

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 9:30 pm
by jjackdev
I fixed it in my code but it did not work? I just need a solution for my current solution.

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 9:57 pm
by WSPSNIPER
jjackdev wrote:I fixed it in my code but it did not work? I just need a solution for my current solution.

ok what is the problem? is it just a black screen. it the contents in the background are on the same z plane as the rect you are drawing and you enable GL_DEPTH it will not render so try glDisable(GL_DEPTH); or make sure that the contents under the rect are on a > z coord then the rect you are rendering

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 10:08 pm
by jjackdev
Let me tell you the whole problem.

1. I check to see if there are any collisions (that works)
2. I see if the collision is a certain collider (that works)
3. I then fade it while warping the character (the warp works not the fade)

The problem with the fade is it just stays completely black but when I press a key it goes to the current alpha like if triggering a key turns on the alpha blending.

The fading works because when I just trigger the fading using a key that I set then it works flawlessly.

Re: OpenGL fade effect

Posted: Mon Sep 13, 2010 10:14 pm
by WSPSNIPER
alright so lets think for a sec, if the fade and the warp work apart it must be something when they are being called together, so can you post the code where you call the warp and the fade.