Using Video as Textures

Another common feature for video files that users must have is the capability to play these movies onto a 3D texture inside the application they are building. Many of the developers use movie files as the "cut-scenes" during their game, and need some way to show the movie in full screen mode. The Audio Video playback classes can handle both cases quite well.

For the simplest case of a developer wanting to use a full screen movie as a cut-scene in the game they are writing, we made it as simple as could be. Simply load the video file into your Video object, set the "Fullscreen" property to true, and play the video. Nothing could be simpler…well, unless there was a constructor that took these parameters.

For the more difficult scenario where the developer has some set of geometry they want to texture using a video, we've come up with a different solution. First, there is an event you will want to hook, TextureReadyToRender. This event will be fired every time there is a new frame in the video file that is ready to be rendered as a texture.

The TextureRenderEventArgs member that is passed into the event handler contains the newly created texture that is ready to be rendered. This event may be fired from many different threads, so ensure that you do not have multiple threads accessing variables simultaneously. Who knows what craziness you can end up with doing that? All you need to do to start the video playing and the event firing is call the method RenderToTexture on the video object. You should begin receiving the events and textures immediately.

The sample that ships with the DirectX SDK renders the entire scene at the same frame rate as the movie being played; it only renders each frame when a new texture is ready. In many cases, this is adequate for the developers, since movies being rendered as a texture is a fairly uncommon occurrence. However, you may still want to use the texture in your "real time" rendering application, so how would you go about that?

First, you will need to detect whether your video texture has been created in the system memory pool or the default pool (since you won't have control over this). You can do this by checking the level description of the first level of the texture.

If the texture was created in the default pool (most common), you will need to use the Direct3D device's method GetRenderTargetData on the surface of the texture and a surface of your own created in the system memory pool. You cannot render textures created in the system memory pool, so you will also need to create a texture in the default memory pool to use for rendering. Something like

 SurfaceDescription ds = e.Texture.GetLevelDescription(0); if (ds.Pool == Pool.Default) {     systemSurface = device.CreateOffscreenPlainSurface(ds.Width, ds.Height,         ds.Format, Pool.SystemMemory); } texture = new Texture(device, ds.Width, ds.Height,     1, Usage.Dynamic, ds.Format, Pool.Default); 

This code will check to see whether your video texture has been created in the default memory pool, and if it has, it will create an off-screen plain surface to hold the texture data, plus a texture in the default memory pool that we can use to render our scene. After this is executed, there are two methods on the device that we need to use to get the data from one texture to the next:

 using(Surface videoSurface = e.Texture.GetSurfaceLevel(0)) {     using(Surface textureSurface = texture.GetSurfaceLevel(0))     {         device.GetRenderTargetData(videoSurface, systemSurface);         device.UpdateSurface(systemSurface, textureSurface);     } } 

As you can see here, we get the render target data from our video texture and move it into our temporary system memory surface. From there, we move it into our real texture. We can then use the texture we created in the default pool as the texture to render our scene.

Since the texture will be accessed by more than one thread, you will need to ensure that only one thread at a time has access to this texture by using the lock semantics on it.

The Audio and Video playback classes are not designed to be fully featured implementations of DirectShow. They do not offer many of the in-depth features, such as video capture or filter graph management. They are designed for the simplest cases of loading audio and video data, and playing this data back; nothing more. They are quick and easy to use, but should not be considered a full and robust solution, nor a "replacement" to the DirectShow APIs already out there.

There are no planned updates for this namespace or references.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net