5
\$\begingroup\$

I want to render some text over a mesh in my DirectX app. It will have a similar application to health bars for units in games, just as text rather than a bar. I'm looking for the simplest way to do this.

It's a Win32 app, I'm using DirectX 9 and I need it to be in 3D (i.e. when the unit is moving around the Z-axis, the text above moves with it).

Any ideas?

\$\endgroup\$
  • \$\begingroup\$ May I ask why you're using DX9? Are you supporting WinXP or something? \$\endgroup\$ – 3Dave Oct 27 '16 at 14:44
7
\$\begingroup\$

ID3DXFont can be used to render 2D text, and D3DXCreateText can be used to generate a 3D text mesh you could render as well. In general I would think the 2D approach is preferable, unless you want the text to do something odd like spin in 3D space.

To find the appropriate place to start rendering the text (for the 2D case), you'll want to use the character's position in world space (perhaps plus some offset vector that provides a position just above the character's head). Then multiply that point by the combined view and projection matrix via something like D3DXVec3Project. This will yield a screen space position you can render the text to. Note that you may need to offset the screen space position by, for example, -(width of text / 2) or use the DT_CENTER flag if you want the text centered.

\$\endgroup\$
  • \$\begingroup\$ Good answer. One last bit: turn off Z testing before you render the text so that it doesn't render behind stuff. \$\endgroup\$ – munificent Jan 27 '11 at 23:30
  • \$\begingroup\$ Word of warning: D3DXCreateText creates meshes that are the same height regardless of the size of the font selected into the DC (CreateFont). \$\endgroup\$ – Pierre Nov 28 '12 at 17:23
3
\$\begingroup\$

You can use D3DXCreateText to create a text "mesh", which you can then render however you like.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.