PDA

View Full Version : Strange bug with glColor*f()



ti83magic
05-06-2009, 12:37 PM
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

dletozeun
05-06-2009, 01:05 PM
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.

ti83magic
05-06-2009, 01:27 PM
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;

http://img8.imageshack.us/img8/8827/blackoverlay.th.png (http://img8.imageshack.us/my.php?image=blackoverlay.png) - Black overlay

http://img15.imageshack.us/img15/4449/blueoverlay.th.png (http://img15.imageshack.us/my.php?image=blueoverlay.png) - Blue overlay

http://img147.imageshack.us/img147/9440/whiteoverlay.th.png (http://img147.imageshack.us/my.php?image=whiteoverlay.png) - '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 :D

Tony

dletozeun
05-06-2009, 01:42 PM
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?

ZbuffeR
05-06-2009, 01:45 PM
> 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.

ti83magic
05-06-2009, 02:21 PM
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?

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




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

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

dletozeun
05-06-2009, 02:30 PM
Great I was far from it! :)



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. :)

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

ti83magic
05-09-2009, 03:48 PM
I see... I have much yet to learn about OpenGL, hehe. Thank you for the help!

Tony