Advertisement

07.22.2008 at 01:26PM PDT, ID: 23586475
[x]
Attachment Details

Fuzzy and Blurry Text using Sprites in Directx9 C#

Asked by ironwill96 in DirectX Graphics & Game Programming, C# Programming Language

Tags: C#, 2005

Hi,

I am using sprites to try to render text because I need to be able to rotate them etc.  I moved this out of the other post about fuzzy text because it was confusing the original poster's issues and mine are not necessarily directly related.

Here is the code where I actually render the sprites.

[source lang="c#"]
Microsoft.DirectX.Direct3D.Font font = new Microsoft.DirectX.Direct3D.Font(device, l.LayerFont.Height, 0, FontWeight.Normal, 0, false, CharacterSet.Default, Precision.Default, FontQuality.AntiAliased, PitchAndFamily.DefaultPitch, l.LayerFont.FontFamily.Name);                      

                       
Microsoft.DirectX.Direct3D.Sprite mySprite = new Sprite(device);

mySprite.Transform = Matrix.Transformation2D(new Microsoft.DirectX.Vector2(0, 0), 0.0f, new Microsoft.DirectX.Vector2(1, 1), new Microsoft.DirectX.Vector2((float)l.BmpObject.Width/2, (float)l.BmpObject.Height/2), l.RotationAngle, new Microsoft.DirectX.Vector2(l.XPosition - .5f, l.YPosition - .5f));

mySprite.Begin(SpriteFlags.AlphaBlend);
font.DrawText(mySprite, l.LayerText, new Rectangle(0, 0, l.BmpObject.Width, l.BmpObject.Height), DrawTextFormat.Center, Color.Black);
mySprite.End();    



[/source]

And here is the code where I setup my device:

[source lang="c#"]
public void InitializeGraphics()
        {
            PresentParameters pp = new PresentParameters();
            pp.Windowed = true;
            pp.SwapEffect = SwapEffect.Discard;
            pp.EnableAutoDepthStencil = true;
            pp.AutoDepthStencilFormat = DepthFormat.D16;
            pp.DeviceWindowHandle = this.Handle;
            device = new Device(0, DeviceType.Hardware, this,
            CreateFlags.HardwareVertexProcessing, pp);
            //this.LoadTexturesAndMaterials();
            device.DeviceReset += new EventHandler(this.OnDeviceReset);
            OnDeviceReset(device, null);
            initialized = true;
           
        }

private void CameraPositioning()
        {
            if(this.mapMain != null)            
            {                                
                device.Transform.Projection = Matrix.OrthoLH(this.mapMain.getDXWidth(), this.mapMain.getDXHeight(), 1f, 550f);                
                device.Transform.View = Matrix.LookAtLH(new Microsoft.DirectX.Vector3(0, 0, 450), new Microsoft.DirectX.Vector3(0, 0, 0), new Microsoft.DirectX.Vector3(0, -1, 0));              
                               
                device.RenderState.Lighting = false;
               
                device.RenderState.CullMode = Cull.None;
               
            }
           
        }

[/source]

The camera positioning method you can see uses getDXWidth and getDXHeight to grab the size of the custom control that we are using to render DirectX to (we are not rendering to a form).

The "l" object is a custom layer object I have for my drawings that stores a lot of meta-data.  As you can see it has a rotation angle float built in as well as its x and y positions as they are translated.  It also has a bitmap object that represents the size of the rectangle needed to draw the sprite that i'm using to grab the width and height.  My font is pretty fuzzy as you can see in the pictures below and gets worse as soon as I rotate it.  It does seem like i'm getting a texel alignment issue of some sort, but I can't figure out where I would pass the -.5 offset to the X and Y as this article suggests to do: http://msdn.microsoft.com/en-us/library/bb219690.aspx

I tried putting it in my translation matrix but that obviously didn't make any difference and the font.DrawText only takes ints for the rectangle the sprite is put on.

Here are two pictures to illustrate the issue:

Test Image before rotation (note still slightly blurry):
<img src="http://www.greatpcs.net/test1.jpg" />  

Test image after some rotation (note how much more blurry it is):
<img src="http://www.greatpcs.net/test1rotate.jpg" />  

Thanks,
NathanStart Free Trial
 
Loading Advertisement...
 
[+][-]07.22.2008 at 01:35PM PDT, ID: 22063393

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.22.2008 at 02:07PM PDT, ID: 22063686

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.22.2008 at 02:09PM PDT, ID: 22063701

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.22.2008 at 02:14PM PDT, ID: 22063746

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 05:39AM PDT, ID: 22068509

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: DirectX Graphics & Game Programming, C# Programming Language
Tags: C#, 2005
Sign Up Now!
Solution Provided By: ironwill96
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.23.2008 at 05:43AM PDT, ID: 22068544

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628