Emil Hunefalk

Managed DirectX with Video Texture

On one of my old blogs I once got a request for some code using the TextureReadyToRender with video surfaces in MDX. The following works, but there’s a problem with disposing the video texture after the video ends. I’ve seen a solution or two, but never tried implementing because this method wouldn’t work with my project either way. If you find a good solution for the disposing a comment would be welcome – or if you ask maybe I could find my old sources again


protected VertexBuffer CreateVertexBuffer(Device dev)
    {
    try
    {
    video = Video.FromFile("test.avi");//videoTexPath
    

    video.TextureReadyToRender += new TextureRenderEventHandler(onTextureReadyToRender);
    video.RenderToTexture(dev);
    video.Play();

    vidLength.Text = Convert.ToString(video.Duration);
    }
    catch (Exception err)
    {
    MessageBox.Show(err.ToString());
    }

    CustomVertex.PositionTextured[] quad = new CustomVertex.PositionTextured[4];
    quad[0] = new CustomVertex.PositionTextured(-300.0f, -300.0f, 0.0f, 0.0f, 0.0f);
    quad[1] = new CustomVertex.PositionTextured(-300.0f, 300.0f, 0.0f, 0.0f, 1.0f);
    quad[2] = new CustomVertex.PositionTextured(300.0f, -300.0f, 0.0f, 1.0f, 0.0f);
    quad[3] = new CustomVertex.PositionTextured(300.0f, 300.0f, 0.0f, 1.0f, 1.0f);

    VertexBuffer buf = new VertexBuffer(
    typeof(CustomVertex.PositionTextured), <em>// What type of vertices</em>
    4,<em> // How many</em>
    dev, <em>// The device</em>
    0, <em>// Default usage</em>
    CustomVertex.PositionTextured.Format, <em>// Vertex format</em>
    Pool.Default); <em>// Default pooling</em>

    GraphicsStream stm = buf.Lock(0, 0, 0);
    stm.Write(quad);

    buf.Unlock();
    return buf;
    }
    
protected void onTextureReadyToRender(object sender, TextureRenderEventArgs e)
    {
    if (e.Texture == null)
    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)
    {
    _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();
    }

This post was inspired by some useful things in the book “Managed DirectX 9 Kick Start” by Tom Miller in the MDX team

This is a shortened version of a longer post on my old developer blog.

  • LinkedIn

    If you want to see my LinkedIn profile, click on this button:

    Emil Hunefalk
  • Find & Follow

    hunefalk StumbleUponhunefalk Twitterhunefalk Flickrhunefalk Delicioushunefalk Facebookhunefalk Friendfeedhunefalk LinkedInhunefalk YouTubehunefalk Last.fmhunefalk MyBlogLoghunefalk Google Readerhunefalk Picasawebhunefalk Facebook Profile
  • Comments

  • Tag Cloud

  • addin
  • development
  • mail-client
  • ms-outlook
  • programming
  • visual-csharp