# Antialiased text rendering (D3DX9)

This topic is 3479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I can't figure out how to render text at a decent quality, with D3DX. I'm creating the font like this:
// Unmanaged C++
D3DXCreateFont(d3dDevice, pxSize, 0, (bold ? FW_BOLD : FW_NORMAL), 0, italics,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE,
fontface, &(pFont->d3dxfont));


Then I render it with:
    m_pD3DXSprite->Begin(D3DXSPRITE_OBJECTSPACE|D3DXSPRITE_ALPHABLEND);

font.d3dxfont->DrawText(m_pD3DXSprite, text, -1, &rect, format, color);
m_pD3DXSprite->End();


The result looks pretty bad compared to drawing text in MS Paint: Any ideas what I'm doing wrong? Why is the text coming out so knobbly and aliased even with ANTIALIASED_QUALITY set? Thanks for any help.

##### Share on other sites
Try CLEARTYPE_QUALITY or PROOF_QUALITY for the Quality parameter. They should produce even better results. Check out the full description here.

##### Share on other sites
Ah, thanks. Cleartype quality improves it - but it's still nowhere near as nice as with MS Paint. Is it not possible to render text at that quality in DirectX?

Here's how it looks now. Note CLEARTYPE_QUALITY is better, but still not as good as MS Paint.

##### Share on other sites
You'll get superior results to anything that GDI can produce if you leverage the GPU in your favor. The best way to do that is described in a paper by Jim Blinn and Charles Loop: Resolution Independent Curve Rendering using Programmable Graphics Hardware

##### Share on other sites
Interesting link legalize, but surely I would be reinventing the wheel? Presumably MS Paint calls some GDI function that looks lovely, so why can't I get D3DX to do it that nicely?

##### Share on other sites
GDI doesn't natively support alpha. Using GDI directly to draw text would be slow. ID3DXFont works by grabbing glyphs from GDI and rasterizing them once using GDI into a texture. The texture is then used for drawing the text, but it isn't re-rasterized at different sizes. GDI re-rasterizes in software every time you change a parameter that controls font size (but not glyph spacing). Reproducing all the font subtlety behaviors in ID3DXFont is tedious and can also impact performance. ID3DXFont doesn't promise to get the same quality as GDI font rendering; it does remarkably well considering. If you need exact GDI quality output, then I'd suggest that you use a system memory surface that is compatible with GDI, render text to that and then make a billboard texture from that surface yourself and draw the text with the billboard.

However, all of that is going to be slower and at worse quality than the method presented by Blinn and Loop in that paper. I would not be surprised if you see GDI's text rendering ultimately replaced with the Blinn and Loop algorithm, or a variant of it, at some point in the future as Vista and subsequent releases of Windows leverage the GPU more and more for desktop operations.

##### Share on other sites
Quote:
 Original post by legalize[...] If you need exact GDI quality output, then I'd suggest that you use a system memory surface that is compatible with GDI, render text to that and then make a billboard texture from that surface yourself and draw the text with the billboard.

Either that or you can construct a bitmapped font altogether if you can stick with one single size. Then you would have full control over the quality of characters.

##### Share on other sites
Quote:
Original post by staaf
Quote:
 Original post by legalize[...] If you need exact GDI quality output, then I'd suggest that you use a system memory surface that is compatible with GDI, render text to that and then make a billboard texture from that surface yourself and draw the text with the billboard.

Either that or you can construct a bitmapped font altogether if you can stick with one single size. Then you would have full control over the quality of characters.

This is what ID3DXFont does.

##### Share on other sites
Ah, yes, sorry. I was going more along the line of using an external program or way to create a high quality bitmap font specifically for the situation. It could be some work but if quality is a high priority it might also be worth it.

Interesting article by the way legalize.

##### Share on other sites
Quote:
 Original post by legalizeID3DXFont works by grabbing glyphs from GDI and rasterizing them once using GDI into a texture.

I don't understand why this still results in poor quality text - doesn't it render each glyph with GDI (which should render glyphs at the same quality MS Paint does), then move them to a texture and render strings from the texture? As in, effectively render it to a bitmap font in video memory. Why would this result in poor quality text rendering, if it's using GDI?

So is there no way for ID3DXFont to achieve MS Paint quality? How do commercial games go about this? And is there some kind of library someone's already written to do text rendering in decent quality? I set out to draw text to the screen, and wasn't really expecting to have to design my own entire font rendering engine...

##### Share on other sites
Quote:
Original post by AshleysBrain
Quote:
 Original post by legalizeID3DXFont works by grabbing glyphs from GDI and rasterizing them once using GDI into a texture.

I don't understand why this still results in poor quality text - doesn't it render each glyph with GDI (which should render glyphs at the same quality MS Paint does), then move them to a texture and render strings from the texture? As in, effectively render it to a bitmap font in video memory. Why would this result in poor quality text rendering, if it's using GDI?

It uses GDI to render glyphs, but doesn't use GDI for kerning inter-character spacing, inter-line spacing, etc. It handles all the character and line spacing by drawing quads. I think there may be additional issues related to scaling. Its also possible that there are bugs in ID3DXFont; its one of the classes that has churned the most in D3DX over the years.

Quote:
 So is there no way for ID3DXFont to achieve MS Paint quality?

Its not a question of whether ID3DXFont can achieve it, but can you achieve it. The answer is yes, you can achieve higher quality by using GDI to rasterize the text any way you want into a surface. However, it won't be the most performant. Quality and performance often compete and this is no exception.

Quote:

They use bitmapped fonts that are created specifically for their game. Games are not hyper-obsessed with typographic quality; they don't give you a GDI font browser to pick the font for your HUD. You get what they give you and nothing else.

##### Share on other sites
One other thing to check - are you absolutely sure that your backbuffer is the same size as your window client area? If you're not using AdjustWindowRectEx() when you create your window, then the backbuffer and window client area are probably different sizes (CreateWindowEx() takes a window size, not client area size), which will mean your backbuffer is being stretched onto the window, which could cause artefacts.

##### Share on other sites
By the way if you want to implement your own font rendering code you'll probably want to use GetGlyphOutline(). Despite the function name it can get you a bitmap for a character as well as a bezier curve outline.