DirectX 9 2D Text in 3D Space

Started by
2 comments, last by Monza 12 years, 3 months ago
I'm using DirectX 9 in C# with SlimDX, and I'm displaying 2D text on a Sprite. However, my text is showing up upside-down. I'm quite new to DirectX, but neither I nor my boss can figure out why the text is upside-down. Also, I'm billboarding the text, but each letter of the text is showing up on its own surface. I'd like the text to show up all on one surface so it billboards correctly (instead of each individual letter billboarding by itself). When I exclude the billboard flag the text displays facing up but still upside-down. I'm not sure if that helps, but that doesn't seem to me to be what "should" happen. [Edited by - dcf22 on June 4, 2009 5:05:29 PM]
Advertisement
OK, I found a solution. Actually, it's the merger of a bunch of solutions from various places online.


// Text to Bitmap:
Bitmap bmp = new Bitmap( 1, 1 );
Graphics g = Graphics.FromImage( ( Image ) bmp );
Font font = new Font( "Arial", 20.0f, Fontstyle.Regular, GraphicsUnit.Pixel );
int width = ( int ) g.MeasureString( text, font ).Width;
int height = ( int ) g.MeasureString( text, font ).Height;
bmp = new Bitmap( bmp, width, height );
g = Graphics.FromImage( ( Image ) bmp );
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillRectangle( Brushes.Transparent, new Rectangle( 0, 0, bmp.Width, bmp.Height ) );
g.DrawString( text, font, new SolidBrush( Color.Yellow ), 0.0f, 0.0f );

// Bitmap to Texture
System.IO.MemoryStream mem = new System.IO.MemoryStream();
bmp.RotateFlip( RotateFlipType.RotateNoneFlipY );
bmp.Save( mem, System.Drawing.Imaging.ImageFormat.Bmp );
SlimDX.Direct3D9.Texture texture = SlimDX.Direct3D9.Texture.FromMemory( device, mem.GetBuffer() );

[end code]

From there I just drew the texture with a Sprite. Sigh... So incredibly annoying...
Unfortunately the above solution uses the GDI+ API to accomplish what can be done in SlimDX.

I am having the same issue. The reason the text appears upside down is when you're rendering the text to a texture it is position at (0, 0) which is the Top Left corner. When trying to render it in 3D space it will be flipped upside down unless the Texture Coordinates and be flipped vertically. I'm still trying to figure out how to do this through the SlimDX wrapper which is where I'm stuck. I know this post is extremely old but it comes up near the top of my Google search when I try to find a solution online.

I managed to get the above solution to work correctly but it looks terrible. The text is drawn at a poor resolution and GDI+ is slow when compared to DirectX. I'm trying to find a solution that does not resort to using a different Rendering API just to get 2D text to billboard correctly in 3D space.
I had the same problem with billboarding - so I gave up using it.

A relatively easy solution which still uses text sprites is to switch to using screen coordinates instead of world coordinates (ie don't use the SpriteFlags.ObjectSpace flag when beginning the sprite).

Just get the screen coordinates from the real world coordinates using the slimdx UNPROJECT method, and use these coordinates to write your sprite to the screen (which will now always be upright and straight). You can even adjust whether you want the text to appear in front of all other graphics by adjusting the Z value of the returned vector3 screen coordinates which control the z-buffer location.

This topic is closed to new replies.

Advertisement