3
votes

I'm working with sprite animation (OpenGL + C++). I have some trouble working with blending.

I'm trying to load an image with a black background and draw it over another texture without a block of black appearing around the image. The image has Alpha channel and blending is enabled.

I tried playing with different blending functions. I either end up with a blocky image or a translucent image.

I know I can do it if I replace the black background with a transparent color instead using an image editing software, but I would like to get this working without that and without using an image mask.

An example to better understand my situation.

The image & Image over texture done incorrectly:

enter image description here

The way I want it to be:

enter image description here

Here is a bit of code that I'm using. I picked out what seemed to be most relevant since a lot of code is spread out over a few classes.

    glEnable(GL_DEPTH_TEST);
    ....
    ....
    glEnable(GL_TEXTURE_2D);
    /*Drawing the image with black background first*/
    glBindTexture(GL_TEXTURE_2D, blockImage);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        //drawing code
        ...
    ...
    ...
    /*background texture is drawn last*/
    glBindTexture(GL_TEXTURE_2D, bgImage);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glBlendFunc(GL_ONE, GL_DST_ALPHA);
    //drawing code
        ...
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);

I don't really need the actual code to do this. A small explanation of the logic would suffice (order of drawing and blending).

1
A representation of the alpha channel of the image would be helpful. - Luca
Also your current set of code!!! I have code that does exactly what you are asking to do, done with OpenGL ES on an iPhone / iPad app. I just need to see what you already have!!! - trumpetlicks
"I know I can do it if I replace the black background with a transparent color instead using an image editing software, but I would like to get this working without that" Are you trying to say that the black background is not 100% transparent in the source image? OpenGL doesn't support color-keying if that's what you're trying to do. - Tim
Ah yes. That is the right term. I want to be able to select a color-key to use for transparency. Is there any other way to do it? - Nathan Morgan

1 Answers

1
votes

OpenGL fixed-function pipeline has no builtin support for color keying. If you want to do it, you can write a shader to test the fragment's color and use the discard operation.

If you want to do this in fixed-function you'll have to properly use the alpha channel (make all the black area alpha = 0 in pre-processing).