Jump to content
  • Advertisement
Sign in to follow this  
SelethD

xna pixel manipulation?

This topic is 3458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
// 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 array
uint[] textureData = new uint[someTexture.Width * someTexture.Height];
someTexture.GetData<uint>(textureData);

// Modify pixel at 50,50 to be white
textureData[50 + 50 * someTexture.Width] = 0xffffffff;

// Copy the array back into the texture
someTexture.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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!