Page MenuHome

Filtered fonts in the game engine
Closed, ResolvedPublic

Description

This patch makes anti-aliasing of fonts in the game engine better. It does this by generating mipmaps for the glyphs, and enabling linear filtering. This is important for games because a single rendering of text can be viewed at different resolutions and even with perspective.

This patch also fixes a z-order bug with overlapping text; see screenshots.

Details

Type
Patch

Event Timeline

Attaching a (hopefully) Tortoise-friendly version of the patch.

Trying the svn conversion thing again.

I like the aliasing, it's looking great here.

Now for the z-order bug:
I still have some glitches in some tilted texts (extreme ones, no real production examples yet). See alias_problem.png.
But I believe your twist code solves the most common case of texts parallel to the camera overlapping each other.

assigning to Diego (and deleting the git/wrong svn fils)

Hello!

The next three or four days are really busy for me, but as soon I have a little of time I will check the patch (I think for the next tuesday), sorry but a lot of work :(

Hi Dalai,

Actually I think what you're seeing is not a z-order issue. There are two separate problems:
1. The way I understand it, the glyph textures map into a larger image. The white artefacts you see on the edge of the glyphs in "AntiAlias" are because the higher mipmap levels are bleeding in characters from other parts of the base image. There's a .pad member of GlyphCache; maybe if we increase that there will be greater spacing between the glyphs and it could solve this issue.

2. The remaining aliased edges of the glyphs, e.g. the top edge of the T, are due to the texture being drawn right to the edge of the quad. In that case, mipmapping and filtering won't help. Enabling FSAA should fix it. Alternatively we could adjust the UV mapping to give a bit of spacing around the glyph in its quad. But we would also need to adjust the spacing between glyphs to compensate for that.

As you say, the second issue doesn't show up when the text is aligned to the camera.

Attaching a new version of the patch (blf_aa_2_svn.patch) that fixes issue 1 in the previous comment. In the new version, the padding around glyphs is increased when mipmapping is in use.

Added files to demonstrate the difference padding has on the artefacts:
- ge_text_nopadding.png - made with the original patch.
- ge_text_padding.png - made with the new patch (blf_aa_2_svn.patch)
- text.blend - the test file.

blf_aa_3_svn.patch: only "twist" the leading edge of each glyph. This prevents the trailing edge from disappearing when the text is coplanar with a face.

Hi Diego, Dalai showed me the conversation you two had in IRC the other day. You are right about the padding, and the twist:
- Extra padding is required for mipmaps, because otherwise the letters bleed into neighbouring glyphs.
- The twist stops z-fighting when two glyphs overlap.

I tried making the twist optional, but I think it makes the code more complicated than it's worth - especially if Z is compressed in the normal GUI matrix. The twist code should be pretty fast. So it's still not optional in this patch, but I can add the option back if you want.

Hi Alex,

As I say to Dalai, the twist is ok, the Z is always clip when the UI is drawing in 2D, there is no problem there!.
I will talk with Dalai today, I think the patch is ready for trunk, sorry that take so long time!! and thank for the hard work!! :)

Hi Diego,

Thanks for that! ... I'd like to commit this myself, if that's ok :)

Hi Alex,

Sure! go ahead! :)

Patch committed, r43876 (version 3 of the patch, see above for details).

There is something still strange with this patch (the committed one).
The problem for me happens when the text object is parented by an object that is parented to another, ...

Check this out: http://www.pasteall.org/pic/25805 (it's not a typo, the j can't be seen).
Looking at the KX_FontObject.cpp code I tried to fix this with this patch:
http://www.pasteall.org/28797/diff

This fix it, but all the texts seem over blurred (http://www.pasteall.org/pic/25806) AND the text is missing as well: http://www.pasteall.org/pic/25807

For my test (real file actually) I'm using a orthographic camera

Hmm, didn't we figure out that was something slightly different? I'm not sure how this commit would affect that. Text that sits exactly on a plane will probably show issues like this, even with twisting, depending on the scene setup. Suggestions:
- Try moving the near clipping plane of the camera closer to the text.
- Move the text a bit towards the camera.

Actually, we had the same problem with non-text objects in the game we made at the game jam. The solution was to bring the objects closer to the camera.

Can you post a test file?

Alex please see attached file (packed font Papyrus included) - blf_bug.blend
(If you need I can send some screenshots)

With my patch (to use Nodal global Scaling instead of m_size[0]) makes both lines to display all letters and are the same size. They are blurry though so there is yet something else wrong.

As discussed in IRC, I can't reproduce the missing J in blf_bug.blend with an nVidia card. But the bug *is* present when using my ATI card. Attaching a screenshot (missing-j.png) - the left image is with this patch disabled; the right shows the patch enabled: the text is blurry and the J is missing.

Note that the blurriness is expected in this case. The text simply has the wrong resolution, as shown in the image on the left: without the patch, it's blocky. If that is a bug, then it's unrelated to this patch. The missing J, however, is a concern. And the fact that it only shows up on ATI cards makes me think this will be difficult to fix.

Dalai Felinto (dfelinto) closed this task as Resolved.Feb 10 2012, 7:20 AM

patch reverted on rev. 44011. Let's have it bright and shinning after 2.62 is out.

I uploaded my modified version of this patch (blf_aa_3_HG1.patch).

This patch should fix flowing problems:
1. Missing J on ATI cards.
2. Increase the letter twist to reduce the clipping problem.
3. Fix mipmap crash on non GLSL cards.

New patch (blf_aa_4_HG1.patch).
Fixed implicit type conversion.

@Thomas Szepe (hg1), how does your new version of the patch fix the issue with the J? I can't see what the difference is in the code. Was it fixed by another commit?

I think the J was clipped by the ATI card. So the increased twist (0.0002 to 0.0004) has fixed the problem.

Hello!! there!

I have a machine with an ATI card here, so tomorrow I'm going to update my git repo and try the patch!!

Cool, here's a new version that applies against HEAD. Note that this reverts commit 7c6d52e which stopped initialisation of the font image; initialisation is actually required for glyph padding when using mipmapping. Thanks to @Campbell Barton (campbellbarton) for assistance with this update.

And this version adds alpha clipping, reducing artefacts for kerned (overlapping) letters.

In the new patches there is a printf left.

Oops, you're right - but it's only printed when creating a cache (very infrequently). It will be removed before committing, but you can test with the patch as-is.