Output.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using OpenTK;
  9. using OpenTK.Graphics.OpenGL;
  10. using MathNet.Numerics.LinearAlgebra.Single;
  11. using bbiwarg.DataSource;
  12. namespace bbiwarg.Graphics
  13. {
  14. class Output : GameWindow
  15. {
  16. private IVideoDataSource source;
  17. private Point[] depthPixels;
  18. private ImageData currentImage;
  19. private List<IGraphicElement> graphicElements = new List<IGraphicElement>();
  20. private int VBOid = new int();
  21. private int IBOid = new int();
  22. private float[] vertices = new float[0];
  23. private uint[] triangles = new uint[0];
  24. private Point palmPoint;
  25. private Point[] fingerPoints;
  26. public Output(IVideoDataSource source)
  27. {
  28. this.source = source;
  29. currentImage = source.getImageData();
  30. }
  31. protected override void OnLoad(EventArgs e)
  32. {
  33. base.OnLoad(e);
  34. Title = "OutputTest";
  35. GL.ClearColor(Color.Black);
  36. initializeDepthPixels();
  37. //initBuffers();
  38. }
  39. protected override void OnRenderFrame(FrameEventArgs e)
  40. {
  41. base.OnRenderFrame(e);
  42. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  43. Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
  44. GL.MatrixMode(MatrixMode.Modelview);
  45. GL.LoadMatrix(ref modelview);
  46. source.releaseFrame();
  47. source.updateFrame();
  48. currentImage = source.getImageData();
  49. updateDepthPixels();
  50. foreach (IGraphicElement graphicElement in graphicElements) {
  51. graphicElement.draw();
  52. }
  53. //updateBuffer();
  54. //drawBuffer();
  55. SwapBuffers();
  56. }
  57. protected override void OnResize(EventArgs e)
  58. {
  59. base.OnResize(e);
  60. GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
  61. Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 3, Width / (float)Height, 0.01f, 3.0f);
  62. GL.MatrixMode(MatrixMode.Projection);
  63. GL.LoadMatrix(ref projection);
  64. }
  65. private void initializeDepthPixels()
  66. {
  67. VertexArray vertexArray = source.getVertexArray();
  68. int numVertices = vertexArray.getNumVertices();
  69. depthPixels = new Point[numVertices];
  70. float size = 0.002f;
  71. for (int i = 0; i < numVertices; i++) {
  72. Vertex vertex = vertexArray.getVertex(i);
  73. Color color = vertexArray.getColor(i);
  74. Point pixel = new Point(vertex, color, size);
  75. depthPixels[i] = pixel;
  76. graphicElements.Add(pixel);
  77. }
  78. Vector palmPosition = source.getPalmPosition3D(1);
  79. Vertex palmVertex = new Vertex(palmPosition[0], palmPosition[2], palmPosition[1]);
  80. palmPoint = new Point(palmVertex, Color.Yellow, 0.005f);
  81. graphicElements.Add(palmPoint);
  82. fingerPoints = new Point[5];
  83. for (int i = 0; i < 5; i++)
  84. {
  85. fingerPoints[i] = new Point(new Vertex(0f, 0f, 0f), Color.Yellow, 0.005f);
  86. }
  87. }
  88. private void updateDepthPixels()
  89. {
  90. VertexArray vertexArray = source.getVertexArray();
  91. int numVertices = vertexArray.getNumVertices();
  92. for (int i = 0; i < numVertices; i++)
  93. {
  94. depthPixels[i].position = vertexArray.getVertex(i);
  95. depthPixels[i].color = vertexArray.getColor(i);
  96. }
  97. Vector palmPosition = source.getPalmPosition3D(1);
  98. palmPoint.position.x = palmPosition[0];
  99. palmPoint.position.y = palmPosition[2];
  100. palmPoint.position.z = palmPosition[1];
  101. Vector[] fingerPositions = source.getFingerTipPositions3D(1);
  102. DetectionStatus[] fingerStatus = source.getFingerStatus(1);
  103. for(int i=0; i<5; i++) {
  104. if(fingerStatus[i] == DetectionStatus.Tracked) {
  105. fingerPoints[i].position.x = fingerPositions[i][0];
  106. fingerPoints[i].position.y = fingerPositions[i][2];
  107. fingerPoints[i].position.z = fingerPositions[i][1];
  108. }
  109. else if(fingerStatus[i] == DetectionStatus.Detected) {
  110. graphicElements.Add(fingerPoints[i]);
  111. fingerPoints[i].position.x = fingerPositions[i][0];
  112. fingerPoints[i].position.y = fingerPositions[i][2];
  113. fingerPoints[i].position.z = fingerPositions[i][1];
  114. }
  115. else if(fingerStatus[i] == DetectionStatus.Inactive && graphicElements.IndexOf(fingerPoints[i]) > -1) {
  116. graphicElements.Remove(fingerPoints[i]);
  117. }
  118. }
  119. }
  120. private void initBuffers()
  121. {
  122. GL.EnableClientState(ArrayCap.VertexArray);
  123. GL.GenBuffers(1, out VBOid);
  124. GL.GenBuffers(1, out IBOid);
  125. GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
  126. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StreamDraw);
  127. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  128. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
  129. GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(triangles.Length * sizeof(int)), triangles, BufferUsageHint.StaticDraw);
  130. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  131. }
  132. public void updateBuffer()
  133. {
  134. List<float> verticesData = new List<float>();
  135. List<uint> triangleData = new List<uint>();
  136. foreach (IGraphicElement graphicElement in graphicElements)
  137. {
  138. List<Vector> elementVertices = graphicElement.getVertices();
  139. uint[] elementTriangles = graphicElement.getTriangleIndices();
  140. Color elementColor = graphicElement.getColor();
  141. uint c = (uint)verticesData.Count / 6;
  142. foreach (Vector elementVertex in elementVertices)
  143. {
  144. verticesData.AddRange(convertColor(elementColor));
  145. verticesData.AddRange(elementVertex);
  146. }
  147. for (int i = 0; i < elementTriangles.Length; i++)
  148. {
  149. triangleData.Add(elementTriangles[i] + c);
  150. }
  151. }
  152. vertices = verticesData.ToArray();
  153. triangles = triangleData.ToArray();
  154. GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
  155. GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StreamDraw);
  156. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  157. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
  158. GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(triangles.Length * sizeof(int)), triangles, BufferUsageHint.StaticDraw);
  159. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  160. }
  161. private void drawBuffer()
  162. {
  163. GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid);
  164. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBOid);
  165. GL.InterleavedArrays(InterleavedArrayFormat.C3fV3f, 0, IntPtr.Zero);
  166. GL.DrawElements(BeginMode.Triangles, triangles.Length, DrawElementsType.UnsignedInt, 0);
  167. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  168. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  169. }
  170. private List<float> convertColor(Color color)
  171. {
  172. List<float> result = new List<float>();
  173. result.Add(color.R / 255f);
  174. result.Add(color.G / 255f);
  175. result.Add(color.B / 255f);
  176. return result;
  177. }
  178. }
  179. }