Michael Springer

In my application, I am procedurally assembling a texture from a non-standard data source.  When I display the texture at a 1:1 ratio, the image is blurred considerably.  I am assembling the same texture to a GDI+ picturebox directly next to this, and its crystal clear.  The difference is one is assembled to a Bitmap while the other is assembled to a Texture2D.  The code is otherwise identical; indeed, they are assembled at the same time with the same code.

I seem to recall encountering the same problem when I started using GDI+ a few years ago, but the solution I used then is not coming to me (and likely would not work here anyway).  I saw in another thread a mention of setting Min, Mag, and Mip filters to Point to remove the standard texture interpolation, but that does not seem to be doing anything at all.

I would provide an example, but I cannot seem to figure out how to paste code into a post (it does not appear in the forum).



Re: XNA Framework Procedural Texture2D = Blurred Texture

Robert Dunlop

Look in the SDK C++ docs for an article titled "Directly Mapping Texels to Pixels" (to lazy to do a web search just now) for a detailed explanation, the short version is that you need to subtract 0.5 from the x and y coordinates of your transformed vertices to get the pixels and texels to line up properly.





Re: XNA Framework Procedural Texture2D = Blurred Texture

Michael Springer

It looks like you are referring to this article: http://forums.microsoft.com/MSDN/AddPost.aspx PostID=1079372&SiteID=1&ReturnUrl=

I tried changing the SpriteBatch to use a Vector2 with an origin at (-.5, -.5), but it still comes up blurry. It looks like SpriteBatch works in integer units as the entire picture was shifted one pixel up and left.

I am still writing the code to try this in 3D and see if the problem goes away.





Re: XNA Framework Procedural Texture2D = Blurred Texture

Michael Springer

I am trying to get a texture to draw on a simple square polygon formed from two triangles, but nothing is displayed (except the client window). I have tried changing my code in a number of places to more closely match examples I have seen on the web, but thus far nothing has worked.

Can someone please point out my error before my head pops :)

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace XNA {

public class Game1 : Microsoft.Xna.Framework.Game {

GraphicsDeviceManager graphics;
ContentManager content;
VertexPositionTexture[] vertices = new VertexPositionTexture[4];
short[] indices;
BasicEffect effect;
Matrix viewMatrix;
Matrix projectionMatrix;
GraphicsDevice device;
VertexBuffer vertexBuff;
IndexBuffer indexBuff;
Texture2D texBricks;

public Game1() {
graphics = new GraphicsDeviceManager(this);
content =
new ContentManager(Services);
}

protected override void Initialize() {

device = graphics.GraphicsDevice;

viewMatrix = Matrix.CreateLookAt(new Vector3(0, 50, 10), Vector3.Zero, Vector3.Up);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 640f/480f, 1f, 100f);

texBricks = content.Load<Texture2D>("brick");

effect = new BasicEffect(device, null);
effect.View = viewMatrix;
effect.Projection = projectionMatrix;
effect.World =
Matrix.Identity;
effect.Texture = texBricks;
effect.TextureEnabled =
true;
effect.CommitChanges();

vertices[0].Position = Vector3.Zero;
vertices[0].TextureCoordinate.X = 0f;
vertices[0].TextureCoordinate.Y = 0f;

vertices[1].Position = new Vector3(255, 400, 0);
vertices[1].TextureCoordinate.X = 1f;
vertices[1].TextureCoordinate.Y = 1f;

vertices[2].Position = new Vector3(0, 400, 0);
vertices[2].TextureCoordinate.X = 0f;
vertices[2].TextureCoordinate.Y = 1f;

vertices[3].Position = new Vector3(255, 0, 0);
vertices[3].TextureCoordinate.X = 1f;
vertices[3].TextureCoordinate.Y = 0f;

vertexBuff = new VertexBuffer(device, VertexPositionTexture.SizeInBytes * vertices.Length, ResourceUsage.None,
ResourceManagementMode
.Automatic);
vertexBuff.SetData<VertexPositionTexture>(vertices);

indices = new short[]{
0, 1, 2,
0, 3, 1
};

indexBuff = new IndexBuffer(device, sizeof(short) * indices.Length, ResourceUsage.None, ResourceManagementMode.Automatic,
IndexElementSize
.SixteenBits);
indexBuff.SetData<short>(indices);

base.Initialize();

}

protected override void LoadGraphicsContent(bool loadAllContent) {
if(loadAllContent) {
}
}

protected override void UnloadGraphicsContent(bool unloadAllContent) {
if(unloadAllContent == true) {
content.Unload();
}
}

protected override void Update(GameTime gameTime) {
if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime) {

device.Clear(Color.CornflowerBlue);
device.VertexDeclaration =
new VertexDeclaration(device, VertexPositionTexture.VertexElements);
device.Vertices[0].SetSource(vertexBuff, 0,
VertexPositionTexture.SizeInBytes);
device.Indices = indexBuff;

effect.CommitChanges();
effect.Begin();
foreach(EffectPass pass in effect.CurrentTechnique.Passes) {
pass.Begin();
device.DrawIndexedPrimitives(
PrimitiveType.TriangleList, 0, 0, 6, 0, 2);
pass.End();
}
effect.End();

base.Draw(gameTime);

}

}

}





Re: XNA Framework Procedural Texture2D = Blurred Texture

Michael Springer

I started working backwards from the BasicEffect example in the documentation. I am starting to see where some of the problems are occuring with the 3D code, though I have not quite figured out why yet.

I also tried saving the procedural Texture to file and loading from there using various texture settings (the exact structure name escapes me), 2D was unchanged. Tried making the area slightly bigger and drawing the texture in the middle using an origin at (.5, .5). Surprisingly, I could not tell the texture had moved and it looked more blurred than the (0,0) origin.

Given that the blur effect seemed uneven before, my current working theory is that the Texture is not being displayed at 100% of size, despite telling XNA to do otherwise.





Re: XNA Framework Procedural Texture2D = Blurred Texture

Michael Springer

Having spent some more time working with the code, I do not think there is a solution to this problem. It finally occured to me that GDI+ uses the standard Windows 2D software rendering whereas XNA is making a trip down through the Direct X libraries and drivers to be rendered in 3D. And any solution to my original problem would also likely be hardware dependent. Its a shame the hardware rendering path is not as accurate as the software path.



Re: XNA Framework Procedural Texture2D = Blurred Texture

The ZMan

If you can see the texture clean in an X*Y picture box then there is no reason that you shouldn't be able to get the same image clean in an X * Y sprite. Can you put an image up on http://www.imageshack.us so that we can see exactly the effect.

[also moving to the XNA forums]






Re: XNA Framework Procedural Texture2D = Blurred Texture

Shawn Hargreaves - MSFT

It would also be useful to narrow down whether the problem is in your code that creates the texture, or the code that displays it.

Do you get similar blurring when drawing images that you just loaded from a file If so, it's your display code that is wrong. If not, it is your texture creation code.

It is certainly possible to display 2D images with no filtering using XNA, so rest assured there is a solution to this.





Re: XNA Framework Procedural Texture2D = Blurred Texture

irishcoder

see if assembling your image as a gdi bitmap and saving into a memory stream and the using the texture2d.fromstream function to generate a texture from the memory stream works It works perfectly for me but its done during initialization and not in the game loop, I presume gdi+ would have a significant performance impact. I use it to generate UI buttons.