From 99a5923fa497f2a6fc568e9141cc8ee81ee2dc82 Mon Sep 17 00:00:00 2001 From: Glatzemann Date: Wed, 9 Nov 2011 08:56:37 +0000 Subject: [PATCH] Kinect: refactored some small things and added experimental handling of Depth and RGB images --- ANX.Framework/ANX.Framework.csproj | 5 +- .../MotionSensingDevice.cs | 23 +++++- .../MotionSensingDeviceState.cs | 15 ++-- .../MotionSensing/MotionSensingDeviceType.cs | 61 ++++++++++++++++ .../InputSystem/IMotionSensingDevice.cs | 8 ++- .../ANX.InputSystem.Windows.Kinect.csproj | 2 +- ANX.InputSystem.Windows.Kinect/Creator.cs | 7 +- ANX.InputSystem.Windows.Kinect/Kinect.cs | 72 +++++++++++++++---- 8 files changed, 158 insertions(+), 35 deletions(-) rename ANX.Framework/Input/{ => MotionSensing}/MotionSensingDevice.cs (85%) rename ANX.Framework/Input/{ => MotionSensing}/MotionSensingDeviceState.cs (91%) create mode 100644 ANX.Framework/Input/MotionSensing/MotionSensingDeviceType.cs diff --git a/ANX.Framework/ANX.Framework.csproj b/ANX.Framework/ANX.Framework.csproj index 0f03f469..868c2e96 100644 --- a/ANX.Framework/ANX.Framework.csproj +++ b/ANX.Framework/ANX.Framework.csproj @@ -276,8 +276,9 @@ - - + + + diff --git a/ANX.Framework/Input/MotionSensingDevice.cs b/ANX.Framework/Input/MotionSensing/MotionSensingDevice.cs similarity index 85% rename from ANX.Framework/Input/MotionSensingDevice.cs rename to ANX.Framework/Input/MotionSensing/MotionSensingDevice.cs index d72ebb6c..f0c5188a 100644 --- a/ANX.Framework/Input/MotionSensingDevice.cs +++ b/ANX.Framework/Input/MotionSensing/MotionSensingDevice.cs @@ -1,6 +1,7 @@ #region Using Statements using System; using ANX.Framework.NonXNA; +using ANX.Framework.Graphics; #endregion // Using Statements @@ -53,7 +54,7 @@ using ANX.Framework.NonXNA; #if XNAEXT -namespace ANX.Framework.Input +namespace ANX.Framework.Input.MotionSensing { public class MotionSensingDevice { @@ -64,12 +65,30 @@ namespace ANX.Framework.Input motionSensingDevice = AddInSystemFactory.Instance.GetCurrentCreator().MotionSensingDevice; } + public GraphicsDevice GraphicsDevice + { + get + { + return motionSensingDevice.GraphicsDevice; + } + set + { + motionSensingDevice.GraphicsDevice = value; + } + } + + public static MotionSensingDeviceType DeviceType + { + get + { + return motionSensingDevice.DeviceType; + } + } public static MotionSensingDeviceState GetState() { return motionSensingDevice.GetState(); } - } } diff --git a/ANX.Framework/Input/MotionSensingDeviceState.cs b/ANX.Framework/Input/MotionSensing/MotionSensingDeviceState.cs similarity index 91% rename from ANX.Framework/Input/MotionSensingDeviceState.cs rename to ANX.Framework/Input/MotionSensing/MotionSensingDeviceState.cs index fa2734db..fe04d983 100644 --- a/ANX.Framework/Input/MotionSensingDeviceState.cs +++ b/ANX.Framework/Input/MotionSensing/MotionSensingDeviceState.cs @@ -52,13 +52,12 @@ using ANX.Framework.Graphics; #endregion // License #if XNAEXT -namespace ANX.Framework.Input +namespace ANX.Framework.Input.MotionSensing { public struct MotionSensingDeviceState { - - private Texture pRGB; - private Texture pDeepth; + private Texture2D pRGB; + private Texture2D pDepth; private Vector3 pHipCenter; private Vector3 pSpine; @@ -83,8 +82,8 @@ namespace ANX.Framework.Input private Vector3 pCount; - public Texture RGB { get { return this.pRGB; } } - public Texture Derpth { get { return this.pDeepth; } } + public Texture2D RGB { get { return this.pRGB; } } + public Texture2D Derpth { get { return this.pDepth; } } public Vector3 HipCenter { get { return this.pHipCenter; } } public Vector3 Spine { get { return this.pSpine; } } @@ -109,11 +108,11 @@ namespace ANX.Framework.Input public Vector3 Count { get { return this.pCount; } } - public MotionSensingDeviceState(Texture _RGB, Texture _Deepth, Vector3 _HipCenter, Vector3 _Spine, Vector3 _ShoulderCenter, Vector3 _Head, Vector3 _ShoulderLeft, + public MotionSensingDeviceState(Texture2D _RGB, Texture2D _Depth, Vector3 _HipCenter, Vector3 _Spine, Vector3 _ShoulderCenter, Vector3 _Head, Vector3 _ShoulderLeft, Vector3 _ElbowLeft, Vector3 _WristLeft, Vector3 _HandLeft, Vector3 _ShoulderRight, Vector3 _ElbowRight, Vector3 _WristRight, Vector3 _HandRight, Vector3 _HipLeft, Vector3 _KneeLeft, Vector3 _AnkleLeft, Vector3 _FootLeft, Vector3 _HipRight, Vector3 _KneeRight, Vector3 _AnkleRight, Vector3 _FootRight, Vector3 _Count) { pRGB = _RGB; - pDeepth = _Deepth; + pDepth = _Depth; pHipCenter = _HipCenter; pSpine = _Spine; diff --git a/ANX.Framework/Input/MotionSensing/MotionSensingDeviceType.cs b/ANX.Framework/Input/MotionSensing/MotionSensingDeviceType.cs new file mode 100644 index 00000000..72cb9f12 --- /dev/null +++ b/ANX.Framework/Input/MotionSensing/MotionSensingDeviceType.cs @@ -0,0 +1,61 @@ +#region Using Statements +using System; + +#endregion // Using Statements + +#region License + +// +// This file is part of the ANX.Framework created by the "ANX.Framework developer group". +// +// This file is released under the Ms-PL license. +// +// +// +// Microsoft Public License (Ms-PL) +// +// This license governs use of the accompanying software. If you use the software, you accept this license. +// If you do not accept the license, do not use the software. +// +// 1.Definitions +// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning +// here as under U.S. copyright law. +// A "contribution" is the original software, or any additions or changes to the software. +// A "contributor" is any person that distributes its contribution under this license. +// "Licensed patents" are a contributor's patent claims that read directly on its contribution. +// +// 2.Grant of Rights +// (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations +// in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to +// reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution +// or any derivative works that you create. +// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in +// section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed +// patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution +// in the software or derivative works of the contribution in the software. +// +// 3.Conditions and Limitations +// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks. +// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your +// patent license from such contributor to the software ends automatically. +// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution +// notices that are present in the software. +// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including +// a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or +// object code form, you may only do so under a license that complies with this license. +// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees, +// or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the +// extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a +// particular purpose and non-infringement. + +#endregion // License + +#if XNAEXT +namespace ANX.Framework.Input.MotionSensing +{ + public enum MotionSensingDeviceType + { + Kinect, + } +} +#endif \ No newline at end of file diff --git a/ANX.Framework/NonXNA/InputSystem/IMotionSensingDevice.cs b/ANX.Framework/NonXNA/InputSystem/IMotionSensingDevice.cs index 57bd9e61..45165e85 100644 --- a/ANX.Framework/NonXNA/InputSystem/IMotionSensingDevice.cs +++ b/ANX.Framework/NonXNA/InputSystem/IMotionSensingDevice.cs @@ -1,6 +1,8 @@ #region Using Statements using System; using ANX.Framework.Input; +using ANX.Framework.Input.MotionSensing; +using ANX.Framework.Graphics; #endregion // Using Statements @@ -54,8 +56,12 @@ using ANX.Framework.Input; #if XNAEXT namespace ANX.Framework.NonXNA { - public interface IMotionSensingDevice + public interface IMotionSensingDevice { + GraphicsDevice GraphicsDevice { get; set; } + + MotionSensingDeviceType DeviceType { get; } + MotionSensingDeviceState GetState(); } } diff --git a/ANX.InputSystem.Windows.Kinect/ANX.InputSystem.Windows.Kinect.csproj b/ANX.InputSystem.Windows.Kinect/ANX.InputSystem.Windows.Kinect.csproj index 458bf6d3..fc346522 100644 --- a/ANX.InputSystem.Windows.Kinect/ANX.InputSystem.Windows.Kinect.csproj +++ b/ANX.InputSystem.Windows.Kinect/ANX.InputSystem.Windows.Kinect.csproj @@ -6,7 +6,7 @@ 8.0.30703 2.0 {E5D69E75-D77C-493F-BBDA-6F9E73B82549} - Exe + Library Properties ANX.InputSystem.Windows.Kinect ANX.InputSystem.Windows.Kinect diff --git a/ANX.InputSystem.Windows.Kinect/Creator.cs b/ANX.InputSystem.Windows.Kinect/Creator.cs index f3ad3b17..518edbbd 100644 --- a/ANX.InputSystem.Windows.Kinect/Creator.cs +++ b/ANX.InputSystem.Windows.Kinect/Creator.cs @@ -67,11 +67,6 @@ namespace ANX.InputSystem.Windows.Kinect factory.AddCreator(this); } - public IMotionSensingDevice Kinect - { - get { return new Kinect(); } - } - #region IInputSystemCreator Member public IGamePad GamePad @@ -86,7 +81,7 @@ namespace ANX.InputSystem.Windows.Kinect public IMotionSensingDevice MotionSensingDevice { - get { return null; } + get { return new Kinect(); } } #endregion diff --git a/ANX.InputSystem.Windows.Kinect/Kinect.cs b/ANX.InputSystem.Windows.Kinect/Kinect.cs index e33d64b7..1e8d9901 100644 --- a/ANX.InputSystem.Windows.Kinect/Kinect.cs +++ b/ANX.InputSystem.Windows.Kinect/Kinect.cs @@ -1,11 +1,15 @@ -using System; +#region Using Statements +using System; using System.Collections.Generic; using System.Linq; using System.Text; -using ANX.Framework.NonXNA; -using Microsoft.Research.Kinect.Nui; using ANX.Framework; using ANX.Framework.Graphics; +using ANX.Framework.NonXNA; +using Microsoft.Research.Kinect.Nui; +using ANX.Framework.Input.MotionSensing; + +#endregion // Using Statements #region License @@ -57,17 +61,21 @@ using ANX.Framework.Graphics; namespace ANX.InputSystem.Windows.Kinect { - public class Kinect:IMotionSensingDevice + public class Kinect : IMotionSensingDevice { + #region Private Members private Runtime pNui; private Vector3[] cache; - private Texture rgb; - private Texture deepth; + private GraphicsDevice graphicsDevice; + private Texture2D rgb; + private Texture2D depth; + + #endregion // Private Members + public Kinect() { pNui = new Runtime(); - pNui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | - RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor); + pNui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor); pNui.SkeletonEngine.TransformSmooth = true; this.cache = new Vector3[21]; @@ -89,19 +97,36 @@ namespace ANX.InputSystem.Windows.Kinect pNui.SkeletonEngine.SmoothParameters = parameters; pNui.SkeletonFrameReady += new EventHandler(pNui_SkeletonFrameReady); - // pNui.DepthFrameReady += new EventHandler(pNui_DepthFrameReady); - // pNui.VideoFrameReady += new EventHandler(pNui_VideoFrameReady); - + pNui.DepthFrameReady += new EventHandler(pNui_DepthFrameReady); + pNui.VideoFrameReady += new EventHandler(pNui_VideoFrameReady); } void pNui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e) { - throw new NotImplementedException(); + if (this.graphicsDevice != null) + { + if (this.rgb == null) + { + this.rgb = new Texture2D(this.graphicsDevice, e.ImageFrame.Image.Width, e.ImageFrame.Image.Height); + } + + //TODO: this works only if the image is in RGBA32 Format. Other formats does need a conversion first. + this.rgb.SetData(e.ImageFrame.Image.Bits); + } } void pNui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e) { - throw new NotImplementedException(); + if (this.graphicsDevice != null) + { + if (this.depth == null) + { + this.depth = new Texture2D(this.graphicsDevice, e.ImageFrame.Image.Width, e.ImageFrame.Image.Height); + } + + //TODO: this works only if the image is in RGBA32 Format. Other formats does need a conversion first. + this.rgb.SetData(e.ImageFrame.Image.Bits); + } } private void pNui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) @@ -130,11 +155,28 @@ namespace ANX.InputSystem.Windows.Kinect return new Vector3(vector.X, vector.Y, vector.Z); } - public ANX.Framework.Input.MotionSensingDeviceState GetState() + public MotionSensingDeviceState GetState() { - return new Framework.Input.MotionSensingDeviceState(rgb, deepth, cache[0], cache[1], cache[2], cache[3], cache[4], cache[5], cache[6], cache[7], cache[8], cache[9], cache[10],cache[11], cache[12], cache[13], cache[14], cache[15], cache[16], cache[17], cache[18], cache[19], cache[20]); + return new MotionSensingDeviceState(rgb, depth, cache[0], cache[1], cache[2], cache[3], cache[4], cache[5], cache[6], cache[7], cache[8], cache[9], cache[10],cache[11], cache[12], cache[13], cache[14], cache[15], cache[16], cache[17], cache[18], cache[19], cache[20]); } + + public GraphicsDevice GraphicsDevice + { + get + { + return graphicsDevice; + } + set + { + graphicsDevice = value; + } + } + + public MotionSensingDeviceType DeviceType + { + get { return MotionSensingDeviceType.Kinect; } + } } }