Search code examples
openglsdlrenderbmp

Rendering alpha transparency in geometry and figuring out alpha color in bitmap - OpenGL


This is a two part question. I am rendering some BSP geometry and some of the textures have an alpha color which should be invisible.

So the first step would be figuring out the alpha color so I can correctly render it. I uploaded a sample bitmap:

Image

As you can see, it is a railing texture where RGB(160,158,148) is the color that should be invisible. I can figure this out just by looking at it. The problem is, how do I find this value in the file?

So that is step one. The second is an OpenGL issue where I am rendering a bunch of geometry with vertex arrays. How do I accommodate for the fact a color needs to be removed from the texture when rendered? Do I need to render this in a specific order? Also, I should mention I am using SDL if that helps make any of this a bit easier.

Thank you both for fantastic answers. I wish I could pick both of you...


Solution

  • and some of the textures have an alpha color which should be invisible.

    This is actually called a keying color.

    The alpha channel is an additional channel, adding to the "normal" RGB channels. When alpha blending is used, one uses the alpha channel value as opacity.

    I can figure this out just by looking at it. The problem is, how do I find this value in the file?

    If a color key is used, one uses either some specific color, used for all images, or stores the keying value separately, loading it with the image file. Another common technique is to use the value of the upper left pixels as keying color.

    How do I accommodate for the fact a color needs to be removed from the texture when rendered?

    By preparing the image data with a color key generated alpha channel:

    for(i = 0; i < pixelcount; i++) {
        if( source[i].r == key.r && source[i].g == key.g && source[i].b == key.b ) {
            dest[i].r = dest[i].g = dest[i].b = dest[i].a = 0;
            continue;
        }
        dest[i].r = source[i].r;
        dest[i].g = source[i].g;
        dest[i].b = source[i].b;
        dest[i].a = 255;
    }
    

    Update: In the case of color key generated alpha channel, one can use alpha testing instead of blending. The nice thing with alpha testing is, that it doesn't requires to depth sort the geometry prior to drawing.

    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.5);
    

    The ideal way would be to use a image file format with alpha channel support (PNG, TGA, OpenEXR, …), and not using BMPs (actually the BMP file format would allow for a 4th channel, and in fact I once wrote my own BMP loader with alpha channel support, and a Photoshop export plugin, many years ago).