5,660,782 members and growing! (15,260 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Game Development » Games     Intermediate License: The Code Project Open License (CPOL)

VideoTexture Class and Webcam Application for XNA

By Stephen Bogner

A managed C# video texture class, demonstrated in an XNA WebCamComponent and used in a webcam application
C#, Windows, .NET, Visual Studio, Dev

Posted: 23 Jul 2007
Updated: 31 Jul 2007
Views: 33,157
Bookmarked: 30 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 5.04 Rating: 4.67 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 25.0%
4
9 votes, 75.0%
5

Screenshot - XnaWebcamScreenshot.jpg

Figure 1. Screenshot of the xnaVideoViewer application in action

Introduction

"Video textures" are an important class of multimedia objects with interesting uses in video conferencing, visualization, gaming, robotics and image analysis. The most obvious use -- and therefore the usage that I have focused on in this project -- is to map streaming video from a webcam onto a Texture2D object. This is then applied to a sprite and presented as a live video window within an XNA application. In addition to consuming live video from any of your DirectX video devices, the VideoTexture class is also able to consume AVI, MPEG and WMV format video clips.

The VideoTexture class exposes a property called VideoTexture2D, which is intended to be usable wherever a normal Texture2D object might be used within XNA. This means that it can be used to apply a dynamic video texture onto both sprites and geometric meshes/models. It can also be manipulated by pixel, vertex and geometry shaders, just like any other texture.

Using the code

To use the VideoTexture class, you should:

  1. Copy the VideoTexture.cs file into your own project.
  2. Change the namespace in the VideoTexture.cs file to the name of your project namespace.
  3. Add a reference to DirectShowLib-2005.dll from the DirectShow.NET library.
  4. Compile it. The VideoTexture class should now be available in your project.

The XnaVideoViewer application demonstrates the usage of VideoComponent, which in turn demonstrates the usage of the VideoTexture class.

Points of interest

  • The VideoTexture class depends upon the managed DirectShow.NET library.
  • The VideoTexture.VideoTexture2D property can be polled by other classes to get the most current frame of video. This is what happens, for example, in the VideoComponent.Draw() method. Alternatively, classes that want to use VideoTexture can subscribe to VideoTexture.NewVideoTextureEvent and have the current frame of video pushed to them in NewVideoTextureEventArgs each time a new frame is grabbed from the video stream.
  • VideoComponent automatically registers itself as a service within the game application that uses it. It can be referenced through the Services collection via any other components used by the application.
  • Due to framework limitations within XNA -- specifically, the absence of the System.Windows namespace -- the desired source of video input (i.e. cameras or files) needs to be coded manually in the xnaVideoViewer application. This is done in the LoadComponents() method.
  • The xnaVideoViewer application maps the following commands to the XBox 360 controller: Run = A (Green); Pause = Y (Yellow); Stop = B (Red); Exit = Back.
  • The VideoTexture class has been used to provide the video stream for an experimental HUD application. It has recently been used to stream video textures through vertex shaders to correct lens distortions in real-time as a prelude to stitching the streams into a video mosaic.

History

  • 30 July 2007 - Version 1.1.0.0 uploaded to CodeProject. Changes include renaming and significant refactoring of the xnaVideoViewer, VideoComponent and VideoTexture classes. Support for AVI, MPEG and WMV video files was added. Support for Run, Pause and Stop was added. Support for Looping video clips was added. Two separate methods for capturing and handling DirectShow Filter Graph events were added. Additional error handling code was added.
  • 21 July 2007 - Version 1.0.0.0 uploaded to CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Stephen Bogner


Stephen Bogner is a Senior Research Engineer with Defence R&D Canada. As the Head Autonomous Applications Group, Autonomous Intelligent Systems Section, he only programs when it can't be avoided, and then only in C#.
Occupation: Engineer
Company: Defence R&D Canada
Location: Canada Canada

Other popular Game Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 66 (Total in Forum: 66) (Refresh)FirstPrevNext
QuestionHow to dump a Frame?memberyipling5:09 14 Oct '08  
GeneralDirectShowLib-2005.dll Heeeeelpmembersashdude17:30 7 Jul '08  
GeneralRe: DirectShowLib-2005.dll HeeeeelpmemberStephen Bogner4:25 8 Jul '08  
GeneralRe: DirectShowLib-2005.dll Heeeeelpmembersashdude5:14 9 Jul '08  
GeneralRe: DirectShowLib-2005.dll HeeeeelpmemberStephen Bogner5:45 9 Jul '08  
GeneralToggleFullScreen problem.memberamar212:37 19 Jun '08  
Generalproblem capturing the screenmembercheskochesko0:56 23 May '08  
GeneralProblem with the new xna release 2.0memberMert Ozdag18:46 16 Mar '08  
GeneralRe: Problem with the new xna release 2.0memberStephen Bogner4:03 17 Mar '08  
GeneralAudiomemberAlexandre Ribeiro de Sá16:58 21 Feb '08  
GeneralComposite video inputmembercheskochesko21:57 5 Feb '08  
GeneralChanges Necessary for XNA 2.0memberStephen Bogner10:28 14 Jan '08  
Generalcompile errormemberodinlai10:24 10 Jan '08  
GeneralRe: compile errormemberStephen Bogner5:14 14 Jan '08  
GeneralRe: compile errormemberStephen Bogner8:04 14 Jan '08  
GeneralHow to play two avi files simultaneously?memberOmid.Q.Rose7:30 24 Dec '07  
GeneralHigher Resolution [modified]memberStephen Bogner7:29 11 Oct '07  
Generalhigher resolution for webcammembersccorpius13:45 10 Oct '07  
GeneralIntegration problemmembercheskochesko0:08 5 Oct '07  
QuestionParameters problemmembercheskochesko22:06 2 Oct '07  
AnswerRe: Parameters problem (solved)membercheskochesko23:10 2 Oct '07  
GeneralRe: Parameters problem (solved)membercheskochesko1:07 3 Oct '07  
GeneralNetwork WebcammemberfreshonlineMax20:28 5 Sep '07  
GeneralRe: Network WebcammemberStephen Bogner12:12 6 Sep '07  
GeneralRe: Network Webcammemberalhambra-eidos0:43 6 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Jul 2007
Editor: Genevieve Sovereign
Copyright 2007 by Stephen Bogner
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project