Wednesday, April 12, 2006

MDX Video texture code

Since I've found that a lot of people have had trouble with creating video textures with Managed DirectX, I decided to post some of the code for it here. The code basically loads the video and then renders when the current frame has been copied.

The following can be put for example where you create the mesh, as I did - or of course where ever else you find suitable:
video = Video.FromFile("test.avi"); //I'll set this to open a video with the file menu later
video.TextureReadyToRender += new TextureRenderEventHandler(onTextureReadyToRender); //Set an event handler to fire when the texture is ready to render.
video.RenderToTexture(_device); // Render the texture with the device (graphics card)
video.Play();
About an hour after writing the post it's time to update it.... I changed some stuff in the eventhandler, giving the following code as result:
public void onTextureReadyToRender(object sender, TextureRenderEventArgs e)
{

if (e.Texture == null)//If there's no texture file (video) for 'e', then get out of here
return;
SurfaceDescription ds = e.Texture.GetLevelDescription(0);
if (ds.Pool == Pool.Default)
{
sysSurf = _device.CreateOffscreenPlainSurface(ds.Width, ds.Height,
ds.Format, Pool.SystemMemory);
}
using (Surface vidSurf = e.Texture.GetSurfaceLevel(0))
{
if (_tex == null)//If there is no texture set to "_tex"
{
_tex = new Texture(_device, ds.Width, ds.Height,
1, Usage.Dynamic, ds.Format, ds.Pool);
}
using (Surface texSurf = _tex.GetSurfaceLevel(0))
{
SurfaceLoader.FromSurface(texSurf, vidSurf, Filter.Linear, unchecked((int)0xffffffff));
}
}
Invalidate();
}

To give some quick comments:
  • The update peoblem (application not updating without user interaction) was fixed when I put in the Invalidate() method.
  • The updating looks quicker than before - but of course it's impossible to tell by the naked eye.
  • The changes were made after I found some useful things in the book("Managed DirectX 9 Kick Start" by Tom Miller in the MDX team)
I believe most is pretty obvious. Just get the video file, check when it's loaded and ready to render, render it to a texture and watch the result...

Sorry about the lack of indents in the code - Blogger doesn't like the easy methods to create indents like tab/space, so I'll just skip that unless I get comments about it ;-)

I still have a lot of trouble with disposing the video texture when closing the application, but since I can't find anything to fix it after trying different ways and searching different places, I'll leave it for now...

The next things I'll do will just be minor issues. like for example stretching the video texture different ways, just to see the effect of it, and if it can be used easy and quick to rectify the frame images..

7 comments:

E. H. said...

The old code, with texture update depending on user interaction, can be seen below:

* public void onTextureReadyToRender(object sender, TextureRenderEventArgs e)
* {
* if (e.Texture == null) //If there's no texture file (video) for 'e', then get out of here
* return;
* // TODO: Figure out how to avoid copying surfaces
* using (Surface s = e.Texture.GetSurfaceLevel(0))
* {
* if (_tex == null) //If there is no texture set to "_tex"
* {
* _tex = new Texture(_device, s.Description.Width, s.Description.Height,
* 1, s.Description.Usage, s.Description.Format, s.Description.Pool);
* }
* using (Surface d = _tex.GetSurfaceLevel(0))
* {
* SurfaceLoader.FromSurface(d, s, Filter.Linear, unchecked((int)0xffffffff));
* }
* }
* }

OpenSebJ said...

Hey,

Do you have a working solution with this example? Would you be able to post one up please ;-)

Cheers,
OpenSebJ

E. H. said...

I'll see if I still have it somewhere, and can then post some more. I made it work, but I'm not using it for anything now.

Most of it is in the post by the way - is it something in particular you need?

E. H. said...

I made a new post with more code:

http://hundev.blogspot.com/2006/07/requested-video-texture-in-mdx.html

I hope it helps - tell me if you find out how to completely dispose of the video texture ;p

Anonymous said...

Hello E.H.

Do you have a complete example?

I integrate your code in my code, but the video is bucking. It is very slow and flickers.

I try it also with the code from this link:
http://hundev.blogspot.com/2006/07/requested-video-texture-in-mdx.html

Anonymous said...

Howdy,

I mostly visits this website[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url]Plenty of useful information on hundev.blogspot.com. I am sure due to busy scedules we really do not get time to care about our health. In plain english I must warn you that, you are not serious about your health. Recent Research presents that about 50% of all USA grownups are either chubby or overweight[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url] So if you're one of these people, you're not alone. In fact, most of us need to lose a few pounds once in a while to get sexy and perfect six pack abs. Now next question is how you can achive quick weight loss? [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss[/url] is not like piece of cake. You need to improve some of you daily habbits to achive weight loss in short span of time.

About me: I am writer of [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss tips[/url]. I am also mentor who can help you lose weight quickly. If you do not want to go under hard training program than you may also try [url=http://www.weightrapidloss.com/acai-berry-for-quick-weight-loss]Acai Berry[/url] or [url=http://www.weightrapidloss.com/colon-cleanse-for-weight-loss]Colon Cleansing[/url] for effective weight loss.

Anonymous said...

Great website, looks very clean and organized. Keep up the good work! antibacterial Read a useful article about tramadol tramadol