如何在 Direct3D9 中渲染格式化文本?

发布于 2024-08-20 08:44:20 字数 512 浏览 7 评论 0原文

我正在编写一个应用程序,需要在 Direct3D9 中绘制大量文本(几行,也许几十行)。文本可以采用多种格式(即不同的字体、样式、大小),并且可以包含来自不同字符集的 unicode 符号。最糟糕的是,它可以动态更改,因此它需要是动态的(渲染一次显示总是不行)。

这样做有两个问题:首先,我可能需要大量调用 D3DXCreateFont,这据说是昂贵的(我自己也不确定)。另一种方法是在绘制多部分线之前创建所有字体,然后在它们之间切换 - 这样更好吗?好吧,我还可以在绘制线条时动态创建字体对象,将它们添加到某种字体对象缓存中,然后在尝试创建新字体之前先查看缓存?您认为哪种方法是最好的?

第二个问题是 D3DXFont 似乎不理解下划线/删除线字体样式。尽管 D3DFONT 基于 LOGFONT,但它省略了这些字段(尽管支持斜体)。假设我真的需要下划线/删除线,我该怎么办?有没有办法强制 ID3DXFont 做下划线?我应该自己画这些线吗(我该如何快速完成)?或者也许我应该切换到在 HDC 上使用 GDI 进行绘图,然后将这些像素复制到纹理中 - 这会提供合理的性能吗?

I'm writing an application which needs to draw a lot of text - several lines, maybe tens of lines - in Direct3D9. The text can be heavily formatted (i.e. different typefaces, styles, sizes) and can contain unicode symbols from different charsets. Worst of all, it can change on the fly, so it needs to be dynamic (render once display always won't do).

There are two problems with that: first, I'll probably need a lot of calls to D3DXCreateFont, which are supposedly costly (I'm not sure myself). Another approach is to create all the fonts before drawing the multi-part-line, and then just switch between them - is this better? Well, I can also create font objects on-the-fly as I draw the line, add them to some kind of font-object-cache and then look in cache before trying to create a new one? What do you think, which is the best approach?

The second problem is that D3DXFont seem to not understand underline/strike-through font styles. Although D3DFONT is based on LOGFONT, it omits those fields (supports italic though). Let's say I really really need underline/strike-through, what do I do? Is there a way to force ID3DXFont to do underline? Should I just draw those lines myself (how do I do that fast)? Or maybe I should switch to drawing with GDI on HDC and then copying those pixels into texture - will that provide reasonable performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-08-27 08:44:21

好吧,我还可以在绘制线条时即时创建字体对象,将它们添加到某种字体对象缓存中,然后在尝试创建新字体之前先查看缓存?您认为哪种方法最好?

缓存方法。甚至不要在需要时尝试在没有缓存的情况下即时重新创建字体。我不知道 D3DXCreateFont 的实际成本,但该函数在设计上不打算被过于频繁地调用。

关于下划线/删除线 - 您可以使用 IDirect3DDevice9::DrawPrimitiveUPD3DPT_LINELIST 以及直通着色器在顶部自行渲染线条,以保持线条顶点不变和不变换。

或者也许我应该切换到在 HDC 上使用 GDI 进行绘制,然后将这些像素复制到纹理中 - 这会提供合理的性能吗?

我不会,因为它需要将纹理内容从系统 RAM 传输到 VRAM。

Well, I can also create font objects on-the-fly as I draw the line, add them to some kind of font-object-cache and then look in cache before trying to create a new one? What do you think, which is the best approach?

The caching approach. Don't even try to recreate the fonts on-the-fly whenever you need them, with no caching. I'm unaware of D3DXCreateFont's real cost, but the function is by design not intended to be called too frequently.

Regarding underline/strikeout - you could render the lines yourself on top using IDirect3DDevice9::DrawPrimitiveUP with D3DPT_LINELIST and a pass-through shader to leave the line vertices unchanged and untransformed.

Or maybe I should switch to drawing with GDI on HDC and then copying those pixels into texture - will that provide reasonable performance?

I won't because it needs to transfer the texture contents from system RAM to VRAM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文