Is this possible in Managed DirectX?

Started by
4 comments, last by gnitsuk 16 years, 2 months ago
Hi, I've done this in OpenGL many times, but after much (very much) looking and trying (and learning) I still can't do it in DirectX. I'm using the latest DirectX SDK (Nov 2007) and Dev Studio 2005. I have a solid cube rendered on the screen. The user can rotate it via the mouse. I want to label each corner of the cube with text (e.g. "(1,1,1)" for that corner etc). I need the text to be of constant size (not bigger if nearer to camera) and for the text to take part in any depth calculations (so text will be obscured by the cube if necessary) and for the text to always face the user - it's just 2D annotation on the 3D scene. I can get the screen position no problem using Vector3.Project etc etc. In OpenGL I use wglUseFontBitmaps together with gluProject and gluUnProject and then call the relevant buildlist. Here's an example from OpenGL of what I mean (using a cylinder instead of a cube): http://gnitsuk.110mb.com/1.bmp Thanks for any help, Mitch.
Advertisement
It is completely doable. Look into the Font class.
Thanks for the reply,

In the Font class I find the DrawText method.

All overloads of this method take a first parameter of type Sprite.

Okay, so I make a Sprite object. I can call:

m_sprite.SetWorldViewLH(m_device.Transform.World, m_device.Transform.View);

m_sprite.Begin(SpriteFlags.ObjectSpace | SpriteFlags.AlphaBlend | SpriteFlags.Billboard);

m_sprite.Draw(m_texture, new Rectangle(0, 0, 2,1),Vector3.Empty, new Vector3(0.0f, 0.0f, -2.0f), Color.White.ToArgb());

So I have a rectangular sprite positioned at some point in 3D space which always faces the user. Even if I could get my text to render on that sprite it would not be right as that sprite's apparent size varies with distance to the camera.

I am doubtless missing something, can anyone put me straight?

Thanks.
A simple fix for that might be to use an orthographic projection when drawing the text, and use a perspective projection for everything else.
Quote:Original post by gnitsuk
I am doubtless missing something, can anyone put me straight?

You can pass null as the sprite parameter, IIRC. That would cause the Font class to use an internal Sprite instance (and would prevent you from getting a performance boost thanks to batching).

Alternatively, just create the Sprite without the ObjectSpace flag, and perform the drawing in screen space. You'll have to calculate the position of the text yourself, but that's a breeze with Vector3.Project().

[EDIT] Also, don't forget to call Sprite::End() when you're done drawing, since Sprite doesn't draw anything untill you call End.
Sirob Yes.» - status: Work-O-Rama.
Hi,

Thanks for your replies. None of thise techniques work however.

The most hopeful, which I had not thought, of was the Orthographic projection.

What I would need to do here is Draw the sprite in Orthographic projection and then the cube in Perspective.

Well, the sprite will not relate correctly to the cube in terms of the cube obscuring the sprite when it is nearer to the camera.

The other approach:

Passing null in the m_sprite.Draw method is no good as the Font's DrawText method takes only a 2D position so there is no way to specify the z value of the text.

Not using the SpriteFlags.ObjectSpace and using Vector3.Project is no good also, for exactly the same reason. These last two methods result in drawing in 2D.

The text has to be positioned in 3-Space, be of constant size and be part of the 3D z-buffered pipeline so that text is correctly obscured by parts of the 3D scene (the cube in this case) and always face the user.

Thanks for any further ideas, has anyone else every done this?

Mitch.

This topic is closed to new replies.

Advertisement