Sign in to follow this  
jesseblue3

[SlimDX] Positioning font in 3d space

Recommended Posts

Hello, I can't figure out how to position my font in 3d space. I have drawn an axis in 3d space, it's a simple line.
Vertex[] vertex = new Vertex[2];       
vertex[0].Position = new Vector3(0, 0, 0);
vertex[0].Color = Colors["axis_x"].ToArgb();
vertex[1].Position = new Vector3(1, 0, 0);
vertex[1].Color = Colors["axis_x"].ToArgb();
Device.DrawUserPrimitives<Vertex>(PrimitiveType.LineList, 1, vertex);
Now, I would like to draw a "X" at the end of the axis (line). But I need the exact pixel position if I do it this way.
Sprite fontSprite = new Sprite(Device);
SlimDX.Direct3D9.Font font = new SlimDX.Direct3D9.Font(Device, 12, 0, SlimDX.Direct3D9.FontWeight.Normal, 0, false, CharacterSet.Default, Precision.Default, FontQuality.ClearTypeNatural, PitchAndFamily.DontCare, "tahoma");
fontSprite.Begin(SpriteFlags.AlphaBlend);
fontSprite.Transform = Matrix.Translation((float) 100, (float) 30, (float) 0);
font.DrawString(fontSprite, "X", 0, 0, new Color4(Colors["axis_x"].ToArgb()));
fontSprite.End();
It's a little bit confusing for me, that the translation needs pixels instead of 3d units?! Is there a simpel way to get the pixel position? Or should I do it not this way? Propably there is a simple way to do this, but I'm relatively new to DirectX :-( Thanks for some ideas! Bye, Martin

Share this post


Link to post
Share on other sites
The other option is to create a 3D mesh for the text, which can then be drawn in the 3D world just like any other object. The Mesh class in Direct3D9 has a method to allow you to do this.

Share this post


Link to post
Share on other sites
Thanks for your help. I managed to draw the font on (0, 0, 0). Now I wanted to move it (1, 0, 0) and my problem is, that the whole picture (all the stuff I drew before) also is moving (1, 0, 0).

Mesh mesh = Mesh.CreateText(Device, new System.Drawing.Font("Verdana", 5, System.Drawing.FontStyle.Bold, GraphicsUnit.Pixel), "SlimDX", 0.5f, 0.1f);
Device.SetTransform(TransformState.World, WorldMatrix * Matrix.Translation(1, 0, 0));
mesh.DrawSubset(0);


I looked in some tutorials for Managed DirectX and they seem to make it the same way. They make a translation draw something and then make again a translation (which doesn't seem to affect the drawings from before) and draw the second object.

Share this post


Link to post
Share on other sites
I think I found the problem. My drawing routine was called some times (of course) and the next calls worked with the transformed matrix. After I restored the original WorldMatrix, all worked pretty fine.

Mesh mesh = Mesh.CreateText(Device, new System.Drawing.Font("Verdana", 5, System.Drawing.FontStyle.Bold, GraphicsUnit.Pixel), "SlimDX", 0.5f, 0.1f);
Device.SetTransform(TransformState.World, WorldMatrix * Matrix.Translation(1, 0, 0));
mesh.DrawSubset(0);
Device.SetTransform(TransformState.World, WorldMatrix);


I also tried it with MatrixStack.Push() and MatrixStack.Pop() but it didn't work this way.

Thanks anyway for your help.

Share this post


Link to post
Share on other sites
You know that MatrixStack doesn't actually apply the matrix to the device on its own, right? You need to actually set it.

Share this post


Link to post
Share on other sites
I've got it now also with the MatrixStack.

MatrixStack.Push();
MatrixStack.TranslateLocal(1.2f, -0.1f, 0.0f);
MatrixStack.ScaleLocal(0.2f, 0.2f, 0.2f);
Device.SetTransform(TransformState.World, MatrixStack.Top);
mesh.DrawSubset(0);
MatrixStack.Pop();
Device.SetTransform(TransformState.World, MatrixStack.Top);


Thanks a lot!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this