Sign in to follow this  
Delryn

MDX: Create a new texture from another texture [SOLVED]

Recommended Posts

Hello there! Im using MDX and I need to create a new texture from an existing texture. The new texture can be smaller as the old one, for example is there something like this:
newTexture = anotherTexture.GetRegion(0,0,30,30); 


Im sorry, that I can not post any code, but Im not at home. Textureloading and using is no problem. Thanks :)! [Edited by - Delryn on April 25, 2006 12:32:31 AM]

Share this post


Link to post
Share on other sites
The easiest ways to do this are to use the static methods Device.UpdateSurface (if you don't need stretching) and Device.StrechRectangle (if you do need stretching). Device.UpdateTexture may look easier, but according to the docs this does not allow for copying subregions, only complete textures.

Whatever you choose, be sure to check out the MDX documentation, as all methods place quite a few restrictions on the format, memory pool and other properties of the textures you use. If the textures don't meet the requirements, you'll get an InvalidCallException. And just in case you need to get data from a RenderTarget, Device.GetRenderTargetData is your best bet, however costly it may be.

Share this post


Link to post
Share on other sites
If you'll need to be doing this operation at least once per frame, you could also consider rendering one texture on to the other by making the second a Render Target.
This however isn't really recommended, unless the method presented by remigius gives you performance problems (which is unlikely). Also, making textures a Render Target isn't recommended unless you really need to.

hope this helps :).

Share this post


Link to post
Share on other sites
Thank you two for answering. I know what to do, but I cant code it;
Here is what I've got, I would be glad if you could help me once again ;-)



Microsoft.DirectX.Direct3D.Texture source = new Microsoft.DirectX.Direct3D.Texture( /* Which constructor with which values?! */ );
Microsoft.DirectX.Direct3D.Texture destination = new Microsoft.DirectX.Direct3D.Texture( /* Which constructor with which values?! */ );


Surface sourceSurface = source.GetSurfaceLevel(0);
Surface destinationSurface = destination.GetSurfaceLevel(0);

graphicDevice.UpdateSurface(sourceSurface,
new Rectangle(0,
0,
spriteWidth,
spriteHeight),
destinationSurface);




Share this post


Link to post
Share on other sites
I really should be getting some work done, so here's a quick reply. Untested, so it might need some work.

[source lang=c#]
// You probably want to use TextureLoader.FromFile or something similar here
Texture src = new Texture( device, 256, 256, 1, 0, Format.A8R8G8B8, Pool.SystemMemory);
// I'm assuming you want to copy part of a src sprite onto the dst texture
Texture dst = new Texture( device, spriteWidth, spriteheight, 1, 0, Format.A8R8G8B8, Pool.Default);

using( Surface srcSurf = src.GetSurfaceLevel(0))
using( Surface dstSurf = dst.GetSurfaceLevel(0))
{
device.UpdateSurface(srcSurf,
new Rectangle(0, 0, spriteWidth, spriteHeight),
dstSurf);

}





You will need to dispose of the surfaces once you're done with em, so the using blocks here come in handy. AFAIK, you are limited to the specified Pools as well.

Share this post


Link to post
Share on other sites
Hmm your code works fine but does... nothing, because there was never a bitmapfile for "src" loaded.



Texture src = new Texture( device, 256, 256, 1, 0, Format.A8R8G8B8, Pool.SystemMemory);
// if I load here a texture for src like "src = TextureLoader(...)" the program crashes when Im trying to draw it.
Texture dst = new Texture( device, spriteWidth, spriteheight, 1, 0, Format.A8R8G8B8, Pool.Default);





I checked the parameters (for example the size of src before I loaded the file.
Sorry if Im making stupid mistakes :-/

Share this post


Link to post
Share on other sites
Quote:
Original post by Demirug
UpdateSurface have to many restrictions. Use SurfaceLoader.FromSurface instead.




Great works very fine!!!

Problem solved, thanks to everyone for helping me !

Share this post


Link to post
Share on other sites
Thanks indeed, I didn't know about SurfaceLoader.FromSurface yet... guess my new signature proves true for me already [grin]

[Edited by - remigius on April 24, 2006 2:08:27 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this