# [.net] DrawText

## Recommended Posts

Hello, I am using the DrawText function and it works good but it is being displayed in screen coordinates with a rectangle. Is there a way to get it into 3D coordinates?
[source="csharp"]
DrawText(null, text, rect, DrawTextFormat.Left | DrawTextFormat.Top, Color.Black);


The way I want it is I have a point in 3D space and I want the text to show up on that point. I guess I would need to grab that point and transform it to screen coords and then draw the text?

##### Share on other sites
GDI has no 3D.

To make text 3D you have to use an different technology API that supports 3D.
DirectX, OpenGL, WPF or your own renderer.

##### Share on other sites
Hey ernow,

I've been trying this example in one of Tom Miller's books here..

m = Mesh.TextFromFont(Graphics.device, Graphics.fontType, text, 1f, 1f);
mat = new Microsoft.DirectX.Direct3D.Material();
mat.Diffuse = Color.Black;

Graphics.device.Transform.World = Matrix.Translation(0, 0, 1);
Graphics.device.Material = mat;
m.DrawSubset(0);

When doing this though it just clears the screen of everything. You know a good way to display text with 3D coords? I just need 2D text but able to follow an object.

##### Share on other sites
Just guessing here:
- The diffuse color is black. Is the background black too?
- How's the camera set up? Is the translation still in view? (Near plane, far plane, correct z coordinate?)
- Any lights?

##### Share on other sites
The background is white and lighting is set to false.

The camera is set up like this,

device.Transform.Projection = Matrix.OrthoLH(800, 600, 0.0f, 999.0f);

##### Share on other sites
lol, I just created a way to position text with 2D. Until I figure out how to do the 3D one I will use this.

this.x = 400 + (int)x - (int)Graphics.cameraPosition.X;
this.y = 300 + (int)y - (int)Graphics.cameraPosition.Y;
this.x += 25;

if ((int)y >= ((int)Graphics.cameraPosition.Y - 50))
{
this.y -= 35;
}
else
{
this.y += 5;
}

This works because I draw everything in pixels. I just compute an object's x and y values in world space and compare them to the camera which is always in the middle. x worked right off the bat but had to fiddle around with y to get it positioned properly. I wanted to do it in 3D space so the text could follow an object around as it moves but with this way I will be able to do it also.

I know people say you aren't supposed to plot stuff by pixels but I'm not sure how to do it any other way, maybe you guys could give some examples?

##### Share on other sites
I'm trying to just draw a quad for a background for the text. I only need one so surely there is some way to draw a quad without a vertex buffer.

I heard that DrawUserPrimitives can do this and here is what I got..

t[0] = new CustomVertex.PositionColored(new Vector3(x, y, 1), Color.Red.ToArgb());
t[1] = new CustomVertex.PositionColored(new Vector3(x, y + length, 1), Color.Red.ToArgb());
t[2] = new CustomVertex.PositionColored(new Vector3(x + width, y, 1), Color.Red.ToArgb());
t[3] = new CustomVertex.PositionColored(new Vector3(x + width, y + length, 1), Color.Red.ToArgb());

Graphics.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, t.Length, t);

This doesn't do anything but sometimes I can see these weird black lines appearing when I draw this.

I also tried using GDI and graphics.fillRectangle(), this was real easy and I would like to continue with it but for some reason when drawing a filled rectangle it would flicker off and on and looked pretty crappy. This only happened in fullscreen not windowed.

[Edited by - Meowza on December 29, 2009 11:04:57 PM]

##### Share on other sites
I've narrowed the problem down to this.

Graphics.device.SetStreamSource(0, Map.vb, 0);

When commenting out this and stop drawing everything but the DrawUserPrimitives then it works but when I use a vertex buffer it seems all data is stuck on that vertex buffer.

I'm gonna try and see what happens when I draw everything with out a vertex buffer. Hopefully this won't be a problem since this is a small 2D game.

 - lol dam, since getting rid of the vertex buffer I have been able to compact code like crazy, just redid the engine and it's prolly half as big as I was doing it before. Haven't tested performance yet but it seems the same.

 - So far FPS is 10 frames more but this is probably just how I restructured it. FPS is still crap when in windowed mode though, still trying to figure that one out.

[Edited by - Meowza on December 30, 2009 4:59:38 PM]

##### Share on other sites
Things are going good but still cannot get 3D text going.

m = Mesh.TextFromFont(Graphics.device, Graphics.fontType, "foo", 0f, 0f);
mat = new Microsoft.DirectX.Direct3D.Material();
mat.Diffuse = Color.Black;

Graphics.device.Transform.World = Matrix.Translation(x, y, 2);
Graphics.device.Material = mat;
m.DrawSubset(0);

When I draw this the ground disappears but character sprites are still drawn. Do you need lighting when drawing with a mesh?

Guess it'd help if I post some pics... here is what it looks like normally

http://img6.imageshack.us/img6/6180/testav.png

And here is when I try drawing the 3D text

http://img684.imageshack.us/img684/8568/test2z.png

[Edited by - Meowza on December 30, 2009 9:18:27 PM]

##### Share on other sites
What is the difference between the 'ground' drawing and the 'character' drawing?

There has to be a difference; it looks as if one is affected by the translation and the other one isn't. Or they are both affected but differently.

##### Share on other sites
Hey ernow, thx for the reply man.

I kept playing around with it but I guess it's just over my head. On the other hand I started to play with 2D again and got a system which I like.

http://img695.imageshack.us/img695/1070/22678555.png

Do you see any problems that could occur later on by using this method? It seems to work okay.

##### Share on other sites
If the rest is 2D, this should be much easier than adding 3D text.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628347
• Total Posts
2982204

• 10
• 9
• 24
• 11
• 9