C++ 如何在 DirectX 11 中绘制文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5708860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:42:25  来源:igfitidea点击:

How do you draw text in DirectX 11?

c++directxdirectx-11

提问by Darkenor

In DirectX 10 you could use the font interface provided by D3DX10. In DirectX 11 you are supposed to use DirectWrite. But it looks like DirectWrite doesn't speak natively to Direct3D? Is there something basic I'm missing? How do you draw simple text with DirectX 11?

在 DirectX 10 中,您可以使用 D3DX10 提供的字体接口。在 DirectX 11 中,您应该使用 DirectWrite。但看起来 DirectWrite 本身并不与 Direct3D 对话?我缺少一些基本的东西吗?如何使用 DirectX 11 绘制简单的文本?

采纳答案by Zee

Edit 2014:

2014年编辑:

As pointed out in comment, the link to RasterTek tutorials no longer functions, here is a link to Webarchive of RasterTekprovided by RadioSpace.

正如评论中所指出的,RasterTek 教程的链接不再起作用,这里是RadioSpace提供的RasterTek Webarchive 的链接

The second point of the original answer is no longer valid either, because its now possible to share D3D11 backbuffer with Direct2D and through it draw text with DirectWrite.

原始答案的第二点也不再有效,因为现在可以与 Direct2D 共享 D3D11 后台缓冲区,并通过它使用 DirectWrite 绘制文本。

Edit 2017:

2017年编辑:

RasterTek is still running: font rendering in D3D11

RasterTek 仍在运行:D3D11 中的字体渲染



I know about two options

我知道两个选项

  • make your own font rendering engine see Rastertek DX11 tutorial

  • second option regarding direct write requires sharing backbuffer between d3d11 and d3d10.1 devices and using dwrite + d2d + d3d10.1 to render gui and d3d11 device to render 3d geometry and merge it all in backbuffer see the post from DieterVW on this thread

  • 制作您自己的字体渲染引擎,请参阅Rastertek DX11 教程

  • 关于直接写入的第二个选项需要在 d3d11 和 d3d10.1 设备之间共享后台缓冲区,并使用 dwrite + d2d + d3d10.1 来渲染 gui 和 d3d11 设备来渲染 3d 几何图形并将其全部合并到后台缓冲区中,请参阅 DieterVW 在此线程上的帖子

At this moment dwrite and d2d dont accept surface created with d3d11 device for rendering. But hopefully MS will make it so soon.

此时 dwrite 和 d2d 不接受使用 d3d11 设备创建的表面进行渲染。但希望 MS 能很快做到。

回答by Daemin

There's a SpriteFontclass in the DirectXTKlibrary that can render text to a DirectX11 device context.

DirectXTK库中有一个SpriteFont类可以将文本呈现到 DirectX11 设备上下文。

回答by Tim Coolman

I recently converted a DirectX 10 application over to DirectX 11 and came across this post while searching for the same answer. The Rastertek tutorial mentioned by Zeela is good, but I continued searching and found FW1FontWrapper. After only about 30 minutes, I just finished integrating it into my project and it appears to be working great. The download section provides both x86 and x64 packages including header, library, and DLL. I am just doing a simple text output, so I can't say a lot about the API, except that for what I did (outputting frames per second), it only took about 5 lines of code (including creating/releasing the wrapper object). Based on his samples it seems to provide a lot more options than what I'm using so far.

我最近将 DirectX 10 应用程序转换为 DirectX 11,并在搜索相同答案时看到了这篇文章。Zeela 提到的 Rastertek 教程很好,但是我继续搜索并找到了FW1FontWrapper。仅仅大约 30 分钟后,我就完成了将它集成到我的项目中,它似乎工作得很好。下载部分提供 x86 和 x64 包,包括头文件、库和 DLL。我只是做一个简单的文本输出,所以我不能说太多关于 API 的事情,除了我所做的(每秒输出帧),它只花了大约 5 行代码(包括创建/释放包装对象)。根据他的样本,它似乎提供了比我目前使用的更多的选择。

回答by Faisal

Use DirectWrite, it support high-quality text rendering, resolution-independent outline fonts, and full Unicode text and layout support.

使用DirectWrite,它支持高质量的文本渲染、与分辨率无关的轮廓字体以及完整的 Unicode 文本和布局支持。