Output.cs 8.2 KB

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