Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: How to remove alpha channel and keep full color

  1. #1
    Newbie Newbie
    Join Date
    Dec 2012
    Posts
    1

    How to remove alpha channel and keep full color

    Hello,

    I am an OpenGL beginner. So please forgive me if I don't describe thing as you'd expect

    I have written a little font engine for iOS that essentially is rendering a string to a bitmap context, creates a bitmap from the context and then converts the bitmap into an OpenGL texture. It works fine, except when it comes to color.

    The texture that I am generating is a grayscale texture. Why? Because I have the code from a tutorial and it said that a RGBA texture would be a waste.

    So when I render my texture, I can remove the alpha channel and see a nice white text on the screen. In order to get it colored, I thought I just hand over some color attributes to the verteces that render the texture. But I end up with either one of two unsatisfying results.

    I am using OpenGL 2.0. My development platform is iOS.

    Result 1:
    Nice red color, but the alpha channel is opaque.
    Code :
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


    Result 2:
    Alpha removed, but the red looks pale.
    Code :
    glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);


    I am setting this option in the shader (a GLKBaseEffect)
    Code :
    effect.texture2d0.envMode = GLKTextureEnvModeModulate


    Is it even possible to color my grayscale texture in a way that the alpha channel is removed, but the colors turn out nicely? Or should I use RGBA instead when generating the texture in the first place?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,191
    Quote Originally Posted by redcoding View Post
    Is it even possible to color my grayscale texture in a way that the alpha channel is removed, but the colors turn out nicely? Or should I use RGBA instead when generating the texture in the first place?
    Yes.

    If you don't need full color, then "for uncompressed textures only", yes there's no reason to have RGB channels in your texture. Just have luminance (gray-scale).

    However, that doesn't mean you can't have an alpha value. This seems to be what you want here. Check out the GL_LUMINANCE_ALPHA8 (or GL_RG8 if you don't want to use legacy formats) formats. These have 2 8-bit channels -- one for luminance and one for alpha (16-bits/texel, vs. 32-bits/texel for RGBA8).

    That said, consider compressed texture formats as well. For instance DXT1, DXT5, LUMINANCE_ALPHA_LATC2 (or RG_RGTC2 if you don't want to use legacy formats). For the base maps of your texture, you've only got at most 2 colors per 4x4 block, so this should compress with high quality. DXT1 gives you effective 4-bits/texel compressed size, while the rest are 8-bits/texel compressed size.

    So yeah, use one of these, and then use either GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA blending, or (even better) even better use pre-multiplied alpha (GL_ONE, GL_ONE_MINUS_SRC_ALPHA blending) where you pre-multiply the color/luminance channels in the texture by the alpha value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •