5
votes

I want to draw text with OpenGL using FreeType and to make it sharper, i generate the font texture from FreeType for each mipmap iteration. Everything works quite fine except for one thing. When i do this:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

OpenGL chooses the nearest mipmap according to the size of the text, but if available sizes are 16 and 32, and i want 22, it picks 16, making it look terrible. Is there a way to set it so that it instead always picks the nearest larger mipmap?

I know i can do this while rendering the text to set the mipmap level manually:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, (int) log2(1/scale));
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, (int) log2(1/scale));

But is that effective? Doesn't that kind of drop the need for using mipmaps completely? I could just make different textures and choose one according to size. So is there a better way to acomplish this?

2
"I want to draw text with OpenGL using FreeType and to make it sharper, i generate the font texture from FreeType for each mipmap iteration." This is not a good idea. The way that FreeType renders smaller text is not a way that works well for mipmap interpolation. That's why your GL_LINEAR_MIPMAP_LINEAR text looked terrible; FreeType's small text hinting made the smaller text look wrong. It would be better to just have a certain set of text resolutions that you work with and just use them.Nicol Bolas
"That's why your GL_LINEAR_MIPMAP_LINEAR text looked terrible." Rasterized text that is magnified looks worse than similar but larger rasterizations that are minified. I believe that's the biggest reason why OP's approach looks bad.nmr
Good discussion of mipmap related blur: bgolus.medium.com/…nmr

2 Answers

4
votes

You can use GL_TEXTURE_LOD_BIAS to add a constant to the mipmap level, which could help you accomplish this (for example, by always choosing the next higher mipmap level). However, it could simply be that mipmap-nearest isn't ideal for this situation. If you're always showing your texture in screen-space and know exactly how the font size corresponds to the pixels on the screen, then mipmapping may be too much machinery for you.

3
votes

Have you tried GL_LINEAR_MIPMAP_LINEAR as the GL_TEXTURE_MIN_FILTER parameter? It should blend between the two nearest mipmaps. That might improve the appearance.

Also, you could try using the texture_lod_bias extension which is documented here: http://www.opengl.org/registry/specs/EXT/texture_lod_bias.txt