Utility library for creating a video texture from your webcamBy Rim van Wersch, March 24 2006 |
This utility library allows you to capture frames from your webcam to display these as a video texture in Managed DirectX. The library is designed to make this process as easy as possible, so a normal texture can be swapped out for a webcam video texture in about 5 lines of code.
Inside the MDXInfo.WebCam namespace you'll find our WebCam class, which is essentially a wrapper around a customized DirectShow video capture library I found on the codeproject (my apologies to the original author of this library, but I don't recall which one I used exactly). A WebCam object comes with a WebCamTextureManager, which will automatically consume the images from the webcam and use these to update an appropriately sized texture.
Status and possible optimizations
This library is a work in progress and will probably be finalized for release with our Phoenix engine, so it's not completely finished and optimized yet, though it should work decent enough for now (the demo app runs at 600+ fps on a Radeon X850PE). Since it comes with the source code, you might want to look into the following possible optimizations we didn't implement yet for clarity and ease of use.
- The frames are grabbed from the webcam on a seperate thread, which also takes care of the updating of the texture. This approach takes advantage of dual core processors, but on single core machines it might be wiser to perform this in the main render and update thread.
- Frames are copied from unmanaged memory into Bitmaps by the original library, which yields some cleaner code and offers the functionality of the .NET Bitmaps for doing some preprocessing before the frame is copied to the texture. To achieve maximum performance however, the unmanaged image data could be copied directly into the texture in the Grabber support class for the CaptureDevice.
- We're currently using a buffer Bitmap to resample the image to the texture size by drawing the original frame onto the buffer Bitmap and using the buffer to update the texture. This resampling could also be done manually while copying the original Bitmap pixels into the texture, which should yield a better performance.
- When working with video data, the MDXInfo Kickstart book recommends updating an offscreen plain surface with the data, which may be combined with a Device.UpdateSurface call to copy this data onto the texture used for rendering. This would probably be a better, thread-safe approach which may even yield a better performance since rendering with the texture can continue while the surface is being updated.
Files for this resource
Filename | Size |
MDXInfo.WebCam.zip | 412.3 KB |