1

I'm loading a font from a TGA texture. I generate the mipmap using the gluBuild2DMipmaps() function.

When the font has a certain size, it looks very good. But when it gets smaller, it gets darker and darker whenever it reaches a new mipmap level.

This is how I create the texture:

void TgaLoader::bindTexture(unsigned int* texture)
{
    tImageTGA *pBitMap = m_tgaImage;

    if(pBitMap == 0)
    {
        return;
    }

    glGenTextures(1, texture);
    glBindTexture(GL_TEXTURE_2D, *texture);

    gluBuild2DMipmaps(GL_TEXTURE_2D,
                        pBitMap->channels,
                        pBitMap->size_x,
                        pBitMap->size_y,
                        textureType,
                        GL_UNSIGNED_BYTE,
                        pBitMap->data);


    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}   

This makes the text look like this (it should be white):

enter image description here

Barely visible.

If I change the GL_TEXTURE_MIN_FILTER to basically ignore mipmaps (using GL_LINEAR for example), it looks like this:

enter image description here

I've tried different filter options and also tried using glGenerateMipmap() instead of gluBuild2DMipmaps(), but I always end up with the same result.

What's wrong with the code?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Beriol
  • 646
  • 1
  • 10
  • 25
  • 1
    In simple words, at automatic mipmap generation 4 texels (in a quad) are interpolated and stored to the next mipmap level with half resolution. This causes that the text gets darker and darker by each level. If you want to improve the result then you have to use textures with different size and text rendered in different sizes and you have to load this textures to the different mipmap levels. Or you use a technique like [Distance Fields](https://blog.mapbox.com/drawing-text-with-signed-distance-fields-in-mapbox-gl-b0933af6f817) – Rabbid76 Dec 14 '18 at 14:53
  • Would it be enough to make as many textures as needed dividing the font texture by 2 with a random image editor? So for example, if I start with a 512x512, scale down to 256x256 to make mipmap level 1, then 128x128 for level 2, and so on. – Beriol Dec 14 '18 at 14:58
  • 1
    [Generate your own miplevels using gamma-correct scaling](https://stackoverflow.com/a/832314/44729). Or use a [sRGB texture and `glGenerateMipmap()` will do it for you](https://stackoverflow.com/a/22352552/44729). – genpfault Dec 14 '18 at 14:59

0 Answers0