Blending and enable/disable effect

Started by
3 comments, last by mikeman 19 years, 6 months ago
I try to use the blending functionalities in order to make a enable/disable effect on my buttons of my GUI. Here's some code that might be better to explain what's going on.

void CCheckBox::RenderComponent () {

  if ( !bEnabled ) {
    glEnable ( GL_BLEND );
    glBlendFunc ( GL_SRC_ALPHA, GL_ONE );
    glDepthMask ( GL_FALSE );
    glColor4f ( 0.5f,0.5f,0.5f,1.0f );
  }

  if ( bChecked ) {
    glBindTexture ( GL_TEXTURE_2D, ulChecked );
  }
  else {
    glBindTexture ( GL_TEXTURE_2D, ulUnchecked );
  }

  glPushMatrix ();
  glTranslatef ( lPosX, -lPosY, 0 );
  glBegin ( GL_QUADS );
    glTexCoord2f ( 0.0f, 0.0f );
    glVertex3f ( 0.0f, -lHeight, 0.0f );
    glTexCoord2f ( 1.0f, 0.0f );
    glVertex3f ( lWidth, -lHeight, 0.0f );	
    glTexCoord2f ( 1.0f, 1.0f );
    glVertex3f ( lWidth, 0.0f, 0.0f );
    glTexCoord2f ( 0.0f, 1.0f );
    glVertex3f ( 0.0f, 0.0f, 0.0f );
  glEnd ();

  glPopMatrix();

  if ( !bEnabled ) {
    glDepthMask ( GL_TRUE );
    glDisable ( GL_BLEND );
  }

}

Now I have two checkboxes. When both are enabled they look right, but when one is disabled and the other one enabled, they both look the same way, which means they look disable. When I replace a part of the code by :

void CCheckBox::RenderComponent () {

  glEnable ( GL_BLEND );
  glBlendFunc ( GL_SRC_ALPHA, GL_ONE );
  glDepthMask ( GL_FALSE );
  if ( !bEnabled ) {
    glColor4f ( 0.5f,0.5f,0.5f,1.0f );
  }
  else {
    glColor4f ( 1.0f,1.0f,1.0f,1.0f );
  }
...
  glPopMatrix();

  glDepthMask ( GL_TRUE );
  glDisable ( GL_BLEND );

}

everything is working like it should. The only thing is that I cannot understand why the first code doesn't work... Someone an idea ? I don't believe that this beahiour is due to the fact that I share the same texture (unsigned int) which is a static member of my CCheckBox class... Thx in advance
Advertisement
The problem is quite simple. You probably first render the blended item which then appears like with no blending. Try to reverse the order you draw the checkboxes.
Well, in the first code, you set the color to (0.5,0.5,0.5,1) if it's not enabled, but you don't set it to (1,1,1,1) if it is enabled. So, if you draw a disabled box, the color stays at gray and all the other boxes, whether enabled or not, are drawn gray. This doesn't happen in the second code, because you have an else statement so you handle both cases.

One thing I don't understand is why do you use blend. I don't get it why it is needed, and why you use it only if the box is disabled. But anyway, you know better what you want to do.
Thx for your answer mikeman. Now I understand better what's going on. In fact I do not need blending at all... I removed all this blending stuff and it's working fine. I guess, I need to read this chapter on blending again :).
Glad I could help.

My guess is that you wanted to modulate the current color with the texture, but you don't need blending for that. You need to set TexEnv to GL_MODULATE. But this is the default value anyway, so the code works as is.

This topic is closed to new replies.

Advertisement