FontRendering with FontAtlasData in alpha channel (glTexImage2D + FreeType-GL)

Hello guys,
I’d come to following problem:

  • Im using the freetype-gl library (https://github.com/rougier/freetype-gl)
  • I need to create a OpenGL texture out of a ftgl::texture_atlas_t*.
  • I used to do it with a depth of one byte (atlas = texture_atlas_new(512, 512, 1))
  • I used to upload it to OpenGL with glTexImage2D on the GL_RED channel.
  • But now I need the fontData to be in the alpha channel.
  • In the fragment-shader I want to have a sampler which returns me a vec4 (the pixelColor) [with rgb=1 & a=0-1 (fontData)]
  • But the problem is that I don’t actually know how to get the single channel data from freetype-gl into the alpha channel of the OpenGL-texture.
    (I guess I’m using the glTexImage2D(…) function in the wrong way.

I hope you’re able to find a solution to my problem, :slight_smile:
rnholzinger01

In the core profile a single-channel texture doesn’t have an alpha channel, only a red channel. You could use a 4-channel texture, but that wastes memory and you can’t upload just the alpha channel. In the compatibility profile you can use e.g. GL_ALPHA8 as the internal format to generate a texture with only an alpha channel (the other channels will be zero).

In 3.3 (core or compatibility profile) or with the ARB_texture_swizzle extension you can use swizzling, e.g.:


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);

[QUOTE=GClements;1292575]In the core profile a single-channel texture doesn’t have an alpha channel, only a red channel. You could use a 4-channel texture, but that wastes memory and you can’t upload just the alpha channel. In the compatibility profile you can use e.g. GL_ALPHA8 as the internal format to generate a texture with only an alpha channel (the other channels will be zero).

In 3.3 (core or compatibility profile) or with the ARB_texture_swizzle extension you can use swizzling, e.g.:


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);

[/QUOTE]

As you’ve mentioned it’d be stupid to use a four channel texture since it takes a lot of memory…
so I’ll have to use the red channel.

But thanks for pointing out, that there’s now ‘pretty’ solution with a single-alpha-channel texture.:slight_smile: