Advertisement
Robomatics

3D Baby Monitor

Jan 12th, 2013
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.79 KB | None | 0 0
  1. Imports Microsoft.Kinect
  2. Imports OpenTK
  3. Imports OpenTK.Graphics
  4. Imports OpenTK.Graphics.OpenGL
  5. Imports System.Threading
  6.  
  7. Public Class Form1
  8.  
  9.     Dim sw As Stopwatch = New Stopwatch
  10.  
  11.     'Kinect Declares
  12.     Dim kinz As KinectSensor
  13.     Dim depthz As DepthImageFrame
  14.     Dim colorz As ColorImageFrame
  15.     Dim vectordata As List(Of Vector3) = New List(Of Vector3)
  16.     Dim colordata As List(Of Color) = New List(Of Color)
  17.  
  18.     'OpenTK Declares
  19.     Dim rotateX As Integer = 0
  20.     Dim rotatey As Integer = 1
  21.     Dim rotatez As Integer = 0
  22.     Dim positionx As Integer = 0 '250
  23.     Dim positiony As Integer = 0 '-250
  24.     Dim Scalez As Decimal = 1
  25.     Dim zpos As Integer = 0 '-100
  26.     Dim mousecontrol As Boolean = False
  27.     Dim mousestartrotate As Point
  28.     Dim mousestartposition As Point
  29.     Dim mousestartZpos As Integer
  30.     Dim mousecontroltype As Integer = 0
  31.     Dim rotateshift As Boolean = False
  32.     Dim translateshift As Boolean = False
  33.  
  34.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  35.  
  36.         sw.Restart()
  37.         sw.Start()
  38.         kinectread()
  39.  
  40.         GlControl1.Invalidate()
  41.  
  42.         Me.Text = sw.ElapsedMilliseconds
  43.     End Sub
  44.  
  45.     Private Sub kinectread()
  46.         If depthz IsNot Nothing And colorz IsNot Nothing Then
  47.             Dim pixz(kinz.DepthStream.FramePixelDataLength - 1) As Short
  48.             depthz.CopyPixelDataTo(pixz)
  49.             Dim pixzC(kinz.ColorStream.FramePixelDataLength - 1) As Byte
  50.             colorz.CopyPixelDataTo(pixzC)
  51.  
  52.             Dim tempx, tempy As Integer
  53.             Dim tripoints As Vector3
  54.             Dim colorpoints As Drawing.Color
  55.             Dim CM As ColorImagePoint
  56.  
  57.             vectordata = New List(Of Vector3)
  58.             colordata = New List(Of Color)
  59.  
  60.             For i = 0 To pixz.Length - 1
  61.  
  62.                 tempy = i / 640
  63.                 tempx = i - (tempy * 640)
  64.                 If tempx < 0 Then
  65.                     tempx = tempx + 640
  66.                 End If
  67.  
  68.                 Dim depth As Single = (pixz(i) >> 5) 'Kinect gives 11 bits resolution, but it needs to be shifted down.
  69.                 Dim xscale As Single = 1 / 594.21434211923247 'These equations are for the Disparity correction
  70.                 Dim yscale As Single = 1 / 591.04053696870778
  71.                 Dim xoffset As Single = 339.30780975300314
  72.                 Dim yoffset As Single = 242.73913761751615
  73.                 Dim x As Single = ((tempx) - xoffset) * depth * xscale
  74.                 Dim y As Single = (((480 - tempy) - 240) - yoffset) * depth * yscale
  75.  
  76.                 tripoints = New Vector3(x, y, depth) 'make the point
  77.  
  78.                 'Get color
  79.                 CM = kinz.MapDepthToColorImagePoint(DepthImageFormat.Resolution640x480Fps30, tempx, tempy, pixz(i), ColorImageFormat.RgbResolution640x480Fps30)
  80.                 CM.X = CM.X - 1
  81.                 If CM.X >= 640 Then
  82.                     CM.X = 639
  83.                 End If
  84.                 CM.Y = CM.Y - 1
  85.                 If CM.Y >= 480 Then
  86.                     CM.Y = 479
  87.                 End If
  88.                 colorpoints = Color.FromArgb(pixzC((((CM.Y * 640) + CM.X) * 4) + 2), pixzC((((CM.Y * 640) + CM.X) * 4) + 1), pixzC((((CM.Y * 640) + CM.X) * 4) + 0))
  89.  
  90.                 If tripoints.Z > 10 Then 'Throw out too small
  91.                     vectordata.Add(tripoints)
  92.                     colordata.Add(colorpoints)
  93.                 End If
  94.  
  95.             Next
  96.  
  97.  
  98.         End If
  99.     End Sub
  100.  
  101.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  102.         For Each potentialSensor In KinectSensor.KinectSensors
  103.             If potentialSensor.Status = KinectStatus.Connected Then
  104.                 kinz = potentialSensor
  105.                 Exit For
  106.             End If
  107.         Next potentialSensor
  108.  
  109.         If kinz Is Nothing Then
  110.             Return
  111.         End If
  112.  
  113.         kinz.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30)
  114.         kinz.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30)
  115.         AddHandler kinz.DepthFrameReady, AddressOf depthready
  116.         AddHandler kinz.ColorFrameReady, AddressOf colorready
  117.         kinz.Start()
  118.  
  119.         kinz.DepthStream.Range = DepthRange.Default
  120.         kinz.ElevationAngle = kinz.MinElevationAngle
  121.     End Sub
  122.  
  123.     Private Sub depthready(ByVal sender As Object, ByVal e As DepthImageFrameReadyEventArgs)
  124.  
  125.         depthz = e.OpenDepthImageFrame
  126.  
  127.     End Sub
  128.     Private Sub colorready(ByVal sender As Object, ByVal e As ColorImageFrameReadyEventArgs)
  129.  
  130.         colorz = e.OpenColorImageFrame
  131.  
  132.     End Sub
  133.     Private Sub GlControl1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GlControl1.Load
  134.         GL.ClearColor(Color.Black)
  135.     End Sub
  136.  
  137.     'GL mouse control
  138.     Private Sub GlControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseDown
  139.         mousecontrol = True
  140.         If e.Button = MouseButtons.Right Then
  141.             mousecontroltype = 2
  142.             mousestartposition = New Point(MousePosition.X - positionx, MousePosition.Y - positiony)
  143.         ElseIf e.Button = MouseButtons.Left Then
  144.             mousestartrotate = New Point(MousePosition.X - rotatey, MousePosition.Y - rotateX)
  145.             mousecontroltype = 1
  146.         ElseIf e.Button = MouseButtons.Middle Then
  147.             mousestartZpos = MousePosition.Y - zpos
  148.             mousecontroltype = 3
  149.         End If
  150.  
  151.     End Sub
  152.     Private Sub GlControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseMove
  153.         If mousecontrol = True Then
  154.  
  155.             If mousecontroltype = 1 Then
  156.                 rotatey = MousePosition.X - mousestartrotate.X
  157.                 rotateX = MousePosition.Y - mousestartrotate.Y
  158.             ElseIf mousecontroltype = 2 Then
  159.                 positionx = MousePosition.X - mousestartposition.X
  160.                 positiony = MousePosition.Y - mousestartposition.Y
  161.             ElseIf mousecontroltype = 3 Then
  162.                 zpos = MousePosition.Y - mousestartZpos
  163.             End If
  164.  
  165.         End If
  166.     End Sub
  167.     Private Sub GlControl1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseUp
  168.         mousecontrol = False
  169.         mousecontroltype = 0
  170.         GlControl1.Invalidate()
  171.     End Sub
  172.     Private Sub GlControl1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseWheel
  173.         Scalez = Scalez + e.Delta / 240
  174.         If Scalez <= 0 Then
  175.             Scalez = 0.1
  176.         End If
  177.         GlControl1.Invalidate()
  178.     End Sub
  179.  
  180.     Private Sub GlControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
  181.  
  182.         GL.Clear(ClearBufferMask.ColorBufferBit)
  183.         GL.Clear(ClearBufferMask.DepthBufferBit)
  184.  
  185.         Dim perspective As OpenTK.Matrix4 = OpenTK.Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000)
  186.         Dim lookat As OpenTK.Matrix4 = OpenTK.Matrix4.LookAt(0, 20, -200, 0, 0, 0, 0, 1, 0)
  187.  
  188.         GL.MatrixMode(MatrixMode.Projection)
  189.         GL.LoadIdentity()
  190.         GL.LoadMatrix(perspective)
  191.  
  192.         GL.MatrixMode(MatrixMode.Modelview)
  193.         GL.LoadIdentity()
  194.         GL.LoadMatrix(lookat)
  195.  
  196.         GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height) 'Size of window
  197.         GL.Rotate(-rotateX, 1, 0, 0)
  198.         GL.Rotate(rotatey, 0, 1, 0)
  199.  
  200.         GL.Translate((-positionx * Scalez), (-positiony * Scalez), (zpos * Scalez))
  201.         GL.Scale(Scalez, Scalez, Scalez)
  202.  
  203.         GL.Enable(EnableCap.DepthTest) 'Enable correct Z Drawings
  204.         GL.DepthFunc(DepthFunction.Less) 'Enable correct Z Drawings
  205.         GL.DepthMask(True)
  206.  
  207.         Dim vertz((vectordata.Count) * 3) As Single
  208.         For i = 0 To vectordata.Count - 1
  209.             vertz((i * 3) + 0) = vectordata(i).X
  210.             vertz((i * 3) + 1) = vectordata(i).Y
  211.             vertz((i * 3) + 2) = vectordata(i).Z
  212.         Next
  213.         Dim colorz((colordata.Count) * 3) As Single
  214.         For i = 0 To colordata.Count - 1
  215.             colorz((i * 3) + 0) = colordata(i).R / 255
  216.             colorz((i * 3) + 1) = colordata(i).G / 255
  217.             colorz((i * 3) + 2) = colordata(i).B / 255
  218.         Next
  219.         GL.PointSize(3)
  220.         GL.EnableClientState(ArrayCap.VertexArray)
  221.         GL.EnableClientState(ArrayCap.ColorArray)
  222.         GL.ColorPointer(3, VertexPointerType.Float, 0, colorz)
  223.         GL.VertexPointer(3, VertexPointerType.Float, 0, vertz)
  224.         GL.DrawArrays(BeginMode.Points, 0, vectordata.Count)
  225.         GL.DisableClientState(ArrayCap.VertexArray)
  226.         GL.DisableClientState(ArrayCap.ColorArray)
  227.  
  228.         GraphicsContext.CurrentContext.VSync = True
  229.         GlControl1.SwapBuffers()
  230.  
  231.  
  232.     End Sub
  233. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement