简体   繁体   English

在DirectX 9中如何在没有D3DX9的情况下绘制线条和字体?

[英]How to draw line and font without D3DX9 in DirectX 9?

I saw that D3DX9 is not recommended by Microsoft and not shipped with Windows SDK. 我看到Microsoft不推荐D3DX9,Windows SDK也不附带D3DX9。 I would like to adopt. 我想采用。

I'm missing my line drawing utility. 我缺少线条画工具。

How can I replace ID3DXLine and ID3DXFont in "new" DirectX9? 如何在“新” DirectX9中替换ID3DXLine和ID3DXFont?

Generally, lines and fonts are sprites. 通常,线条和字体是精灵。 ID3DXLine and ID3DXFont use ID3DXSprite interface under the hood. ID3DXLineID3DXFontID3DXFont使用ID3DXSprite接口。 (Of course, there are other options too, but sprite approach is the most widely used) (当然,还有其他选择,但最常用的是精灵方法)

Drawing sprites 绘制精灵

So, firstly, you will need either 3rd party or your own sprite renderer. 因此,首先,您将需要第三者或您自己的精灵渲染器。 Typically, development of "bedroom" sprite engine, consists of stages: 通常,“卧室”精灵引擎的开发包括以下阶段:

  • drawing bunch of simple colored quads (two triangles forming rectangle). 绘制一堆简单的彩色方形框(形成矩形的两个三角形)。 There are different techniques, but even simplest "all-in-one vertex buffer" approach is not so bad. 有不同的技术,但即使是最简单的“多合一顶点缓冲区”方法也不错。 More advanced techniques includes instancing, point sprites, geometry shader and tessellation tricks (last two are not applicable in DX9). 更高级的技术包括实例化,点精灵,几何体着色器和细分技巧(在DX9中不适用最后两个技巧)。 But don't even try to draw million sprites with million draw calls ;) 但是,甚至不要尝试通过百万次抽奖来抽出百万张精灵;)
  • Texturing those quads. 对那些四边形进行纹理处理。 You will need bitmap loader. 您将需要位图加载器。 If you don't want to use D3DX at all, you can pick open-source FreeImage library for example, of write your own loader. 如果您根本不想使用D3DX,则可以选择开源FreeImage库,例如编写自己的加载器。
  • optimizing rendering using batching. 使用批处理优化渲染。 Sort your sprites, to minimize draw calls number and/or minimize context state changes. 对Sprite进行排序,以最大程度减少绘图调用次数和/或最小化上下文状态更改。
  • optimizing texturing using texture atlases. 使用纹理地图集优化纹理。 You will need to solve rectangle packing algorithm (there are already plenty of implementations on web, or pick up you math book) and roll out some kind of texture atlas format. 您将需要解决矩形打包算法(网络上已经有很多实现,或者是数学书),并推出某种纹理图集格式。

You can choose on what stage you stop. 您可以选择停止的阶段。 Later, you can go back and continue. 稍后,您可以返回并继续。

Drawing lines 画线

Then, for straight lines, you will simply draw a thin rectangular sprite. 然后,对于直线,您只需绘制一个细的矩形精灵即可。 User will input values such as beginning, end and thickness of line, and you will need to do some simple math to calculate position and rotation of this sprite. 用户将输入诸如线条的开始,结束和粗细之类的值,并且您将需要做一些简单的数学运算来计算此子画面的位置和旋转。 Sprite can be just colored or have a texture: for dotted lines, stripped lines, lines with pink fluffy kittens etc. Then, you can implement curved lines as a set of straight lines. Sprite可以仅是彩色的,也可以具有纹理的:对于虚线,带状线,带有粉红色蓬松小猫的线等。然后,您可以将曲线实现为一组直线。 You can optionally add sprites to the ends of line, such as arrows. 您可以选择将精灵添加到行尾,例如箭头。

Drawing text 绘图文字

For text, things can be very complicated (and I will tell only about sprite fonts here): 对于文本,事情可能会非常复杂(我将仅在此介绍精灵字体):

  • each character is a little sprite 每个角色都是一个小精灵
  • you draw texture of a letter over it 你在上面画一个字母的纹理
  • you have a texture with those letters, and sample it using dictionary. 您具有包含这些字母的纹理,并使用字典对其进行采样。 Dictionary is a map of character (or character code) to texture coordinates where it's picture situated, along with additional info, such as spacing, kerning, etc. 字典是字符(或字符代码)到其图片所在位置的纹理坐标的地图,以及诸如间距,字距调整等其他信息。
  • you can have pre-baked (offline) texture atlas with all letters of all fonts of all font sizes you need, along with dictionary. 您可以预先烘焙(离线)纹理图集,其中包含所需的所有字体大小的所有字体的所有字母以及字典。 Obviously you cannot have all letters of all languages on a planet in your resource cache. 显然,您的资源缓存中不能包含星球上所有语言的所有字母。
  • you can bake each character as needed on runtime and add it to your cache (texture atlas + dictionary) 您可以在运行时根据需要烘烤每个字符,并将其添加到缓存中(纹理图集+词典)

To get characters from font file such as .ttf to a bitmap (image) you can use library. 要将字符从字体文件(例如.ttf转换为位图(图像),可以使用库。 FreeType is a best open-source I know. 我知道FreeType是最好的开源。 Parsing fonts yourself can be... complicated. 您自己解析字体可能会很复杂。

You can then mix all together and draw lines with text texture. 然后,您可以将所有内容混合在一起,并用文本纹理绘制线条。 Or draw text surrounded by frame of lines. 或绘制线条框包围的文本。 Or sprite with a text above it. 或在其上方带有文字的精灵。 Or GUI. 或GUI。 All those stuff will be the sprites. 所有这些东西将是精灵。

...or just not bother ...或者只是不打扰

If you still using DirectX 9, do you really need to bother with Windows SDK, removing D3DX stuff? 如果您仍在使用DirectX 9,是否真的需要打扰Windows SDK,删除D3DX内容? Maybe you can continue developing with Direct SDK and D3DX if it works for you? 如果适合您,也许您可​​以继续使用Direct SDK和D3DX进行开发? Note, that if, for some reason, you'll decide to move to DX11, there are DirectXTK , which partially replaces D3DX11 stuff. 请注意,如果由于某种原因决定迁移到DX11,则可以使用DirectXTK ,它可以部分替代D3DX11。 Still, your own, or 3rd party solution will probably be more flexible and suitable for you. 不过,您自己的解决方案或第三方解决方案可能会更灵活并且更适合您。 There are many others applications of sprites in 3D graphics,, such as billboarding, GUI, particles, etc. And as always, reinventing the wheel is a much fun and positive experience ;) 精灵在3D图形中还有许多其他应用,例如广告牌,GUI,粒子等。而且一如既往,重新发明轮子是一种非常有趣和积极的体验;)

Hope it helps. 希望能帮助到你。 Happy coding! 编码愉快!

Why not try and use DirectX 11? 为什么不尝试使用DirectX 11?

Oterhwise OpenGL is supported on almost any platform. 几乎所有平台都支持Oterhwise OpenGL。

I would recommend trying SDL it has helper methods for most 2D stuff you can imagine. 我建议尝试使用SDL,它具有您可以想象的大多数2D东西的辅助方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM