OpenGL fade effect

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

OpenGL fade effect

Post 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
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: OpenGL fade effect

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: OpenGL fade effect

Post by dandymcgee »

ajtgarber wrote:Fade to black? Fade to another level/scene?
Details, please. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

Re: OpenGL fade effect

Post by jjackdev »

Yes a fade to black effect in OpenGL.
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: OpenGL fade effect

Post 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


User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: OpenGL fade effect

Post by davidthefat »

jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

Re: OpenGL fade effect

Post 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.
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: OpenGL fade effect

Post 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
Last edited by WSPSNIPER on Mon Sep 13, 2010 9:27 pm, edited 1 time in total.
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

Re: OpenGL fade effect

Post 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.
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: OpenGL fade effect

Post 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
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: OpenGL fade effect

Post 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!!!
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

Re: OpenGL fade effect

Post by jjackdev »

I fixed it in my code but it did not work? I just need a solution for my current solution.
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: OpenGL fade effect

Post 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
jjackdev
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Mon Aug 30, 2010 11:55 pm

Re: OpenGL fade effect

Post 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.
User avatar
WSPSNIPER
Chaos Rift Regular
Chaos Rift Regular
Posts: 145
Joined: Sun Jan 03, 2010 6:19 pm
Current Project: top down shooter
Favorite Gaming Platforms: ps3
Programming Language of Choice: c++

Re: OpenGL fade effect

Post 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.
Post Reply