[SlimDX] Positioning font in 3d space

Started by
5 comments, last by jesseblue3 14 years, 10 months ago
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
Advertisement
Vector3.Project.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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.
Mike Popoloski | Journal | SlimDX
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.
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.
You know that MatrixStack doesn't actually apply the matrix to the device on its own, right? You need to actually set it.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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!

This topic is closed to new replies.

Advertisement