問題描述
如何在 Direct3D 11 上使用字體文件渲染文本? (How can I render text using font files on Direct3D 11?)
我已經閱讀了很多關於此的問題,但它們並不能滿足我想要做的事情。我正在嘗試在我的應用程序中使用 TTF 文件作為文本字體,我想使用 Direct Draw,但是 tutorial 來自 Microsoft 網站,僅說明瞭如何將其與 Direct2D 一起使用。我應該如何從這個文件加載數據並使用這個文件的字體為我的 Direct3D 應用程序渲染文本?我還閱讀了有關 AddFontResourceExA()
函數的信息,但我沒有找到任何關於如何使用它的內容。我真的迷路了,因此感謝您的幫助。
參考解法
方法 1:
There are basically two approaches for rendering text on a Direct3D 11 Render Target / Texture.
Rendering using a 'sprite sheet'. Here you capture the font at a particular resolution and generate a texture from it. Then you use the texture to render the glyphs as textured triangles. This is very fast and inexpensive to render, but does not scale to arbitrary resolutions (you can capture the 'sprite sheet' at multiple point sizes to get some scaling) and does not work well with "CKJ" languages due to the large size of the fonts. For an example of this, see
SpriteFont
in the DirectX Tool Kit. This is what legacy D3DX9/D3DX10 did as well.Rendering using vector fonts directly. Here you have some kind of library that generates triangles 'on‑the‑fly' from the "TrueType" vector font data. This is what Direct2D+DirectWrite is designed to do. You can use interop with Direct3D 11 surfaces, but essentially you are using DirectWrite ‑> Direct2D ‑> shared texture. Then you draw the shared texture with Direct3D as a 'sprite'. This is more complicated to setup, but results in arbitrary resolutions scaling, support for large character set fonts, and handles complex writing systems.
(by Rafael Ferreira、Chuck Walbourn)