xna pixel manipulation?

Started by
2 comments, last by MJP 14 years, 11 months ago
Is there a way in Xna to create a 2dtexture and then be able to manipulate individual pixels on it, then show it to the screen as you would any other 2dtexture?
Advertisement
GetData and SetData. Enjoy.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
// Create the texture object. Args are Width, Height, Mipmap count, TextureUsage (usually set to None) and SurfaceFormat (usually set to Color)Texture2D someTexture = new Texture2D(256, 256, 1, TextureUsage.None, SurfaceFormat.Color);// Copy the texture out into an arrayuint[] textureData = new uint[someTexture.Width * someTexture.Height];someTexture.GetData<uint>(textureData);// Modify pixel at 50,50 to be whitetextureData[50 + 50 * someTexture.Width] = 0xffffffff;// Copy the array back into the texturesomeTexture.SetData<uint>(textureData)

Alternatively, you can use Color instead of a uint in the example above if you prefer to work with a color structure instead of a uint.

After you do that, you basically use the texture as you normally would. I would not recommend editing textures multiple times every frame or something though, as copying the data to and from the texture can be pretty costly, not to mention the allocations in C# will go through the roof.
Usually when people think they need to manipulate pixels on the CPU, there's a better GPU-oriented approach available. What is it that you're trying to do?

This topic is closed to new replies.

Advertisement