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 :slight_smile:

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.

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Result 2:
Alpha removed, but the red looks pale.

glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);

I am setting this option in the shader (a GLKBaseEffect)

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?

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.