Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.Composition;
  6.  
  7. using SlimDX;
  8. using SlimDX.Direct3D11;
  9.  
  10. using VVVV.PluginInterfaces.V2;
  11. using VVVV.PluginInterfaces.V1;
  12. using VVVV.Utils;
  13. using VVVV.Utils.VMath;
  14. using FeralTic.DX11;
  15. using FeralTic.DX11.Resources;
  16.  
  17. using Microsoft.Kinect;
  18.  
  19. namespace VVVV.DX11.Nodes.MSKinect
  20. {
  21.     [PluginInfo(Name = "RGBDepth",
  22.                 Category = "Kinect",
  23.                 Version = "Microsoft",
  24.                 Author = "tmp",
  25.                 Tags = "DX11, texture",
  26.                 Help = "Returns a G32R32F formatted texture whose pixels represent a UV map mapping pixels from depth to color space. Enable Relative Lookup to use it as displacement texture.")]
  27.     public unsafe class KinectColorDepthTextureNode : KinectBaseTextureNode
  28.     {
  29.         private DepthImagePixel[] depthpixels;
  30.         private ColorImagePoint[] colpoints;
  31.         private float[] colorimage;
  32.         private DepthImageFormat currentformat = DepthImageFormat.Resolution320x240Fps30;
  33.         private int width;
  34.         private int height;
  35.  
  36.         [Input("Relative Lookup", IsSingle = true, IsToggle = true, DefaultBoolean = false)]
  37.         protected Pin<bool> FRelativeLookup;
  38.  
  39.         public KinectColorDepthTextureNode()
  40.         {
  41.             this.RebuildBuffer(DepthImageFormat.Resolution320x240Fps30, true);
  42.         }
  43.  
  44.         private void RebuildBuffer(DepthImageFormat format, bool force)
  45.         {
  46.             if (format != this.currentformat || force)
  47.             {
  48.  
  49.                 this.Resized = true;
  50.                 this.currentformat = format;
  51.                 if (this.currentformat == DepthImageFormat.Resolution320x240Fps30)
  52.                 {
  53.                     this.colpoints = new ColorImagePoint[320 * 240];
  54.                     this.colorimage = new float[320 * 240 * 2];
  55.                     this.depthpixels = new DepthImagePixel[320 * 240];
  56.                     this.width = 320;
  57.                     this.height = 240;
  58.                 }
  59.                 else if (this.currentformat == DepthImageFormat.Resolution640x480Fps30)
  60.                 {
  61.                     this.colpoints = new ColorImagePoint[640 * 480];
  62.                     this.colorimage = new float[640 * 480 * 2];
  63.                     this.depthpixels = new DepthImagePixel[640 * 480];
  64.                     this.width = 640;
  65.                     this.height = 480;
  66.                 }
  67.             }
  68.  
  69.         }
  70.  
  71.         private void AllFrameReady(object sender, AllFramesReadyEventArgs e)
  72.         {
  73.             DepthImageFrame frame = e.OpenDepthImageFrame();
  74.  
  75.             if (frame != null)
  76.             {
  77.                 if (frame.FrameNumber != this.frameindex)
  78.                 {
  79.                     this.FInvalidate = true;
  80.                     this.RebuildBuffer(frame.Format, false);
  81.  
  82.                     this.frameindex = frame.FrameNumber;
  83.                     frame.CopyDepthImagePixelDataTo(this.depthpixels);
  84.  
  85.                     lock (m_lock)
  86.                     {
  87.                         this.runtime.Runtime.CoordinateMapper.MapDepthFrameToColorFrame(frame.Format, this.depthpixels, ColorImageFormat.RgbResolution640x480Fps30, this.colpoints);
  88.                     }
  89.                     frame.Dispose();
  90.                 }
  91.             }
  92.         }
  93.  
  94.         protected override int Width
  95.         {
  96.             get { return this.width; }
  97.         }
  98.  
  99.         protected override int Height
  100.         {
  101.             get { return this.height; }
  102.         }
  103.  
  104.         protected override SlimDX.DXGI.Format Format
  105.         {
  106.             get { return SlimDX.DXGI.Format.R32G32_Float;}
  107.         }
  108.  
  109.         protected override void CopyData(DX11DynamicTexture2D texture)
  110.         {
  111.            
  112.             lock (m_lock)
  113.             {
  114.                 for (int i = 0; i < this.colpoints.Length; i++)
  115.                 {
  116.                     if (FRelativeLookup[0])
  117.                     {
  118.                         this.colorimage[i * 2] = (float)VMath.Map(colpoints[i].X - i % 640, 0, 640, 0, 1, TMapMode.Float);
  119.                         this.colorimage[i * 2 + 1] = (float)VMath.Map(colpoints[i].Y - VMath.Abs(i / 640), 0, 480, 0, 1, TMapMode.Float);
  120.                     }
  121.                     else
  122.                     {
  123.                         this.colorimage[i * 2] = (float)VMath.Map(colpoints[i].X, 0, 640, 0, 1, TMapMode.Clamp);
  124.                         this.colorimage[i * 2 + 1] = (float)VMath.Map(colpoints[i].Y, 0, 480, 0, 1, TMapMode.Clamp);
  125.                     }
  126.                 }
  127.  
  128.                 fixed (float* f = &this.colorimage[0])
  129.                 {
  130.                     IntPtr ptr = new IntPtr(f);
  131.                     texture.WriteData(ptr, this.width * this.height * 8);
  132.                 }
  133.             }
  134.         }
  135.  
  136.         protected override void OnRuntimeConnected()
  137.         {
  138.             this.runtime.AllFrameReady += AllFrameReady;
  139.         }
  140.  
  141.         protected override void OnRuntimeDisconnected()
  142.         {
  143.             this.runtime.AllFrameReady -= AllFrameReady;
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement