My OpenGL fade function

Started by
7 comments, last by 3Dgonewild 16 years, 10 months ago
Well ,im having hard times with my fade function , and i need your help a little.. Here's the function:


sys::DO_FADE()
{
for (int f=0;f<255;f++)
{
glClearColor(f,f,f,f);
SDL_Delay(0);
glLoadIdentity();
}
}

main.cpp:

while game 
clear color bit
handle keys
aSYS.DO_FADE();
SDL_GL_SwapBuffers();

The only thing i can see , is a white screen -__-..WHY?!
Advertisement
a. In your loop, you're setting the clear colour but not actually clearing the buffer on each iteration.
b. You're not swapping the buffer on each iteration, so you'll only see the final colour (ie. white).
c. glLoadIdentity in the loop isn't going to do anything useful.
d. It'll probably happen WAYYYY to fast since you're not doing any delay.
e. Why not use a full screen quad and fade that from fully transparent to fully opaque?
f. It may be what you intend, but the DO_FADE is going to block the rest of the application until the fade is finished (e.g. you won't be able to fade out over the top of a game that is running).

- Thomas Cowellwebsite | journal | engine video

Quote:Original post by cow_in_the_well
a. In your loop, you're setting the clear colour but not actually clearing the buffer on each iteration.
b. You're not swapping the buffer on each iteration, so you'll only see the final colour (ie. white).
c. glLoadIdentity in the loop isn't going to do anything useful.
d. It'll probably happen WAYYYY to fast since you're not doing any delay.
e. Why not use a full screen quad and fade that from fully transparent to fully opaque?
f. It may be what you intend, but the DO_FADE is going to block the rest of the application until the fade is finished (e.g. you won't be able to fade out over the top of a game that is running).


I've changed the code to this:
for (GLfloat i=0.0f;i<255.0f;i++){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef( 0,0, 0 );glBegin( GL_QUADS );glColor4f(i,i,i,i);glVertex3f( 0,0,0 );glVertex3f( SCREEN_WIDTH, 0,0 );glVertex3f( SCREEN_WIDTH, SCREEN_HEIGHT,0 );glVertex3f( 0,SCREEN_HEIGHT, 0 );glEnd();SDL_GL_SwapBuffers();}

But what im getting is this :
whydamnithc3.jpg
http://img388.imageshack.us/img388/743/whydamnithc3.jpg

Quote:Original post by cow_in_the_well
f. It may be what you intend, but the DO_FADE is going to block the rest of the application until the fade is finished (e.g. you won't be able to fade out over the top of a game that is running).

Its just what i want !the fade effect will show up after some special events!
My main code is actually this :
if (aSYS.effect==true)
{
aSYS.DO_FADE();
}
Colors when using floating point values range from 0 to 1, not 0 to 255. Either loop values from 0 to 1, or use glColor4ub (which use the range 0, 255). And if the white quad doesn't cover the whole screen, then SCREEN_WIDTH and SCREEN_HEIGHT doesn't correspond to the viewport coordinate system's width and height, so check your projectio and modelview matrix.
Thanks bob , the fade function works now..
But still im having problem with quad's position..


Here's the open gl init function :
glMatrixMode(GL_PROJECTION);glLoadIdentity(); glOrtho(0,SCREEN_WIDTH,SCREEN_WIDTH,0,-1,1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();



And the working fade function:
for (GLfloat i=0.0f;i<256.0f;i++){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef( 0,0, 0 );glLoadIdentity();glBegin( GL_QUADS );glColor4ub(i,i,i,i);glVertex3f( 0,0,0 );glVertex3f( SCREEN_WIDTH, 0,0 );glVertex3f( SCREEN_WIDTH, SCREEN_HEIGHT,0 );glVertex3f( 0,SCREEN_HEIGHT, 0 );glEnd();SDL_GL_SwapBuffers();SDL_Delay(1);}

....any ideas/??


edited:
My desktop resolution is setted to 1024x768..the game is setted to 800x600.
The weird thing is , that if i set the game's resolution to 1024x768 everything's fine..
Quote:Original post by 3Dgonewild



edited:
My desktop resolution is setted to 1024x768..the game is setted to 800x600.
The weird thing is , that if i set the game's resolution to 1024x768 everything's fine..

Also , the quad is at the right position in WINDOW MODE..
I've edited the open gl init function a little:

    GLfloat _div = ( GLfloat ) SCREEN_WIDTH / ( GLfloat )SCREEN_HEIGHT;    glViewport( 0, 0, ( GLint ) SCREEN_WIDTH , ( GLint )SCREEN_HEIGHT );glMatrixMode(GL_PROJECTION);glLoadIdentity();       gluPerspective( 45.0f, _div, 0.1f, 100.0f );glMatrixMode(GL_MODELVIEW);glLoadIdentity(); 


And now , i cant draw 2d objects...just 3d..
Quote:Original post by 3Dgonewild
I've edited the open gl init function a little:

*** Source Snippet Removed ***

And now , i cant draw 2d objects...just 3d..

If you insist on setting the projection matrix just once at startup, then yes, it will be dificult to mix them. But nothing stops you from changing it while the program is running. While drawing, set projection matrix to perspective and draw the 3D object. Change it to orthographic and draw the 2D objects. Repeat as many times as you like.
Quote:Original post by Brother Bob
Quote:Original post by 3Dgonewild
I've edited the open gl init function a little:

*** Source Snippet Removed ***

And now , i cant draw 2d objects...just 3d..

If you insist on setting the projection matrix just once at startup, then yes, it will be dificult to mix them. But nothing stops you from changing it while the program is running. While drawing, set projection matrix to perspective and draw the 3D object. Change it to orthographic and draw the 2D objects. Repeat as many times as you like.


Thanks Supporter(!) Bob !! i didnt knew that!
And for one more time , thanks everyone for helping me! i've done great progress!!!(i thought that i had no chance in 3d programming , but with hard work and asking experts i gained much experience xD !!!!!!!!)

This topic is closed to new replies.

Advertisement