This topic is 3974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm using OpenGL and SDL. I'm trying to draw a transparent black rectangle over the screen to make it fade to black, but it refuses to appear transparent. It's just all black. Here's the color function I used: glColor4ub( 0, 0, 0, factor ); Factor goes from 0 to 255 and nothing happens. I also tried just 0 and 1, and it didn't work. It didn't work when i tried glColor4f from 0.0 to 0.1 either. Am I doing something wrong? Any help is appreciated.

Share on other sites
You need to enable alpha blending.

glBlendFunc( GL_SRC_ALPHA, GL_ONE );glEnable( GL_BLEND );

You may also need to turn depth testing off (and in that case, make sure the quad is the last thing drawn). But other than that, you have the right idea (fading from 0 to 255).

Share on other sites
Wouldn't you actually want glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)? That will let you fade to any color or texture.

Share on other sites
I've been checking out some OpenGL reference pages and they don't give much of an explanation. If it makes a difference, I'm using glOrtho to render in 2D, and everything has the same depth (0.0f).

Share on other sites
Did you enable blending and set the blend functions?

Share on other sites
glEnable( GL_ALPHA_TEST );
glAlphaFunc( GL_GREATER, 1.0 );

glEnable( GL_BLEND );
glBlendFunc( GL_ONE, GL_ONE );

Blending is enabled, but I have no idea which alpha function to use.

Share on other sites
Use glBlendFunc( GL_SOURCE_ALPHA, GL_ONE_MINUS_SOURCE_ALPHA );
(the exact syntax is available in the ogl reference, but something along those lines)
This means that for the rectangle, you use the alpha specified by glColor, and for what is already on screen, you use one minus that. That means the final color is computed to be rectangle_color * x + existing_color * (1-x). Over time, let x go from 0 to 1, and the effect is complete...

Share on other sites
Also, remove "glEnable( GL_ALPHA_TEST )" and "glAlphaFunc( GL_GREATER, 1.0 )".

Share on other sites
Moving to OpenGL.

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
13
5. 5

• 9
• 18
• 11
• 9
• 17
• Forum Statistics

• Total Topics
632605
• Total Posts
3007371

×