XNA Creators Club Online
Page 1 of 1 (11 items)
Sort Posts: Previous Next

How to use Texture2D.SetData properly?

Last post 05-06-2008 2:04 AM by Teddga. 10 replies.
  • 12-31-2007 8:36 PM

    How to use Texture2D.SetData properly?

    I have the data I need to put in the texture in a byte array, but it seems I always get an ArgumentException ("Value does not fall within the expected range.").

    What am I doing wrong?  What should SetDataOptions be if I want to replace the whole texture?

    "length" is 454.  And yes, the size of a bitmap header is 54 bytes ;)
    texture = new Texture2D(GraphicsDevice, 10, 10, 1, TextureUsage.Linear, SurfaceFormat.Rgb32);

    texture.SetData<byte>(tempBuffer, 54, length-54, SetDataOptions.Discard);



  • 01-01-2008 2:22 PM In reply to

    Re: How to use Texture2D.SetData properly?

    Texture2D's have their own in memory format which is encapsulated from the user.  They have different formats on XBox360 and on windows, and the developers don't let you access their memory directly.

    The Bitmap format is simply a on disk format that Texture2D's can be loaded from.  After loading, the specifics of the bitmap format and header lengths is irrelevant.

    The GetData<> and SetData<> methods just operate on the "pixels".

    If you want to replace all of content on a 32 bit Texture2D do this:

    Texture2D texture = // some Texture2D
    int[] pixels = new int[texture.Width * texture.Height];
    //   now manipulate these blank pixels to what you want
    texture.SetData(pixels);

    (Just remove your header length code sections and your sample will probably run fine.)
  • 01-01-2008 5:26 PM In reply to

    Re: How to use Texture2D.SetData properly?

    Can I ask: Why are you manipulating a bitmap directly?

    It looks like you are trying to load some content at runtime (as opposed to building with the pipeline in VS) and there are easier ways to accomplish that. If you provide a little background we can probably save you some headachs.
    Brandon Bloom I am a former XNA Intern rejoining the team soon. Nothing I say is official.
  • 01-01-2008 7:34 PM In reply to

    Re: How to use Texture2D.SetData properly?

    Answer
    I know what the pixel format of the texture is (rgb32) and I know that the data in my array is in that format, once it's past the bitmap header.  All I really want to do is slam the array of bytes (after some offset) into the texture.

    I got this to work when I was using the simpler overload for SetData.

    I'm trying to send the image over the network, which is why I can't just use the content manager the normal way.



    Well, I figured out why the exception was being thrown. The offset from the start of the buffer you pass to SetData has to be a multiple of 4 bytes. 52 works great, except all the color data is shifted =(. I'll solve that problem some other way.
  • 04-23-2008 12:38 PM In reply to

    Re: How to use Texture2D.SetData properly?

    Brandon,

    What is the best way to draw dynamically to a region on the screen rather than loading content from a file? Or at least the basic steps? I need to create an app that draws programmatically as simple as a small tile area so I just need the best

    approach in XNA. I was trying to do the same thing as the original poster: figure out how to create a compatible Texture2D without loading from a file, fill the texture using the SetData<> method and drawing it with the SpriteBatch.Draw() method.

    Is this not the best way to do it? Is there any examples out there?

     

    Thanks

  • 04-23-2008 12:44 PM In reply to

    Re: How to use Texture2D.SetData properly?

    What exactly are you trying to do? There are lots of different things you could be talking about. If you are generating a dynamic texture at load time and re-using it, then SetData is great for it. If you want to create a new texture each frame, that might be too slow to really do using SetData so you could look into other methods.

    NickGravelyn -- Microsoft XNA MVP
    Blog | XNA wiki | My Projects
    Write an Article, Win a Zune!
  • 04-23-2008 3:57 PM In reply to

    Re: How to use Texture2D.SetData properly?

    I think the best way to describe what I need to do in layman terms (to avoid any terminology misunderstandings of what I'm trying to ask) is to look at the viewable area of the screen, divide it into small rectangular areas. The pixels of one or more of these rectangles will be drawn programatically at a rate probably not more than 15 - 20  FPS. Just a simple DirectDraw concept,  but I'd like to figure the best way to do this with Direct3d (using the the XNA framework if possible) for future compatibility and the added benefits of scaling, clipping, hardware support, ect. This is essentially the graphic portion of a CDG (karaoke) file reader so it is in 2D and the data for the graghics are coming from a file in time with an audio file.

    The problem has been that no one seems to agree on the best way to do simple, dynamically generated bitmap writes in 2D using either D3D or XNA. In most cases the MSDN help documentation does not clarify anything more than the name of somehthing, not how it's used and what needs to be done prior to using it.  I was told that the texture to sprite was the only method. Every 2D example in XNA or D3D I've found uses a file for texture data. Text books repeat those examples - what few there are out there on Managed D3D.  I can do this in DirectDraw and I can write a colored tile to the screen viewing area with D3D and XNA using the texture -> sprite, but if there is a better approach than using single textures for each rectangle, I'd like to know before I get too committed on a certain direction.

    In a nutshell:

       - I need to find out the pixel format at runtime for the current selected display. The user can run in windowed (primary display) or fullscreen on a separate monitor. (Easiest method in XNA?)

       - Write pixel data that was calculated programatically to one or more individual small surface rectangles of the screen at a rate of  15 - 20 fps.

       - Allow for verticle and horizontal scrolling.

    Sorry for the long winded reply.  Any direction would be appreciated.

  • 04-23-2008 4:01 PM In reply to

    Re: How to use Texture2D.SetData properly?

    Are you writing visualization data to the screen or the words? If you are doing the words, I'd just read them in from the file and draw them to the screen using SpriteBatch.DrawString. If the visualization data is what you're doing, I'd be inclined to suggest using a shader of some sorts that would be able to render it. I'm not sure if that's possible or how hard it would be, but it seems like something you could offload to the GPU. Or is there something else that you need to render?

    NickGravelyn -- Microsoft XNA MVP
    Blog | XNA wiki | My Projects
    Write an Article, Win a Zune!
  • 04-23-2008 4:08 PM In reply to

    Re: How to use Texture2D.SetData properly?

    Visualization data. The file is the same RedBook spec as a CD + Graphics cdrom. Packets are read in and decoded based on the audio stream. The tiles are created by parsing the bits and matching a 16 color lookup table. All old school techology.

  • 04-23-2008 4:12 PM In reply to

    Re: How to use Texture2D.SetData properly?

    When I was looking at the D3D methods, I thought the best way would be to treat the entire screen as a texture and use the CreateDirtyRectangle method but I couldn't get enough information how to set that up and it was dependent on the surface types and their Pool type settings which I didn't have much luck with. Is there anything in the XNA comparable to that?
  • 05-06-2008 2:04 AM In reply to

    Re: How to use Texture2D.SetData properly?

    I'm trying to figure out much the same for my "game".

    My goal is to make a simple program that allows the user to paint both on a canvas and on the 3D model that it is going to be used on.

    I am not trying to remake Adobe Photoshop, or even MS Paint.  This is just a concept proof.

    The entier program will have 1 window + standard save/load dialog and even the dialog is optional.

    Main window contains a canvas (texture to draw on), 3d viewport for the model, a couple of colors to select(sprites), and maybe a few extra simple controls for whatever seems simple to impliment at the time.

    The XNA documentation is... well, it's a start.  I need to learn: when a texture can be modified with setData<>, and the correct way to do this.  Any functions for adding lines and other painting functions, as I am sure not going to reinvent THAT wheel.  :)  Right now, I'm still struggling with changing a single pixel in the texture.

    Yes, I'm still trying to reinvent the wheel, because they still don't play chess.
Page 1 of 1 (11 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG