1
votes

I want to draw an opaque texture (legacy openGL - so no shaders) on a quad with a transparency value so it blends to a certain extend with the background, making the quad transparent.

What's the trick? Searching for drawing stuff with transparency mostly results in tutorials for textures that already are transparent by enabling and setting the blend function ...

EDIT: considering the commentaries, it seems like this is it:

drawTranparentSprite(TextureInformation t, ImageInformation i){
    glBindTexture(t.target, t.textureID);
    glBegin(GL_QUAD);
    {
        glColor4f(1,1,1,i.alpha)
        glTexCoord2d(t.x, t.y);
        glVertex3d(i.x, i.y, 0);
        glTexCoord2d(t.x, t.y + t.h);
        glVertex3d(i.x, i.y + i.h, 0);
        glTexCoord2d(t.x + t.w, t.y + t.h);
        glVertex3d(i.x + i.w, i.y + i.h, 0);
        glTexCoord2d(t.x + t.w, t.y);
        glVertex3d(i.x + i.w, i.y, 0);
    }
    glEnd();
}

Though you are welcome to correct me ... (TextureInformation and ImageInformation are here random abstractions of things you need to handle glTexture and random image data to draw to a surface ...)

Also the programming language is completly irrelevant here ...

1
Blending is the trick. By default OpenGL just overwrites the contents of the framebuffer with anything new. But if you use a blend function like glBlendFunc (GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA), it will blend the new (source color) with the old (destination color) -- modulated by the new (source alpha). - Andon M. Coleman
The thing is, both source and target alpha value are 1 ... So how do I set that alpha value per vertex? - salbeira
@salberia: You can set a per-vertex alpha value to modulate with your opaque texture to produce the alpha component in the fixed-function pipeline. glColor4f (1.0f, 1.0f, 1.0f, 0.5f), for instance. Though this may require you to enable GL_COLOR_MATERIAL. You may also be using vertex arrays, it is not clear from the question - but what you want is either material or per-vertex alpha. - Andon M. Coleman
No, I am doing drawing by a per-quad basis (drawing sprites) - I want to integrate a "draw transparent image" into my library - salbeira

1 Answers

3
votes

I have modified your function with the necessary states to do what you want... you can figure out where to enable/disable these states on your own, but they must be present before you try to draw this polygon.

drawTranparentSprite(TextureInformation t, ImageInformation i){
  /**
   * Additions: Setup state necessary for blending and per-vertex color.
   *
  **/
  glBlendFunc  (GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
  glEnable     (GL_BLEND);
  glEnable     (GL_COLOR_MATERIAL);

  // Moved this out of the begin/end block to make it more obvious that it is constant...
  glColor4f    (1.0f, 1.0f, 1.0f, i.alpha);

  glBindTexture(t.target, t.textureID);
  glBegin(GL_QUAD);
  {
    glTexCoord2d(t.x, t.y);
    glVertex3d(i.x, i.y, 0);
    glTexCoord2d(t.x, t.y + t.h);
    glVertex3d(i.x, i.y + i.h, 0);
    glTexCoord2d(t.x + t.w, t.y + t.h);
    glVertex3d(i.x + i.w, i.y + i.h, 0);
    glTexCoord2d(t.x + t.w, t.y);
    glVertex3d(i.x + i.w, i.y, 0);
  }
  glEnd();
}