Strange bug with glColor*f()

Hey hey,

I must have done something terribly wrong, hehe. I’m pretty new to openGL, so it’s all a matter of stumbling and getting up again, but here’s the del, bear with me. I’m writing a 2d game, using openGL to display the graphics. As a function of my graphicsengine, I wanted to draw a rect over the entire scene with any color and alpha value, so that I could dim the scene to black for example, or have an orange glow for levels in a vulcano.

Anyways, the fade to black works, simply using the following function I made:


////////////////////////////////////////////////////////////////////////////////
/// Draw a colored rectangle over the screen, with a specified alpha value.
///
/// \param Color The color of the overlay.
/// \param Alpha The alpha value the overlay should have.
/// \bug glColor4f seems to skip green, for some reason. 
////////////////////////////////////////////////////////////////////////////////
void cEffectManager::DrawOverlay(sColor Color, GLfloat Alpha) {
    // If it's 0, and therefore not visible, simply don't draw any overlay.
    if(Alpha==0.0f) {
        return;
    } // if
    
    glLoadIdentity();
    
    glBegin(GL_QUADS);
        glColor4f(Color.Red, Color.Green, Color.Blue, Alpha);
//        glColor4f(1.0f, 1.0f, 1.0f, Alpha);
        
        glVertex2f(0,
                   0);
        glVertex2f(g_Graphics->GetWidth(),
                   0);
        glVertex2f(g_Graphics->GetWidth(),
                   g_Graphics->GetHeight() );
        glVertex2f(0,
                   g_Graphics->GetHeight() );
    glEnd();

Fading to black works, as mentioned… but the fun (?) thing is, most other colors don’t. What happens is that the second parameter of glColor4f() is simply ignored completely. If I were to display a white square (the commented line in my function), it gives a pink overlay in my game… and (0,0,0) and (0,1,0) both display as black…

I have yet to isolate the cause of the bug, but I am at a loss here. Why on earth does openGL skip my green? I can’t seem to reproduce it on small scale…

Tony

When you say the fading to black is working you mean that the fullscreen quad is entirely blended with the background and turns to black?

How do you set up blending?

You need something like:

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

before drawing your quad.

then just disable it calling:

glDisable( GL_BLEND);

This way OpenGL will use the quad alpha value to blend the quad color with the background.

I use these blending settings:


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

Perhaps a picture will make my problem more clear;

- Black overlay

- Blue overlay

- ‘White’ overlay

As you can see, it works as long as I don’t want to use the green component. When I do though, it simply ignores green, and ONLY green, making my ‘white’ into the pink you can see in the image…

It’s not my sColor struct btw, which was the first thing I checked. Any idea what I could be doing wrong? Really breaking my head on this one :smiley:

Tony

And if you uncomment the glColor4f(1.0f, 1.0f, 1.0f, Alpha) line I suppose you still get a pink overlay?

Maybe it is something with the framebuffer pixel format. How is created the window? If you load a texture, does it look fine?

> I can’t seem to reproduce it on small scale…

It means somewhere you mess with the opengl state.
Is your drawOverlay() called last, right before the swapbuffers ?
You don’t mess with glColorMask do you ?
Make sure texturing is disabled when drawing your overlay.

Aye, that was indeed still pink :slight_smile: Textureloading and displaying all goes well, using glEnable(GL_TEXTURE_2D). And the next poster found the source of the problem!

I call drawOverlay just before the HUD (fps counter in this case). I did not yet touch colormasking, that will be the next step (if it is indeed what I think it is- transparent backgrounds with custom colors?). But the texturing, I had no idea… disabling it just before drawing the quads and enabling it again when drawing the (textured) text did indeed do the trick! Thanks for that, but… now I am wondering, WHY did enabling textures interfere with color generating, in such a way that only green is influenced?

Tony

Great I was far from it! :slight_smile:

WHY did enabling textures interfere with color generating, in such a way that only green is influenced?

Opengl is a state machine. So since texture mapping was enabled, it keeps track of the current texture bound the the 1st texture unit and the last given texture coordinates. In addition, by default Opengl blends the current color by the texture color. Consequently, it was reading the same texel and modulating its color by the current color over the entire quad rasterization. :slight_smile:

If you bind another texture or set different texture coordinates before drawing the overlaying quad you will probably obtain another color.

I see… I have much yet to learn about OpenGL, hehe. Thank you for the help!

Tony